25

I created a trigger to open a case in my account if (Atribuido__c == 'SIM')

I want the trigger to fire just when this field is updated.

If this field is already updated and I update any other fields in my Account the trigger should not.

The trigger that I've done is firing always when I update my account if (Atribuido__c == 'SIM')

trigger CriarCaseAccount_AR on Account (after update) {
    List<Case> Cases = new List<Case>();
    List<Account> act = Trigger.new;

    for (Account ct : act) {
        if (ct.Antecipado__c == 'SIM' && ct.RecordTypeID == '012U00000000vYx' || ct.Antecipado__c == 'AR' && ct.RecordTypeID == '012U00000000vYx'){
            Case c = new Case(
                AccountID = ct.id,
                RecordTypeID = '012U000000011yC',
                Motivo__c = 'Formandos AR',
                Prazo_para_resolucao__c = Date.today(),
                Description = 'Montagens',
                Subject = ct.id,
                Ownerid = '005U0000001KraS'
            );

            cases.add(c);
        }
     }

     insert cases;
}

How I determine the trigger just fire when this specific field is updated?

Thanks Phil !

I just made some changes and works perfectly !

I was trying to fire my trigger just when "Antecipado" == 'SIM' or 'AR'. If this field is already 'SIM' or 'AR' and I update another field in my Account. The trigger can't be fired.

So:

for (Account acc: Trigger.new) {
    Account oldAccount = Trigger.oldMap.get(acc.ID);

    if (acc.Antecipado__c != oldAccount.Antecipado__c) {
        If (acc.Antecipado__c == 'SIM' || acc.Antecipado__c == 'AR') {

        }
    }
}
  • what do you mean: if the field is already updated? I assume it's translation thing, but can you be more specific? do you want to compare to the before update? was it updated on a previous transaction? – Saariko Apr 30 '13 at 15:22
  • Great, glad it worked @Everton CP7, if click on the Tick next to my answer, that marks it as answered. – Phil Hawthorn May 1 '13 at 7:03
38

For this, you can reference the Trigger.oldMap and Trigger.newMap map collections. So, you can compare old values with new values. For example:

for( Id accountId : Trigger.newMap.keySet() )
{
  if( Trigger.oldMap.get( accountId ).Antecipado__c != Trigger.newMap.get( accountId ).Antecipado__c )
  {
     // do something here because your field has changed
  }
}

So, the Trigger will always 'fire' but you will only act upon it if the new and old values are different (i.e. if the user updated the value).

  • Help I have almost the same problem, however, it doesn't seem to be working for me. **trigger PropOwned on Contact (after update) {<br> for (Contact c : Trigger.new){<br> Contact oldContact = Trigger.oldMap.get(c.id);<br> if (c.One_Prop_Owned__c != oldContact.One_Prop_Owned__c){<br> if (c.One_Prop_Owned__c == '');<br> McLabs2__Ownership__c newOwn = new McLabs2__Ownership__c();<br> newOwn.McLabs2__Contact__c = c.id;<br> newOwn.McLabs2__Property__c = c.One_Prop_Owned__c;<br> insert newOwn;<br> }<br> }<br> – Michael Webb Nov 10 '14 at 20:35
  • 2
    I believe you need to change Account to Id in the for loop. At least I did. – Matthew Brown Jun 15 '18 at 14:32
  • 1
    You're right, have modified - only took 5 years for someone to notice :) – Phil Hawthorn Jun 16 '18 at 17:49

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.