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?

6
  • Did you specify a namespace in your scratch org configuration file? Did you link the namespace to your Dev Hub org?
    – sfdcfox
    Commented May 16, 2018 at 15:05
  • Yes I have already specified... Commented May 18, 2018 at 4:25
  • I am not able to think who the lightning attribute will add namespace after creating the managed package? Commented May 18, 2018 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
    Commented May 18, 2018 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. Commented May 18, 2018 at 5:20

1 Answer 1

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"

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .