2

As in C++ we use,

using namespace std;

Or in Java we use,

import packageName;

Is there a way to import / use a package or namespace while writing Force.com Apex code? For instance, I am writing simple Apex in Developer Console in "Execute Anonymous Window" as

System.debug('Hello world!');

Is there any way to write something like

import System;

debug('Hello world!');

This link seemed relevant, but did not answer my syntactical concern.

Any help is appreciated! Redirecting to something obvious which I might have missed is more than welcome, Thanks!

Community
  • 1
  • 1
RVP
  • 71
  • 1
  • 7

1 Answers1

2

I think this is a hidden detail in APEX. I've extended classes and referenced system methods without ever having to worry about it.

It might work to think of Salesforce managing one big import list that it adds to all of your code. When you declare a class as global it gets added to your global import list. At least this line in the documentation hints in that direction, "The global access modifier declares that this class is known by all Apex code everywhere."

Regarding namespaces, I've only seen this made available as part of managed packages. So for instance, SteelBrick's managed package QuoteQuickly has the prefix SBQQ on all of it's code. More about that here. I don't believe you can namespace in any other way.

There are some really sharp peeps over at salesforce.stackexchange.com that would know better than me though. I think you should ask over there.


EDIT At DreamForce '13 I attend a presentation by Steven Herod where he made his DTO classes inner classes and referenced them elsewhere like this: QuoteModel.QuoteRequest and QuoteModel.QuoteResponse. I asked him about it and he said it was a decent way of mimicking namespaces. I found his slide deck but it didn't include the code he demo'd live. I found this code which illustrates it a bit anyways. Look for addTextrBox.textBoxClass.

twamley
  • 801
  • 2
  • 14
  • 22