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
ruchika Nayyarruchika Nayyar 

trigger code for this?

Write a trigger on Opportunity, when an Opportunity will be insert an Opportunity Line Item should be insert by default with any of the Product associated with Opportunity
FearNoneFearNone
Show us first what you have done so far. Then we will help you from there.
GauravGargGauravGarg

Hi Ruchika,

Please add trigger like below:

trigger opportunityTrigger on Opportunity (After Insert){
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();  
for(Opportunity Op: Trigger.New){
       if(op.product != null ){
        OpportunityLineItem oli =  new OpportunityLineItem();
        oli.Product = op.product;
        oli.opportunity = op.id
oliList.add(oli);
}
}
if(oliList != null && oliList.size() > 0)
 insert oliList;
}
 

Add / Remove field as per your requirements.
Let me know if you still found issues.

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990

ruchika Nayyarruchika Nayyar
Error is coming invalid field product for opportunity line item .
From where we get opportunity line item in opportunity field?? Please help me out
 
GauravGargGauravGarg
Hi Ruchika,

As per salesfroce standard Opportunity Data structure, Product field is not available on Opportunity. Are you trying to add this field addionationally. If yes. please replace this line:

oli.Product = op.product;

with below line:

oli.Product2Id = op.product_id__c;

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990