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
Ryno LourensRyno Lourens 

Trigger to update an Opportunity field when a new Opportunity Product is added or edited.

Hi everyone,

I've got two custom fields in the Opportunities Object (Custom_Opp_Field_1__c, Custom_Opp_Field_2__c) that need to be updated with information from a two fields in Opportunity Product Object (Custom_Prod_Field_1__c, Custom_Prod_Field_2__c) whenever a new Opportunity Product is added or edited.

I've never written a Trigger before, so any help is very much appreciated!

Thanks in advance!
Ryno Lourens

Shri RajShri Raj
trigger OpportunityProductTrigger on OpportunityLineItem (after insert, after update) {
    // Get the Opportunity Ids for the Opportunity Products that have been added or updated
    Set<Id> opportunityIds = new Set<Id>();
    for (OpportunityLineItem oli : {

    // Query the Opportunities to get the current values of the custom Opportunity fields
    Map<Id, Opportunity> opportunities = new Map<Id, Opportunity>([
        SELECT Id, Custom_Opp_Field_1__c, Custom_Opp_Field_2__c
        FROM Opportunity
        WHERE Id IN :opportunityIds

    // Query the Opportunity Products to get the values of the custom Opportunity Product fields
    Map<Id, OpportunityLineItem> opportunityProducts = new Map<Id, OpportunityLineItem>([
        SELECT OpportunityId, Custom_Prod_Field_1__c, Custom_Prod_Field_2__c
        FROM OpportunityLineItem
        WHERE OpportunityId IN :opportunityIds

    // Loop through the Opportunities and update the custom Opportunity fields
    for (Id opportunityId : opportunityIds) {
        Opportunity opportunity = opportunities.get(opportunityId);
        for (OpportunityLineItem oli : opportunityProducts.values()) {
            if (oli.OpportunityId == opportunityId) {
                opportunity.Custom_Opp_Field_1__c = oli.Custom_Prod_Field_1__c;
                opportunity.Custom_Opp_Field_2__c = oli.Custom_Prod_Field_2__c;

    // Update the Opportunities
    update opportunities.values();

Litami naraLitami nara
I offer this website for retiree paystubs to all Americans. Jack In the Box Pay Stubs Login How to Access Jack In the Box Pay Stubs & Jack In the Box W2s Form Online
Amidou CisseAmidou Cisse
Here's an example of a trigger that can help you accomplish this:

Copy code : 

trigger UpdateOpportunityFields on OpportunityLineItem (after insert, after update) {
    // Map to store OpportunityIds and the sum of Custom_Prod_Field_1__c and Custom_Prod_Field_2__c for each Opportunity
    Map<Id, OpportunityFields> oppFieldsMap = new Map<Id, OpportunityFields>();

    // Iterate through the OpportunityLineItems and store the sum of Custom_Prod_Field_1__c and Custom_Prod_Field_2__c for each Opportunity
    for (OpportunityLineItem oppLineItem : {
        // Check if the OpportunityId is not already in the map
        if (!oppFieldsMap.containsKey(oppLineItem.OpportunityId)) {
            // Create a new OpportunityFields object and add it to the map
            OpportunityFields oppFields = new OpportunityFields();
            oppFields.OpportunityId = oppLineItem.OpportunityId;
            oppFields.Custom_Opp_Field_1__c = oppLineItem.Custom_Prod_Field_1__c;
            oppFields.Custom_Opp_Field_2__c = oppLineItem.Custom_Prod_Field_2__c;
            oppFieldsMap.put(oppLineItem.OpportunityId, oppFields);
        } else {
            // If the OpportunityId is already in the map, update the sum of Custom_Prod_Field_1__c and Custom_Prod_Field_2__c
            OpportunityFields oppFields = oppFieldsMap.get(oppLineItem.OpportunityId);
            oppFields.Custom_Opp_Field_1__c += oppLineItem.Custom_Prod_Field_1__c;
            oppFields.Custom_Opp_Field_2__c += oppLineItem.Custom_Prod_Field_2__c;

    // Create a list to store the updated Opportunities
    List<Opportunity> oppsToUpdate = new List<Opportunity>();

    // Iterate through the map and update the Opportunity fields
    for (OpportunityFields oppFields : oppFieldsMap.values()) {
        Opportunity opp = new Opportunity();
        opp.Id = oppFields.OpportunityId;
        opp.Custom_Opp_Field_1__c = oppFields.Custom_Opp_Field_1__c;
        opp.Custom_Opp_Field_2__c = oppFields.Custom_Opp_Field_2__c;

    // Update the Opportunities
    if (!oppsToUpdate.isEmpty()) {
        update oppsToUpdate;

// OpportunityFields class to store the OpportunityId and the sum of Custom_Prod_Field_1__c and Custom_Prod_Field_2__c for each Opportunity
public class OpportunityFields {
    public Id OpportunityId { get; set; }
    public decimal Custom_Opp_Field_1__c { get; set; }
    public decimal Custom_Opp_Field_2__c { get; set; }
This trigger listens to the `