You need to sign in to do that
Don't have an account?
ruchika Nayyar
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
trigger opportunitytrigger1 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;
}
error-invalid field for product sobject
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;
}
error-invalid field for product sobject
Instead you also need to associate to the correct pricebook, as that in your opportunity.
The code would look something like below. Let me know if this helps.
Why we used required product