0

I All, I am facing an issue with the test class. I have trigger on Insert and Update. I am using the test class given below which covers Insert trigger but do not cover the portion in Update. Need some help here. Thanks in advance Trigger:

trigger AccountTrigger on Account (before update, after insert, after update) {                
        if(checkRecursive.runOnce()){
            if(Trigger.isUpdate && Trigger.isAfter){  
                //Update Account based on some rules    
            }
        }
    }

TestClass:

static testMethod void test() {

    account OrgAccount = new account();      
    OrgAccount .Name = 'Test Org Account 1'; 
    OrgAccount .RecordTypeId = orgRecordTypeId;
    Insert OrgAccount ;

    OrgAccount .Some_Id= 'PA-7891';                
    Update testOrgAccount;

    Test.stopTest();        
}
  • 1
    welcome to SFSE KrishSalesforce -- best practice is to not just dump code after your question title - instead, explain where in the code things aren't going as expected; also remove unnecessary code not relevant to the issue – cropredy Feb 24 '15 at 1:12
  • I am sorry, I am totally new to this thing. Can you please how do I update my content? – KrishSalesforce Feb 24 '15 at 1:41
  • click the edit button underneath your post – cropredy Feb 24 '15 at 1:42
  • Krish you might need more rep to edit (I forget if that's the case). If so, post a comment here and someone can move it into the question for you. Welcome to SFSE! – Matt Lacey Feb 24 '15 at 3:24
  • most likely you need to reset the value of checkRecursive.runOnce() before the update. Otherwise there are so many errors with your code one it will not compile two you will get an error that testing has not started. – Eric Feb 24 '15 at 3:59
1

My money is on the runOnce() method. It probably relies on a static boolean that may need to be reset throughout the test if the trigger fires more than once. For example lets say my code looked like:

Account one = new Account();
insert one;
Account two = new Account();
insert two;

The second insert will cause the trigger to fire again, but the static boolean that runOnce() looks at will be true in the second insert. Something like this might help:

Account one = new Account();
insert one;
checkRecursive.alreadyRun = false;
Account two = new Account();
insert two;
0

As you didn't have provided much of details. Try using this kind of pattern for your test class.

static testMethod void test() {
    account orgAccount = new account();      
    orgAccount.Name = 'Test Org Account'; 
    orgAccount.RecordTypeId = orgRecordTypeId;
    Insert orgAccount;

    orgAccount.Some_Id= 'PA-7891';                
    Update orgAccount;        
}

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.