+ Start a Discussion
Frank van Meegen 13Frank 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?
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);                 
                insert Childs;

Kaustubh LabheKaustubh Labhe
Mind if you show me the JSON response and the wrapper class please? 

Frank van Meegen 13Frank van Meegen 13
public class JSON2Apex {
    public String Code, Value;
    public static List<JSON2Apex> parse(String json) {
        return (List<JSON2Apex>)System.JSON.deserialize(json, List<JSON2Apex>.class);

Kaustubh LabheKaustubh Labhe

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.)?                                        
Tuan LuTuan Lu
You might want to print out the value of cp.Code. Its probably not set or unserialized incorrectly.
Frank van Meegen 13Frank van Meegen 13
The ParentObject__c  looks like the following:
Id, Name, Code__c

The ChildObject__c records I whant to insert are like:

ParentObject__c, ValueField__c 
'ABC', '123'
'XYZ', '456'

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'

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?
Tuan LuTuan Lu
You can query all your parent objects in one query like select Id, Name, Code__c where Name in :childParentCodes. Loop through results and put in map by Name. Then loop through all child objects and lookup Name in map and set Id.