1

Code:

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

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

    //create a list where all the products to be added.
    for(OpportunityLineItem optLineItem: Trigger.new)
    {
        if(optLineItem.Name == 'Product A')
        {
            lstProductNames.add(optLineItem.Name);
            lstProductNames.add('Product B');
        }
    }

    //retrieve the values based on Product list
    List<OpportunityLineItem> lstOpptyLineItems = [SELECT OpportunityId, Name , PricebookEntryId, Quantity, UnitPrice
                                                    FROM    OpportunityLineItem
                                                    WHERE   Id = :Trigger.new 
                                                    AND Name IN:lstProductNames];

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


    for(OpportunityLineItem item: lstOpptyLineItems)
    {
        //retrieve the values and compare the name
        if(item.name == 'Product B')
        {
            //retrieve Product A item from the map.         
            OpportunityLineItem itemProductA = mapOpptyLineItem.get('Product A');

            //now assign Product A items as required, you can retrieve the amount from Product A
            oliList.add(new OpportunityLineItem(
                OpportunityId = itemProductA.OpportunityId, 
                PricebookEntryId = itemProductA.PricebookEntryId,
                Quantity = 1,
                UnitPrice = 25)
              );
        }
    }
    insert oliList;
}

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. Also, the $ value of "Product B" needs to be 5% of "Product A". To add to this complex situation, we have two pricebooks. One is named "Standard Price Book" and the other is named "TD Pricebook". Both pricebooks have "Product A" and "Product B"...

Here is the data:

Standard Price Book (pricebook2id: 01sA00000004lbRIA1)

  • Product A (full price book entry id: 01uA000000QPDlTI1a)
  • Product B (full price book entry id: 01uA000000QPDldI1b)

TD Pricebook (pricebook2id: 01sA00000007eyNIA2)

  • Product A (full price book entry id: 01uA000000QPDlTI2a)

  • Product B (full price book entry id: 01uA000000QPDldI2b)

  • 1
    Please do not remove so much code from your post that it loses all context. – Adrian Larson Nov 21 '17 at 16:35
  • Please stop defacing your own posts. – Adrian Larson Nov 21 '17 at 16:35
  • @AdrianLarson I was trying to follow the guidelines and improve the questions. That's what I was told to do. – user48169 Nov 21 '17 at 16:40
  • 1
    Applying those guidelines moving forward is a good thing. And occasionally touching up your old posts is a good thing (though not all at once). Regardless, you are removing so much information that the question would not have even been answerable if it had been posed that way originally. – Adrian Larson Nov 21 '17 at 16:43
  • Well certainly focus on questions which have downvotes then. It is good to remove unnecessary information, but the question has to still make sense. – Adrian Larson Nov 21 '17 at 16:56
0

Here is the approach, before inserting Product B record, you need to find the PriceBookEntry related to Price Book. Also, taken an assumption that, UnitPrice of Product B will be 5% of UnitPrice of Product A. You may change based on your need.

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

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

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

    //retrieve the values based on Product list
    List<OpportunityLineItem> lstOpptyLineItems = [SELECT OpportunityId, Name , PricebookEntryId, Quantity, UnitPrice
                                                    FROM    OpportunityLineItem
                                                    WHERE   Id = :Trigger.new 
                                                    AND Name IN:lstProductNames];

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

    //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 Name = 'TD Pricebook') LIMIT 1];


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

    //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)
      );

    insert oliList;
}
| improve this answer | |

Your Answer

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