0

I'm creating a trigger on the user object which is supposed to do a certain action if a checkbox is checked.

trigger Penalizacion_Memo on User (before update) {
for (user us : trigger.new){
    if (us.MEMO__c==TRUE){

        Decimal Cambio_Porcentual   = -0.2;
        Penalizacion_Porcentual_Logica.Creacion_Tareas_Porcentual(Cambio_Porcentual, us.Id);

    }    
  }
}

My question is if another update is made to the user record and the MEMO checkbox is still checked would that still fire the tigger? I think yes and if so what can I do for it to trigger only when the specific change is made?

0

You could use the oldMap, but that won't stop it the 2nd time it is unselected, You would need to figure out some different logic if you want that trigger to never fire once Memo is selected

for(ID u : trigger.newMap.keySet())
{
    if(trigger.newMap.get(u).Memo__c && trigger.newMap.get(u).Memo__c != trigger.oldMap.get(u).Memo__c )
    {

    }
}
  • you just need to do oldMap call: if( u.Memo__c && u.Memo__c != trigger.oldMap.get(u).Memo__c ), and also trigger.oldMap.get(u).Memo__c would have the same value if record is new... – o-lexi Apr 24 '17 at 19:51
  • Huh? That's what I wrote, just using the map.. Also brand new records old map is null on insert and update – EricSSH Apr 24 '17 at 19:58
  • Perfect! Thats exactly what I needed to know – Victor Echeverría Apr 24 '17 at 20:02
0

Please take a look at

Trigger.old
Trigger.oldMap

You can compare record before update (old) and after (new).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.