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?

2 Answers 2

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 )
    {

    }
}
3
  • 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
    Commented Apr 24, 2017 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
    Commented Apr 24, 2017 at 19:58
  • Perfect! Thats exactly what I needed to know Commented Apr 24, 2017 at 20:02
0

Please take a look at

Trigger.old
Trigger.oldMap

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

You must log in to answer this question.

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