0

I have a function that have 2 List of Objects in parameters (Schedule__c recList and scheduleTable__c recList2). Once I insert the recList, I am looping through the ID and insert child records to it. To do so, I am doing two loops. Here it is.

@AuraEnabled
public static void saveRecord(List<Schedule__c> recList, List<scheduleTable__c> recList2){
        
    insert recList;

    for(Schedule__c a : recList){
        String newId = a.Id;
        for(scheduleTable__c s : recList2){
            s.Schedule__c.add(newId);
            insert s;
        }
    }
}

I have an error saying Method does not exist or incorrect signature: void add(String) from the type Id. Not too sure what that means... Any idea how I can insert the child record under the Parent record? Thanks

1 Answer 1

0

You're calling add on a String/Id, which isn't a valid method (that's what the error means). You probably want to assign each Schedule__c to the related scheduleTable__c, so you need one loop, not two:

insert recList;
for(Integer i = 0, s = recList.size(); i < s; i++) {
    Schedule__c sc = recList[i];
    scheduleTable__c st = recList2[i];
    st.Schedule = sc.Id;
}
insert recList2;

Remember to perform your DML operations outside of a loop, or you'll quickly run up against governor limits.

2
  • Yes, I changed a bit your code because I had the duplicate variable error so it is now like that: insert recList; for(Integer i = 0, s = recList.size(); i < s; i++) { Schedule__c sched = recList[i]; scheduleTable__c st = recList2[i]; st.Schedule__c = sched.Id; } insert recList2; thank you! much appreciated
    – Eric
    Mar 23, 2021 at 2:37
  • @Eric Indeed. I don't normally use such short variable names, lol. Glad I could help!
    – sfdcfox
    Mar 23, 2021 at 2:41

You must log in to answer this question.

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