You need to sign in to do that
Don't have an account?
Brian Nolau 2
Question about Assigning column values in a list to new Records in a custom Object
actual 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?
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?
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:
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.
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