9

We have one dev org(without namespace) and one packaging org(with namespace) for developing lightning app. We are trying to access custom field in Lightning helper method as expence__c. It works fine in dev org. But in managed package response returned from the server controller prefixes namespace to given field as namespace__expence__c.

Code tries to access field expence__c which in turn gives field undefined error.

We can't hardcode namespace in code as it won't work in dev org.

We found two solution to this.

  1. Add/Remove namespace to field depending on org in Lightning code. (not preferred)

  2. Write DTO(data transfer object) class for all objects which we wish to use in Lightning. (need to create multiple DTO classes for those objects)

What is the best way to handle given problem.

5
  • Can you use c__expense__c? I don't know if that would work at all, just a thought.
    – Kris Gray
    Commented Nov 29, 2016 at 6:17
  • @KrisGray sorry given solution is not working in dev org. As apex controller returns field as expence__c and js code is trying to access as c__expence__c. Thinks of it as js object where expenc__c field is key in expense object and you are trying to access key with/without prefixes. In dev org there is no prefix in key so it won't work and will throw key undefined error.
    – Soham
    Commented Nov 29, 2016 at 7:08
  • @KrisGray This is a known limitation which adds pain to adopt lightning for managed package apps . Commented Nov 29, 2016 at 10:18
  • @MohithShrivastava pls check this passing-complex-structured-data-type-from-lightning-to-apex I'm getting same kind of salesforce internal error while passing wrapper class to controller. Why we need to serialize/deserialize, is this known bug? Code works fine for objects but not for classes.
    – Soham
    Commented Nov 30, 2016 at 14:28
  • Soham that's a bug as well Commented Nov 30, 2016 at 14:32

3 Answers 3

7

Thats a current limitation of the platform .The Javascript file does not get namespace added unlike visualforce pages for managed package .

I wrote a blog post on this .

The best way would be to create a message layer or a wrapper class like your second approach and that avoids the problem .The only caveat is you end up writing bunch of classes .

0
0

Here is the full description

Component used in markup ==><yournamespace:myComponent />

Component used in a system attribute ==>

<aura:component extends="yournamespace:myComponent">

<aura:component implements="yournamespace:myInterface">
<aura:component controller="yournamespace.ExpenseController">
<aura:attribute name="expenses" type="yournamespace__Expense__c[]" />
<aura:attribute name="newExpense" type="yournamespace__Expense__c"
       default="{ 'sobjectType': 'yournamespace__Expense__c',
           'Name': '',
           'yournamespace__Amount__c': 0}" />
<aura:handler event="yournamespace:updateExpenseItem" action=… />
<aura:registerEvent type="yournamespace:updateExpenseItem" name=… />
<aura:handler event="yournamespace:updateExpenseItem" action=… />
<aura:dependency resource="markup://yournamespace:myComponent" />
<ltng:require scripts="{!$Resource.yournamespace__resourceName}" styles="{!$Resource.yournamespace__resourceName}" />

etc.

0

Not sure if this has been solved a different way yet, but we found an elegant way of doing this is returning the SObject as a JSON string then letting JavaScript parse the JSON string. We only need to do this for any method that returns custom objects.

in apex:

@AuraEnabled
public static String getSObject() {
  return JSON.serialize(customSObject).replace('namespaceprefix__', '');
}

in Javascript whatever variable is assigned this value:

data = JSON.parse(data);
fieldValue = data.fieldWithoutNamespacePrefix;

You must log in to answer this question.

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