1

I am using Schema class to retrieve metadata of object.

Controller :

getCustomObject : function(cmp, event, helper) {
   var action = cmp.get("c.getCustomObject");
   var objectName = component.get("v.objectName"); // objectName = 'Test__c' without namespace
   action.setParams({
        "jsonObject" : JSON.stringify(obj)
    });
    action.setCallback(this, function(response){
    var state = response.getState();
        if (state === "SUCCESS") {
            var result = response.getReturnValue();
            // something login
       }
    });
    $A.enqueueAction(action);
}

Apex class :

@AuraEnabled
public static void getCustomObject() {
   Map<String, Schema.SObjectField> objectFieldSetMap = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
}

This was working fine as I was passing the objectName from my lightning component. But when I tested this in Sandbox after creating managed package and tested it throws

NullPointerException

After searching little bit I found the objectName coming from lightning component is without Namespace.

But in my scratch org it doesnt work with namespace. Please ignore any typing mistake or let me know if something is missing or I am not clear.

What would be the feasible solution for this?

  • Did you specify a namespace in your scratch org configuration file? Did you link the namespace to your Dev Hub org? – sfdcfox May 16 '18 at 15:05
  • Yes I have already specified... – Nupur Bhagat May 18 '18 at 4:25
  • I am not able to think who the lightning attribute will add namespace after creating the managed package? – Nupur Bhagat May 18 '18 at 4:28
  • Where is "v.objectName" coming from? Why would it be "Test__c" instead of "ns__Test__c"? I think you've done something wrong in your code somewhere, because the namespace shouldn't be required in your own code. – sfdcfox May 18 '18 at 4:40
  • "v.objectName" is an attribute type of "String". earlier everything was working fine and it was with NS only... but for SalesforceDx i removed all the namespace and it started creating issue. – Nupur Bhagat May 18 '18 at 5:20
2

You can create a method to get the org namespace prefix. Create a custom label and add the namespace prefix value in that custom label. When installing the package, put the value as namespace prefix else "c". Call this method to get the namespace prefix

public class NamespaceClass {
    public static String getNameSpacePrefix(){
        return Label.NamespacePrefix != 'c'? Label.NamespacePrefix : '';
    }
}

@AuraEnabled
public static void getCustomObject() {
   Map<String, Schema.SObjectField> objectFieldSetMap = 
   Schema.getGlobalDescribe().get(NamespaceClass .getNameSpacePrefix()).getDescribe().fields.getMap();
}

Also, ensure to put the two underscore value in the custom label if you have a package. For ex: namespace prefix in the custom label will be either "test__" or "c"

  • Instead of using a Label, maybe automatic detection would be preferred? – sfdcfox May 16 '18 at 15:03

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.