0

The goal of the trigger is to automatically add "Product B" to the opportunity once "Product A" has been added manually by the Technical Writer. The code works really well, but I need Product B to update if I go into the opportunity and update the values of Product A. I thought it would, but it doesn't. Nothing happens to Product B if I update the values of Product A. I tried a few things but nothing work. Can anyone help me?

Please see go to the link below to see what the code does. It will help you understand better: Error: de-reference a null object

trigger Attempt1 on OpportunityLineItem (after insert) 
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); 

    List<String> lstProductCodes = new List<String>();

    for(OpportunityLineItem optLineItem: Trigger.new)
    {
        if(optLineItem.ProductCode == 'Product A')
        {
            lstProductCodes.add(optLineItem.ProductCode);
        }
    }

    if(lstProductCodes.size()>0)
    {
        System.debug('lstProductCodes=' + lstProductCodes);

        //retrieve the values based on Product list
        List<OpportunityLineItem> lstOpptyLineItems = [SELECT OpportunityId, Opportunity.Pricebook2Id, Name, ProductCode , PricebookEntryId, Quantity, UnitPrice
                                                        FROM    OpportunityLineItem
                                                        WHERE ProductCode IN:lstProductCodes];

        //create a map which contains Product Name and OpportunityLineItem
        Map<String, OpportunityLineItem> mapOpptyLineItem = new Map<String, OpportunityLineItem>();
        for(OpportunityLineItem item:lstOpptyLineItems)
        {
            mapOpptyLineItem.put(item.ProductCode, item);
        }


        Id pbkId = lstOpptyLineItems[0].Opportunity.Pricebook2Id;

        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name 
                                        FROM PricebookEntry 
                                        WHERE Name ='Product B' 
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id =:pbkId) LIMIT 1];


        //retrieve Product A item from the map.         
        OpportunityLineItem itemProductA = mapOpptyLineItem.get('Product A');
        System.debug('itemProductA= ' + itemProductA);

        if(itemProductA != null)
        {
            //now assign Product A items as required, you can retrieve the amount from Product A
            oliList.add(new OpportunityLineItem(
                OpportunityId = itemProductA.OpportunityId, 
                PricebookEntryId = pbeProduct2.Id,
                Quantity = 1,
                UnitPrice = itemProductA.UnitPrice * 0.05)
              );
            System.debug('oliList=' + oliList);
            insert oliList;
        }   
    }
}
0

You might have to refactor your code, but here is the approach.

Along with after insert, you need to have after update trigger. Rest of the logic given in the code.

            else if (Trigger.isUpdate)
            {
                //if you need to update PriceBookEntry of Product B
                pbeProduct2.UnitPrice = itemProductA.UnitPrice * 0.05;
                update pbeProduct2;

                //if you need to update OpportunityLineItem of Product B
                OpportunityLineItem optLIProductB = [SELECT OpportunityId, Opportunity.Pricebook2Id, 
                                                        Name, ProductCode , PricebookEntryId, 
                                                        Quantity, UnitPrice
                                                        FROM    OpportunityLineItem
                                                        WHERE ProductCode WHERE ProductCode = 'Product B'];

                optLIProductB.UnitPrice = mapOpptyLineItem.get('Product A').UnitPrice;

                update optLIProductB;
            }
| improve this answer | |
  • you can refactor the code, I mentioned, if you face any issues after your change then post a separate question – Santanu Boral Sep 8 '17 at 17:46
  • that could be possible, you can update your code and post where exactly you are facing issues – Santanu Boral Sep 8 '17 at 22:17
  • you can differentiate based on createDate of the record, you could run SOQL with ORDER BY Product Code and CreatedDate – Santanu Boral Sep 9 '17 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy