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
Jan VandeveldeJan Vandevelde 

Update an opportunity field when a product that meets specific criteria is added or removed

Hi all,

to start off, I'm not a coder but just a Salesforce Admin. And I think I'll need to do what I want using a trigger. So if anybody could help me achieve this I would greatly appreciate it.

On and opportunity I've got a custom checkboxfield called Guarantee__c (true or false), I would like that this checkbox is checked whenever I add a product to the opportunity that has in a custom productfield Product_Code__c the word "GUAR". offcourse the checkbox should also be unchecked whenever I remove all products containing the word GUAR.

Just to be clear:

custom field on opportunity: Guarantee__c (checkbox)
custom field on product: Product_Code__c
some products have in the product code the word GUAR  exemple: GUAR-5, GUAR-3, GUAR-7

Whenever adding one or more products to the opportunity which have the word GUAR in their Product_Code__c I want the checkbox on the opportunity Guarantee__c set to TRUE and if the opportunity is not containing any of those products it should be false.

Can anybody help me with how I can get this done?

Thanks in advance
Dev AngelDev Angel
This is a bit tricky as every time you add an opportunity line item, we need to scan all the line items for any affected opportunities to determine true or false.  This is doable, but if you ever import a lot of line items you could get tangled up. The other issue is that if you edit a product to remove the GUAR bit, then you will need to scan all open opportunities and their line items to correct for the change.

Both can be done, sounds like two triggers. I've got the first trigger partially written and will share here. Not sure if I will have time to finish, but maybe some other nice person might.

trigger UpdateOpptyProd on OpportunityLineItem (after insert, after update, after delete) {
   
    // Get all the other products on the oppty
    List<OpportunityLineItem> lineItems;
    if (trigger.IsDelete) {
        lineItems = Trigger.Old;
    } else if (trigger.IsInsert || trigger.IsUpdate) {
        lineItems = Trigger.New;
    }
   
    Map<String, OpportunityLineItem> opptyLookup = new Map<String, OpportunityLineItem>();
    for (OpportunityLineItem li : lineItems) {
        opptyLookup.put(li.OpportunityId, li);
    }
   
    List<Opportunity> oppty = [Select Opportunity.Id From Opportunity Where Id in
   (Select OpportunityId From OpportunityLineItem where OpportunityId in :opptyLookup.keySet() And PriceBookEntry.Product2.Product_Code__c Like 'GUAR%')];
    if (oppty.size() == 0) {
        return;
    } else {
        for (Opportunity o : oppty) {
           // Note, just setting a text value not a checkbox.
            o.Guarantee__c = '1';
        }
        update oppty;
    }

}