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;
}
Best Answer
It is updating anyway, because the line
The code should be something like below: