You need to sign in to do that
Don't have an account?
Vishal Saw
OpportunityLineItem On Opportunity
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
How to create this Opportunity line item?
How to create this Opportunity line item?
Trigger OpportunityTrigger on Opportunity(after Insert){
if(trigger.isAfter){
if(trigger.isInsert){
InsertOpportunityLineItem.updateOpportunityWithLineItem(trigger.new);
}
}
}
public Class InsertOpportunityLineItem{
public static void updateOpportunityWithLineItem(List<Opportunity> oppList){
Product2 prod = [SELECT Id, Name FROM Product2 LIMIT 1];
PriceBookEntry pbe = [SELECT Id FROM PriceBookEntry LIMIT 1];
List<OpportunityLineItem> oppItemList = new <OpportunityLineItem>();
for(Opportunity opp : oppList){
OpportunityLineItem oppItem = new OpportunityLineItem();
oppItem.OpportunityId = opp.Id;
oppItem.PricebookEntryId = pbe.Id;
oppItem.UnitPrice = 10;
oppItem.Product2Id = prod.Id;
oppItem.Quantity = 1;
oppItemList.add(oppItem);
}
insert oppItemList;
}
}
If you find this answer helpful, Please mark it as the best answer.