You need to sign in to do that
Don't have an account?

Trigger to create an Opportunity Product when an Opportunity is Closed/Won
Hi all,
I was wondering if its possible for a trigger to automatically create an opportunity product when an opportunity is closed won.
I have an Object related to the opportunity that has fields called Product_Name__c and Total_Price__c.
I would like the trigger to populate these fields into the corresponding fields on the Opportunity Products object.
Thanks
I was wondering if its possible for a trigger to automatically create an opportunity product when an opportunity is closed won.
I have an Object related to the opportunity that has fields called Product_Name__c and Total_Price__c.
I would like the trigger to populate these fields into the corresponding fields on the Opportunity Products object.
Thanks
Yes you can write a trigger on Opportunity and check if the opportunity is get Closed Won the you can do other action.
-Thanks
Ashlekh Gera
trigger OpportunityProduct on Opportunity (After update) {
List<Opportunity> opp = new List<Opportunity>();
Pricebook2 standardBook = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
List<OpportunityLineItem> lines = new List<OpportunityLineItem>();
PricebookEntry entry = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardBook.Id AND Product2.ProductCode = 'DEFAULT'];
for(Opportunity o:Trigger.New){
if(o.StageName == 'Closed/Won'){
lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=record.Id, UnitPrice=entry.UnitPrice, Quantity=1));
}
insert lines;
}
}
By using trigger you can do it. But try process builder (Create New Record) option. Without code you can achive this.
I am getting the error message:
Error:Apex trigger OpportunityProduct caused an unexpected exception, contact your administrator: OpportunityProduct: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.OpportunityProduct: line 6, column 1
Thanks