1

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 Attempt1 on OpportunityLineItem (after insert) {
    List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); 
for(OpportunityLineItem item: [
   SELECT OpportunityId, Name 
   FROM OpportunityLineItem
   WHERE Id = :Trigger.new AND Name = 'Car_Filter_Plus']) {
        OpportunityLineItems.add(new OpportunityLineItem(
            OpportunityId = item.OpportunityId, 
            PricebookEntryId ='01uA000000QdelEIAD',
            Quantity = 1,
            UnitPrice = 25)
        );
}
}

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...

1 Answer 1

1

You didn't specify OpportunityId as an attribute of line item during creation.

That's why it is not being created for any opportunities.

Here is an approach, assigning Opportunity from newOpportunityLineItems.OpportunityId:

trigger AUTOADDPRODUCT on OpportunityLineItem (after insert) {
    List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); 
    for (OpportunityLineItem newOpportunityLineItems: Trigger.New) { 

            OpportunityLineItems.add(new OpportunityLineItem(
                                        OpportunityId = newOpportunityLineItems.OpportunityId, 
                                        PricebookEntryId ='ID CODE FOR CarOILplus',
                                        Quantity = 1,
                                        UnitPrice = 25)
                                    );

    } 
    if(!OpportunityLineItems.isEmpty()){
        insert OpportunityLineItems; 
    }
}
10
  • How do I add that to the code?
    – user48169
    Commented Aug 13, 2017 at 12:18
  • I gave it a try but the new product is still not adding to the opportunity.
    – user48169
    Commented Aug 13, 2017 at 19:08
  • I tried again under trigger (Opportunity Product) and here is the error that I got once I tried to add the product to the oppty: "Apex trigger Attempt1 caused an unexpected exception, contact your administrator: Attempt1: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Attempt1: line 4, column 1 "
    – user48169
    Commented Aug 14, 2017 at 13:46
  • 'trigger Attempt1 on OpportunityLineItem (after insert) { List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); for (OpportunityLineItem newOpportunityLineItems: Trigger.New) { if(newOpportunityLineItems.name.equals('Car_FILTER')){ OpportunityLineItems.add(new OpportunityLineItem( OpportunityId = '006g000000R2o1U', PricebookEntryId ='05uA000000UPRlcOAF', Quantity = 1, UnitPrice = 25)'
    – user48169
    Commented Aug 14, 2017 at 13:50
  • I updated the code in the post. still not adding the 2nd product to the opportunity automatically.... :'( I don't get it...
    – user48169
    Commented Aug 14, 2017 at 14:51

You must log in to answer this question.