function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Travis WrightTravis Wright 

Update Opportunity Fields when Service products are selected

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'];
 
Best Answer chosen by Travis Wright
doravmondoravmon
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 ~^_^
      

All Answers

doravmondoravmon
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 ~^_^
      
This was selected as the best answer
Travis WrightTravis Wright
Sorry I believe that I figured it out but wanted to check. I am getting the following error "OR operator can only be applied to Boolean expressions" So I have made the following change and everything seems to be fine can you confirm that this is correct. 

Old Line
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')

New Line
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')

 
doravmondoravmon
Yup ~this is correct !~^_^