function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GailGail 

test class for trigger failing - trigger not triggering or querying wrong variable?

I have a before insert and update trigger that updates two writeable fields in the account based on custom setting fields. When I test manually, the trigger works. However, my test code is failing. The account name is saving but the trigger to update continent and region doesn't seem to run. I added start test and stop test around inserting the accounts but that doesn't seem to help. How do I ensure the trigger runs and updates the account a3? 

 

@isTest
private class Test_AccountTrigger {

    static testMethod void test() {


       //create 2 accounts -  billing country not in list, billing country in list
        Account a2 = new Account();
        a2.Name = 'invalid country';
        a2.billingcountry='alsdkfjasd;if';
        a2.industry = 'Agency';
        a2.Annual_company_revenue__c = 'under $100M';

        Account a3 = new Account();
        a3.Name = 'valid country';
        a3.billingcountry='Germany';
        a3.industry = 'Agency';        
        a3.Annual_company_revenue__c = 'under $100M';

        
        Test.startTest();  
                insert a2;    
                insert a3;
        Test.stopTest();
                  
                
        // Query the newly inserted records
        Account aa2 = [Select Name, Continent__c, Region1__c FROM Account where Id = :a2.Id];
        Account aa3 = [Select Name, Continent__c, Region1__c FROM Account where Id = :a3.Id];
        System.debug('aa2 ' + aa2.Name + ' continent = ' + aa2.Continent__c);
        System.debug('aa3 ' + aa3.Name + ' continent = ' + aa3.Continent__c);

        // If the trigger works, then territory and region will only be set on last account
        System.assertEquals(null, aa2.Continent__c);
        System.assertEquals(null, aa2.Region1__c);
        System.assertEquals('Europe',aa3.Continent__c);
        System.assertEquals('EMEA', aa3.Region1__c);
       

    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MTBRiderMTBRider

Ok, well, where is the contintent and region values that the trigger inserts coming from?  If these values are stored in a table that the trigger retrieves, the test method may not see it if your API version is 24.0 or later.  See page 151 in the Apex Developers guide, the section called "Isolation of Test Data from Organization Data in Unit Tests"

 

You can either create create the continent and region data in the test method or use IsTest(SeeAllData=true) per the guide.

 

Although it is not part of the problem, you really do not need the startTest and stopTest.  Those are just used to isolate the SOQL in your test method from governor limits.  It does not hurt to have them there, but may not be necessary.

 

Good luck.

All Answers

souvik9086souvik9086

Hi,

 

This one is correct

System.assertEquals(null, aa2.Continent__c);
System.assertEquals(null, aa2.Region1__c);

 

This one might gave the error.
System.assertEquals('Europe',aa3.Continent__c);
System.assertEquals('EMEA', aa3.Region1__c);

 

As you didn't inserted any value in this fields through test method. So it will always ne null. you can check by removing this two lines.

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

GailGail

Sorry, should have been more clear about the trigger I'm testing. The trigger updates Continent and Region based on the Billing Country of the Account. So, I'm trying to verify that after an Account is inserted with a valid country, the trigger then updates Continent and Region. If I pass values into this via the test class, then I'm not testing the trigger. 

MTBRiderMTBRider

The trigger may be working fine, the issue may be the assertEquals.   I sometimes have trouble with assertEquals giving me a false negative.  I am not sure why.  

 

Just for giggles, try changing from:

 

System.assertEquals('Europe',aa3.Continent__c);
System.assertEquals('EMEA', aa3.Region1__c);

 

to 

 

System.assert(aa3.Continent__c=='Europe',);
System.assert(aa3.Region1__c=='EMEA');

 

and see if that works

SF94108SF94108

Hello!

 

Instead of exact value match you could try to check for Not Null in case of aa3:

 

System.assertNOTEquals(NULL,aa3.Continent__c);
System.assertNOTEquals(NULL, aa3.Region1__c);

 

See if might do the trick.

GailGail

Unfortunately didn't work. The issue appears to be that the trigger is not running (or a3 is not going through the trigger, even though it should). When I debug the code, a3 has been created and billing country is equal to Germany but Continent and Region are null even though the trigger should have set them to Europe and EMEA. I have created an account with the exact same values manually in Salesforce and that account does get those values. 

GailGail

Hmm, so this is a clue from the debug logs:

 

 

select Name, Continent__c, Region1__c, BillingCountry from Account where Id = :tmpVar1

I would think "tmpVar1" is not the inserted Account but still the temporary account, where the trigger has not run yet. Arghhhh - I thought I might find some clues in sample code but it looks like my code is formatted the same as other tests....

MTBRiderMTBRider

Ok, well, where is the contintent and region values that the trigger inserts coming from?  If these values are stored in a table that the trigger retrieves, the test method may not see it if your API version is 24.0 or later.  See page 151 in the Apex Developers guide, the section called "Isolation of Test Data from Organization Data in Unit Tests"

 

You can either create create the continent and region data in the test method or use IsTest(SeeAllData=true) per the guide.

 

Although it is not part of the problem, you really do not need the startTest and stopTest.  Those are just used to isolate the SOQL in your test method from governor limits.  It does not hurt to have them there, but may not be necessary.

 

Good luck.

This was selected as the best answer
GailGail

Thanks so much, MTBRider!! I forgot about this. Yep, the region and continent are stored in a custom setting so the test wasn't seeing them. 

GailGail

And of course I just realized I'm better off creating the custom setting in my test case...