I have a requirement where user needs to create a new Opportunity record in a lightning popup.Here I have to display a Territory field which is a lookup .So I am using lightning:inputField tag for this within lightning:recordEditForm.

 <lightning:inputField aura:id="Territory" fieldName="Territory__c" required="true"/>

But now user wants to have a default territory to be shown on initial loading. If they dont want the default territory, they can change that value. So in order to show default value I have added value field in lightning:inputField

 <lightning:inputField aura:id="Territory" fieldName="Territory__c" value="{!v.defaultTerritory}" required="true"/>

defaultTerritory --> Assigning it from backend on loading.

Now if I try to save the Opportunity with default territory, the JSON record passed from lightning to apex is

{"sobjectType":"Opportunity","Name":"Jan 2201-1","AccountId":"00X0j00000KGXXXXXX","Territory__c":"a0X0j000000nXXXXXX","CurrencyIsoCode":"JPY","OwnerId":"00Xf100000HIuXXXX"}

If I change from default territory to new territory in lookup , on saving the JSON record passed is,

{"sobjectType":"Opportunity","Name":"Jan 2201-1","AccountId":"00X0j00000KGXXXXXX","Territory__c":["a0X0j000000nXXXXXX"],"CurrencyIsoCode":"JPY","OwnerId":"00Xf100000HIuXXXX"}

The territory Id is enclosed within a square bracket[].

Could anyone please let me know what is the mistake I am doing.Why [] is getting added while changing territory and how to resolve it.

  • Whats data type of v.defaultTerritory ? – Pranay Jaiswal Jan 22 '19 at 14:40
  • How is your save function implemented? – Jayant Das Jan 22 '19 at 15:41
  • v.defaultTerritory is a String which stores the Id of the Territory – Sindhuja Jan 23 '19 at 4:53
  • I am passing the entire Opportunity record from Lightning component to apex class.In save method, i am inserting this Opportunity using Database.insert – Sindhuja Jan 23 '19 at 4:56

I hope you got the workaround to resolve the issue.

But still, let me give the workaround and it would help others

  • If you change from default value to new value in lookup, the value will be assigned as a list in JSON i.e, "Territory__c":["a0X0j000000nXXXXXX"] enclosed within a square bracket[].

I would suggest before assigning value typecast it to a string and assign to JSON in your JS controller. i.e, var Territory = component.get("v.defaultTerritory"); Territory = event.getParam('value').toString(); component.set("v.defaultTerritory", Territory);

Thanks, Kumbresh


Instead of assigning the value using attribute, I am directly assigning using component.find("v.Name","TerritoryId") and it is working fine.

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.