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
sreekanth reddysreekanth reddy 

Update Opportunity Field with Product custom Fields

Hi
How to update opportunity field with product custom field (using trigger).

   
Thanks 
Srikanth.
Phillip SouthernPhillip Southern
Srikanth, you'll need to determine where the update is occuring that you want to monitor (insert/update or opportunity, opportunity line item, or product) and then based on that update perform SOQL to pull the data you need, manipulate it, and write an update back.

Right now the question is so high level its going to be hard to give you direct feedback, but instead just general practice.  
Prad NethasPrad Nethas
Hey Srikanth,

Below code may help you in achieving your requirement.

trigger updateopp on Opportunity (after insert, after update) 
{
  Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
  List<Opportunity> updateList = new List<Opportunity>();

  for(Opportunity op : Trigger.new)
  {
    oppIds.put(op.id,op);
  }

  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 opli : OLI)
  {
    if(opli.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;
  }
}

As philip said you have to know where update is occuring.

Thanks
Prad
 
Prad NethasPrad Nethas
Hey Srikanth,

Please let me know if you are still persisting this issue. let me know if any issues. 

If your problem solved give kudos to the post.

Regards
Prad