Skip to main content
1 of 2
user avatar
user avatar

Trigger issue: Opportunity product is not being added automatically to the opportunity

I have been trying to create this trigger and its not working. I'm not getting any errors, but an opportunity product is not being added to the opportunity automatically. I'm not sure why.

Here is the code:

trigger AUTOADDPRODUCT on OpportunityLineItem (after insert) {
    List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); 
    for (OpportunityLineItem newOpportunityLineItems: Trigger.New) { 
        if(newOpportunityLineItems.Name == 'CarOIL'){
            OpportunityLineItems.add(new OpportunityLineItem(PricebookEntryId ='ID CODE FOR CarOILplus',Quantity = 1,UnitPrice = 25));
        }
    } 
    if(!OpportunityLineItems.isEmpty()){
        insert OpportunityLineItems; 
    }
}

Ideally, here is what I need the code to do:

We have a lot of additional cost products that need to be be added to the opportunity whenever a product is added to the opportunity by the proposal team. The $ value of the additional cost needs to be a % of the product added by the proposal writer. I came across a couple codes online but have not been able to get very far. Here is one that I came across:

Proposal writer adds the following products

  • CarOIL
  • CarFILTER
  • CarTIRE'
  • CarLIGHTS

The $ value is manually added by the writer in SFDC. They put the quantity and sale price and it auto calculates

Here is what I need the code to automatically add to the opportunity

  • CarOILplus
  • CarFILTERplus
  • CarTIREplus
  • CarLIGHTSplus

The $ value of each plus product is a % of the original product, which has a dollar value entered by the writer. For example:

CarOILplus - is 5% of the $ value entered by the write when added CarOIL CarFILTERplus - is 2% of the $ value entered by the write when added CarFILTER CarTIREplus - is 3% of the $ value entered by the write when added CarTIRE CarLIGHTplus - is 1% of the $ value entered by the write when added CarLIGHT Can anyone help me? Please...

user48169