Developing in scratch orgs with a namespace

As previously mentioned, it's a good idea to avoid hardcoding the namespace in your code, so that you can move the code between packages or reuse it in non-packaged situations. When making explicit references, the Apex compiler avoids the need for this and infers the namespace reference required.

However, Apex that utilizes Dynamic Apex to create and populate SObject instances and fields through the SObject.get and SObject.put methods will need to dynamically apply the namespace prefix. The following code snippet is a good way to determine whether the code is running in a namespace context. You can place this in a test utility class, as shown here:

DescribeSObjectResult seasonDescribe = 
Season__c.SObjectType.getDescribe();
String namespace =
seasonDescribe.getName().
removeEnd(seasonDescribe.getLocalName()).
removeEnd('__');

Apex tests that utilize Type.forName to create instances of Apex classes (for example, a test written for an application plugin feature) will also need to dynamically apply the namespace prefix to the class name given to the plugin configuration (such as the Custom Metadata type featured in Chapter 10, Providing Integration and Extensibility).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.119.17