I have custom objects with some standard fields and some custom fields and I want to be able to load test data using Test.loadData() and static resources.

The trouble is that I can't hard-code the namespace in the static resources because every developer has different namespace in their orgs. The call to Test.loadData() errors out because it can't the field myNamespace__myField__c on an org that had a different namespace.

So my question is, can I load test data from static resources without hard-coding the namespace in the resource? Or, would it be less overhead to just instruct the other developers to replace the namespace in the static resources before running the tests?

  • 1
    We solve namespace issues as a build step. We use a token to represent the namespace, and the ant job we use to deploy codes to the org replaces that token with the valid namespace for the org we use. Each developer could have their own override settings in ant to set their own namespaces. If you're interested I could share a bit more of this as an answer. – mkorman Dec 18 '15 at 14:59
  • Hello @mkorman and thanks for the reply. Yes, I'd be interested in more detail. Does the ant script pre-pend the namespace to the values from certain static resource files? If so, is there some intelligence built in that distinguishes when to include the namespace and when not to? – Joe Pescatello Dec 18 '15 at 18:17
  • We add a token to our source code that gets replaced by the namespace on build. I will post some examples if I have more time. – mkorman Dec 19 '15 at 9:15

This is how we've solved this. Essentially, we use an ant build step to replace a token by the right namespace.

Our actual source code (what we check into SVN) may have this line:

private @@namespace@@__CustomObject__c customObj = null;
private @@namespace@@.GlobalClass instance = null;


if (customObj.@@namespace@@__CustomField__c == null) ...

Every time we reference an object, field or class from our package, we use the @@namespace@@ token. This would never compile, as it's not valid language syntax.

Ant to the rescue! We use the Force.com migration tool, which is based on ant. We have added an ant target that looks like this:

<target name="prepareDeployment"
    description="Prepares the files for deployment"
    <echo message="Preparing deployment files..." />
    <copy todir="deploy">
        <fileset dir="..\src"/>
    <echo message="Setting namespace..." />
    <replace dir="deploy" token="@@namespace@@" value="${user.namespace}"/>

This target essentially copies all files in the "src" folder to the "deploy" directory, then runs the token replacement.

user.namespace is a variable defined in the user-specific settings. This way each developer could have his own namespace.

This target would be a dependency for the actual deployment ant target.

Every time we deploy our code to our org, it runs the "prepare" target, replaces the namespace token with the actual namespace, and the code will compile and pass tests in the SF org.

  • Great solution! Thanks for posting such a detailed account of it. – Joe Pescatello Dec 21 '15 at 13:12
  • Glad it could help. I have edited a minor issue in my post. You obviously cannot reference to public classes from your package, but to global ones instead – mkorman Dec 21 '15 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.