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
Andrew Hoban 6Andrew Hoban 6 

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
AnupPrakash_AlgoworksAnupPrakash_Algoworks
This is comepletely feasible. You could write the Trigger on Opportunity.
AshlekhAshlekh
Hi,

Yes you can write a trigger on Opportunity and check if the opportunity is get Closed Won the you can do other action.
 
Trigger DoSomeActionTrigger on Opportunity (After update)
{

// Here you can write a trigger code

}

-Thanks
Ashlekh Gera
BM 5BM 5
Below is the code for creating an opportunity product when an opportunity is closed/Won

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;
    }
    
}
~Onkar~Onkar
Hi Andrew,

By using trigger you can do it. But try process builder (Create New Record) option. Without code you can achive this. User-added image
Andrew Hoban 6Andrew Hoban 6
Hi BM 5,

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
Andrew Hoban 6Andrew Hoban 6
The currect trigger I have an be seen below. I am now trying to add a poduct when the Opportunity is first created. This still doesnt seem to be adding in any products however.
 
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.isclosed == false){
            
            lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=o.Id, UnitPrice=entry.UnitPrice, Quantity=1));
           
        }
        
        insert lines;
    }
    
}

 
mitsvikmitsvik
where do you get the Pricebook2 standardBook ?