You need to sign in to do that
Don't have an account?
Frank van Meegen 13
Insert list via APEX with lookup field populated
Hi all,
I have been struggling to insert a list of records in Salesforce via APEX with populating a lookup field. Each item in the list will have a different parent. How can I map the Id of the specific parent to each child item without doing a SOQL for each child item?
I have a retrieved a list of values from a JSON string called strResponse and parse it via a JSON2APEX class. This list will have 2 items Code and Value.
The code value is also available as an external Id on the parent object ParentObject__c.Code__c that is related via ChildObject__c .ParentObject__c
If I insert the following code I get an error that the lookup field does not contain a valid Id. How can I populate the ParentObject__c field with an Id of the matching parent record?
I have been struggling to insert a list of records in Salesforce via APEX with populating a lookup field. Each item in the list will have a different parent. How can I map the Id of the specific parent to each child item without doing a SOQL for each child item?
I have a retrieved a list of values from a JSON string called strResponse and parse it via a JSON2APEX class. This list will have 2 items Code and Value.
The code value is also available as an external Id on the parent object ParentObject__c.Code__c that is related via ChildObject__c .ParentObject__c
If I insert the following code I get an error that the lookup field does not contain a valid Id. How can I populate the ParentObject__c field with an Id of the matching parent record?
ChildObject__c [] Childs = new List<ChildObject__c>(); String strResponse = httpResponse.getBody(); List<JSON2Apex> delegationMap = (List<JSON2Apex>)JSON.deserialize(strResponse, List<JSON2Apex>.class); for(JSON2Apex cp : delegationMap) { ChildObject__c Child = new ChildObject__c (ParentObject__c = cp.Code, ValueField__c = cp.Value); Childs.add(Child); } insert Childs;
Thanks
Just checking but the ParentObject__c =cp.Code, PatentObject__c here obviously is a lookup field right? and by looking at the error, does the value in that Code a standard salesforce ID string (15/18 digit alphanumeric etc.)?
Id, Name, Code__c
9060G0000005QK3,'XYZ','XYZ'
9060G0000005QK4,'ABC','ABC'
9060G0000005QK5,'DEF','DEF'
The ChildObject__c records I whant to insert are like:
ParentObject__c, ValueField__c
'ABC', '123'
'XYZ', '456'
'DEF','987'
The error I get is valid since I am trying to insert a string e.g. 'ABC' in a lookup field instead of a Salesforce Id. My goal would be to combine the 2 lists so I have the Salesforce Id of the ParentObject__c in the list of ChildObject__c records I whant to insert:
ParentObject__c, ValueField__c
'9060G0000005QK4', '123'
'9060G0000005QK3', '456'
'9060G0000005QK5','987'
I do not know how I can achieve this without querying each childobject in a loop, but this could cause governor limit issues. Any ideas how I can map these 2 lists together?