Skip to main content Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.
I need to update some custom fields on the opportunity when certin products are selected. I just need the starting point to update the opportunity I believe that everything else is correct. 

If Pricebook2.Product2.Sales_Order_Group__c = 'Hotline Services' OR 'Policy Management Services' OR 'Incident Management Services' I need to update Implementation_Team__c to True 

Else if Pricebook2.Product2.Sales_Order_Group__c = 'ReadyTraining Services' I need to Update Training_Team__c to True

There are a couple more values but I am sure that I can figure that part out with a sample. Also I don't think the ELSE IF is right becuase it should be possible that both are selected. 

Any help would be greatly appricated. Below is what I have started but not sure if it is all correct. 

trigger Projectrollup on Opportunity (after insert, after update) {

    Map<PricebookEntry.Product2.Name,OpportunityLineItem> prod = new map<PricebookEntry.Product2.Name,OpportunityLineItem>();

        for(Opportunity o: trigger.new){

            if(o.HasOpportunityLineItem == true){

                string opptyId = o.id;

                OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, 

                                             TotalPrice,

                                             PricebookEntry.Product2.Name, Description, Converted_to_Asset__c,Asset__c,

                                             PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c

                                      From OpportunityLineItem 

                                      where OpportunityId = :opptyId

                                      And   PricebookEntry.Product2.Product_Reporting_Category__c = 'Services'];

 
3 respuestas
  1. 16 jun 2015, 6:43 p.m.
    First, don't write queries in a for loop.

    Second, you can save all the items needed to be updated in a list, then update them.

    Thirdly, if I understand correctly, you need to update Opportunity field based on the OpportunityLineItem field, right?

    Here is the code:

    trigger Projectrollup on Opportunity (after insert, after update) 

    {

      Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();

      List<Opportunity> updateList = new List<Opportunity>();

      for(Opportunity o : Trigger.new)

      {

        oppIds.put(o.id,o);

      }

      OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, TotalPrice,PricebookEntry.Product2.Name, Description, 

                                    Converted_to_Asset__c,Asset__c,PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c

                                   From OpportunityLineItem 

                                   where OpportunityId IN : oppIds.keySet() And PricebookEntry.Product2.Product_Reporting_Category__c = 'Services'];

      for(OpportunityLineItem o : OLI)

      {

        if(o.PricebookEntry.Product2.Sales_Order_Group__c = 'Hotline Services' || o.PricebookEntry.Product2.Sales_Order_Group__c = 'Policy Management Services' || o.PricebookEntry.Product2.Sales_Order_Group__c = 'Incident Management Services')

        {

          Opportunity opp = new Opportunity(id=o.OpportunityId);

          opp.Implementation_Team__c = true;

          updateList.add(opp);

        }

        if(o.PricebookEntry.Product2.Sales_Order_Group__c = 'ReadyTraining Services')

        {

          Opportunity opp = new Opportunity(id=o.OpportunityId);

          opp.Training_Team__c = true;

          updateList.add(opp);

        }

      }

      if(updateList.size()>0)

      {

        update updateList;

      }

    }

    ***:In the first for loop, MAYBE you need to add some line like if o.HasOpportunityLineItem== true, oppIds.put(o.id,o); depends on the real situation.   Let me know if you still have any questions ~^_^

          

Cargando
0/9000