0

I have a trigger which will update the field on update. But how to update only when specific field changes ?

I tried below code but it is still running when any update is made on record.

Note : in the below code I want to update listCB because if I use other list it is throwing some other system exception.

Trigger :

trigger updateTriger on Opportunity(after update) {

    List<Custom_object__c> listCB = [
        Select Id,
         Name,
         Checkbox__c,
         LookupOpp__c
        IN: trigger.newMap.keySet()];

    if(!listCB.isEmpty()) {
        for(Opportunity opp : trigger.new) {
            if(trigger.newMap.get(opp.Id).LookupCB__c != trigger.oldMap.get(opp.Id).LookupCB__c ) {

                for(Custom_object__c cb :listCB) {
                    if(cb.LookupOpp__r.LookupCB__c  != null) {
                        cb.Checkbox__c = true;
                    }
                }           
            }
        }
    }
    update listCB;
    }

3 Answers 3

0

It is updating anyway, because the line

update  listCB;

The code should be something like below:

trigger updateTriger on Opportunity(after update) {

list<Id> l_recordIdChanged = new list<Id>();
list<Opportunity> l_opp2Update = new list<Opportunity>();

for(Opportunity opp : trigger.new) {
    if(opp.LookupCB__c != trigger.oldMap.get(opp.Id).LookupCB__c ) {
        l_recordIdChanged.add(opp.id);
    }
}

for(Opportunity lookupopp : [Select Id,Name,Checkbox__c,LookupOpp__c from Opportunity where LookupOpp__c IN :l_recordIdChanged]){

    lookupopp.checkbox__c= true;
    l_opp2Update.add(lookupopp);
}


if(! l_opp2Update.isEmpty(){
    update l_opp2Update;
}
}
1

You can reference the Trigger.oldMap and Trigger.newMap map collections. So, you can compare old values with new values. I have made some changes to your code and to make comparison checks on new/old map values for the specific field LookupCB__c.

trigger updateTriger on Opportunity(after update) {

    List<Custom_object__c> listCB = [
        Select Id,
         Name,
         Checkbox__c,
         LookupOpp__c
        IN: trigger.newMap.keySet()];

    List<Custom_object__c> toUpdate = new List<Custom_object__c>();
    if(!listCB.isEmpty()) {
        for (Opportunity opp : Trigger.new) {
            Opportunity oldOpportunity = Trigger.oldMap.get(opp.ID);
            if (opp.LookupCB__c != oldOpportunity.LookupCB__c) {
                for(Custom_object__c cb :listCB) {
                    if(cb.LookupOpp__r.LookupCB__c  != null) {
                        cb.Checkbox__c = true;
                        toUpdate.add(cb);
                    }
                }
            }
        }
    }
    update toUpdate;
}
2
  • Thanks for your response.. but it is updating list even though the field is not changed update listCB. It is just updating without any edits though
    – Learner
    Mar 18, 2017 at 17:12
  • @Learner I forgot to use a second List to store the Custom_object__c that need updating. Please see the changes made above.
    – TSmith
    Mar 19, 2017 at 13:39
0

I will take this approach:

  1. Store only those opportunity Ids where LookupCB__c got changed.
  2. Retrieve the filtered list of Custom_object__c
  3. Finally update the listCB.

Trigger code

trigger updateTrigger on Opportunity(after update) {
    Set<Id> opptIds = new Set<Id>(); //store only changed Opportunity Ids
    if(Trigger.isUpdate && Trigger.isAfter) {
        for(Opportunity opp : trigger.new){
            //verify if LookupCB__c is modified and not null
            if(trigger.newMap.get(opp.Id).LookupCB__c !=null 
                && (trigger.newMap.get(opp.Id).LookupCB__c 
                != trigger.oldMap.get(opp.Id).LookupCB__c)) {
                opptIds.add(opp.Id);
            }           
        }       
    }

    if(!opptIds.isEmpty()){

        //retrieve filtered list of Custom_object__c records.
        List<Custom_object__c> listCB = [   SELECT Checkbox__c 
                                            FROM Custom_object__c
                                            WHERE LookupOpp__c IN: opptIds];
        for(Custom_object__c cb:listCB){
            cb.Checkbox__c = true;
        }
        update listCB;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .