• Michael Anderson 40
  • NEWBIE
  • 0 Points
  • Member since 2016
  • Sales Operations Systems Architect
  • Zocdoc

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

I have an after update trigger that is firing 3 times when I run my test class. It only fires twice when I update the record through the UI. I'm stumped!

When I run my test class, the very first time the trigger fires, trigger.New and trigger.Old values appear to be the same, which is not what I expect to see in the debug log. The second time it fires I can see that the new and old values are different, which is what I expect. I know that one of the additional trigger runs is due to a workflow field update on the Account, that's fine. 

I can control the recursion through a static variable in my class, as I would normally do, but the problem here is that the recursion control stops the trigger after the first run, and in the first run the old and new values are the same so my trigger logic doesn't work as I would expect.

I've stripped out most logic from the code so that now it's just a plain vanilla textbook setup. 
 

trigger AccountTrigger on Account (after update) {
     AccountHelper.runUpdate();
}

public class AccountHelper {

     public static void runUpdate(){

              system.debug(trigger.New[0].custom_field__c);
              system.debug(trigger.Old[0].custom_field__c);

     }
}

@isTest
public class AccountHelperTest{

     private static testmethod void testThis{

           Account a = new Account(Name='TestAccount');
           insert a;

           a.custom_field__c = 'Changed';
           update a;
      }
}
When I run the test, with a workflow field update on the Account, I see this in the debug log:
1 newvalue = null
2 oldvalue = null
3 newvalue = Changed
4 oldvalue = null
5 newvalue = Changed
6 oldvalue = null

When I open an account through the UI and change the custom field and save, the debug will show only lines 3 through 6.

Any ideas would be greatly appreciated!

I have an after update trigger that is firing 3 times when I run my test class. It only fires twice when I update the record through the UI. I'm stumped!

When I run my test class, the very first time the trigger fires, trigger.New and trigger.Old values appear to be the same, which is not what I expect to see in the debug log. The second time it fires I can see that the new and old values are different, which is what I expect. I know that one of the additional trigger runs is due to a workflow field update on the Account, that's fine. 

I can control the recursion through a static variable in my class, as I would normally do, but the problem here is that the recursion control stops the trigger after the first run, and in the first run the old and new values are the same so my trigger logic doesn't work as I would expect.

I've stripped out most logic from the code so that now it's just a plain vanilla textbook setup. 
 

trigger AccountTrigger on Account (after update) {
     AccountHelper.runUpdate();
}

public class AccountHelper {

     public static void runUpdate(){

              system.debug(trigger.New[0].custom_field__c);
              system.debug(trigger.Old[0].custom_field__c);

     }
}

@isTest
public class AccountHelperTest{

     private static testmethod void testThis{

           Account a = new Account(Name='TestAccount');
           insert a;

           a.custom_field__c = 'Changed';
           update a;
      }
}
When I run the test, with a workflow field update on the Account, I see this in the debug log:
1 newvalue = null
2 oldvalue = null
3 newvalue = Changed
4 oldvalue = null
5 newvalue = Changed
6 oldvalue = null

When I open an account through the UI and change the custom field and save, the debug will show only lines 3 through 6.

Any ideas would be greatly appreciated!
Hi,

I am having a massive confusion,

We have a database of 400,000 records.. which calls for duplication.

I want to check if alternate phone number of any of the account matches with the Primary phone number of any of the account, If it does, there is a checkbox  called "Marked For deletion"  m that checkbox should get selected and primary phone number of that account should become the alternate phone number of the acount against which it has matched 

So lets say ,

Accoount : A  Primary No : 1234  Secondary No : 5678

Accoount : B  Primary No : 9241  Secondary No : 1234 

Accoount : C  Primary No : 9241  Secondary No :  1234

Account B and Account C should have marked for deletion checked.
and Account B's primary phone number should become Secondary phone number of Account A. 

Please help me in achieving this