+ Start a Discussion
Santosh SSantosh S 

Trigger for creating new Invoice based on completed Deliverables of Opportunity

need Help for creating a Trigger, which can create new Invoice based on completed Deliverables (Custom Object) of Opportunity (Standard)

 

1. Deliverable (Detail object with a Master-Detail relationship with Opportunity object)
  • Deliverable Name (Name field)
  • Opportunity 
  • Estimate (Currency (16,2))
  • Implementation Status (Picklist - Open, Work in Progress, Staged, Completed)
  • Invoice (Lookup - to Invoice object)
2. Invoice (Detail object with a Master-Detail relationship with Opportunity object)
  • Opportunity
  • Invoice Amount (Currency(16,2)) - This should be automatically populated based on the Completed deliverables  Read Only
Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Santosh,

 

Try below code :-

 

Trigger insertInvoice on Deliverable__c(before insert,before update){


List<Invoice__c> invoiceList = new List<Invoice__c>();

for(Deliverable__c dc:trigger.new){
if(dc.Implementation_Status__c=='Completed'){

Invoice__c in = new Invoice__c(Name='Test Invoice',Opportunity__c=dc.Opportunity__c,Invoice_Amount__c=dc.Estimate__c ); //Assuming Invoice Amount to be mapped with Estimate
invoiceList.add(in);
}
}

insert invoiceList;


}

All Answers

GulatiGulati
Hi Santosh,

There can be more than one record of Deliverable under one opportunity, so do you want to check 'Implementation Status' on all records?
Santosh SSantosh S
Implementation status to be checked only with regards to the Particular Opportunity
GulatiGulati
Santosh, But there can be more than 1 Deliverable records related to that particular opportunity which may have different value of 'Implementation Status' field. So do you need to ensure that all records MUST have value as COMPLETED. Right?
Vinit_KumarVinit_Kumar

Santosh,

 

Try below code :-

 

Trigger insertInvoice on Deliverable__c(before insert,before update){


List<Invoice__c> invoiceList = new List<Invoice__c>();

for(Deliverable__c dc:trigger.new){
if(dc.Implementation_Status__c=='Completed'){

Invoice__c in = new Invoice__c(Name='Test Invoice',Opportunity__c=dc.Opportunity__c,Invoice_Amount__c=dc.Estimate__c ); //Assuming Invoice Amount to be mapped with Estimate
invoiceList.add(in);
}
}

insert invoiceList;


}

This was selected as the best answer