function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian Nolau 2Brian Nolau 2 

Question about Assigning column values in a list to new Records in a custom Object

Apex Trigger Codeactual code:
trigger OpportunityTrigger on Opportunity (after update) {
    for(Opportunity opp : Trigger.new){
        if(opp.StageName == 'Closed/Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed/Won') {
        Invoice__c firstInvoice = new Invoice__c();
        firstInvoice.Amount__c =opp.Amount * 0.5;
        firstInvoice.Opportunity__c = opp.Id;
       
        Invoice__c secondInvoice = new Invoice__c();
        secondInvoice.Amount__c = opp.Amount * 0.5;
        secondInvoice.Opportunity__c = opp.Id;
           
        Invoice__c thirdInvoice = new Invoice__c();
        thirdInvoice.Amount__c = opp.Amount * 0.2;
        thirdInvoice.Opportunity__c = opp.Id;
           
            insert new List<Invoice__c>{firstInvoice, secondInvoice, thirdInvoice};
        }
    }
}


How do I now loop through the created List and create the new records in my custom Invoice Object with the values based on the Opportunity stage name changing to Closed/Won?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Brian,

Instead of having an insert statement inside the loop I would suggest you to change code to below one as it might lead to soql 101 error in case if it hits governer limits:
 
trigger OpportunityTrigger on Opportunity (after update) {

List<Invoice__c > toInsertList = new List<Invoice__c >();
List<Invoice__c> temporaryList = new List(Invoice__c)();
List<ChildObj__c> ChildList = new List(ChildObj__c)();


if(trigger.isafter && trigger.isupdate){
    for(Opportunity opp : Trigger.new){

        if(opp.StageName == 'Closed/Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed/Won') {
        Invoice__c firstInvoice = new Invoice__c();
        firstInvoice.Amount__c =opp.Amount * 0.5;
        firstInvoice.Opportunity__c = opp.Id;
        temporaryList .add(firstInvoice);
        
        Invoice__c secondInvoice = new Invoice__c();
        secondInvoice.Amount__c = opp.Amount * 0.5;
        secondInvoice.Opportunity__c = opp.Id;
        temporaryList .add(secondInvoice);
           
        Invoice__c thirdInvoice = new Invoice__c();
        thirdInvoice.Amount__c = opp.Amount * 0.2;
        thirdInvoice.Opportunity__c = opp.Id;
        temporaryList .add(thirdInvoice);
        
        for(Invoice__c I : temporaryList )
        {
        //create list of child object records and add them to the ChildList 
        }

        toInsertList.addall(temporaryList );      
        }
    }

if(toInsertList.size()>0)
{insert toInsertList;}

if(ChildList.size()>0)
{insert ChildList;}
}
}

EDIT: made changes to the above code and please note this is a sample code and you need to modify it as per your use case and in case if this is not the one can you elaborate on what the requirement is with an example so as to check further and respond back?

Looking forward to your response.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Brian Nolau 2Brian Nolau 2
Nevermind, i found it out. I had to replace the text to be the below:


trigger OpportunityTrigger on Opportunity (after update) {
    List<Invoice__c> newInvoices = new
    List<Invoice__c>();    
    for(Opportunity opp : Trigger.new){
        if(opp.StageName == 'Closed Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed/Won') {
        Invoice__c firstInvoice = new Invoice__c();
        firstInvoice.Amount__c = opp.Amount * 0.5;
        firstInvoice.Opportunity__c = opp.Id;
        newInvoices.add(FirstInvoice);
            
        Invoice__c secondInvoice = new Invoice__c();
        secondInvoice.Amount__c = opp.Amount * 0.5;
        secondInvoice.Opportunity__c = opp.Id;
        newInvoices.add(SecondInvoice);
            
        Invoice__c thirdInvoice = new Invoice__c();
        thirdInvoice.Amount__c = opp.Amount * 0.2;
        thirdInvoice.Opportunity__c = opp.Id;
        newInvoices.add(ThirdInvoice); 
            
        }
        }
    insert newInvoices;
}

Obviously, if it equals Closed Won and the prior value did not equal 'whatever', then i want it to create the three invoice records