0

I am new to apex and I've written a trigger to update the Account Manager (user lookup field) to be the same as the Account Owner when certain circumstances are true. I've manually tested it and it works as intended. The problem I'm having is that in order to deploy it to production I need to have test code coverage for it, and after some googling and trial and error, I still don't know what I am doing.

Here is my trigger:

trigger updateAM on Account (before insert, before update) {   
    for (Account acct: trigger.new){  
        if(acct.Owner_AM__c == 0){  
            if(acct.Owner_Role__c == 'Affiliate Team'){  
                acct.Account_Manager__c = acct.OwnerId;   
            }
        }
    }
}

And here is my attempt at a unit test. I've created it as a new apex class.

@isTest  
public class TestAM_Update {   
    static testMethod void createNewAccount () {   
        Account AccountToCreate = new Account();   
        AccountToCreate.Name = 'Test Account';   
        AccountToCreate.RecordTypeId = '01261000000kNNy';   
        insert AccountToCreate;  
    }
}
1
  • Testing is about more than code coverage. You make sure the functionality says what it is supposed to. It is its own kind of documentation and also protection of your functionality. Besides all that, you should never hard-code Ids.
    – Adrian Larson
    Commented Feb 9, 2016 at 22:08

2 Answers 2

2

Consider following these steps:

  1. Use test Setup to create the data required for your test Class so that this data will be re-used for the different number of test methods that you write in your test class.
  2. Write different test Methods for the functionality that you are trying to achieve from your trigger or class followed by assertion statements to check the code whether it was doing what it supposed to do. I would recommend to write both positive test Methods on what's expected and negative test Method on what's not expected.

Quick Note: Don't hard code Id's as they are unique and different for each sandbox. Your trigger might run correctly in your sandbox for now but it will fail once it went to other environment as the same record type will have a different Id there. Use Schema methods to retrieve the recordType Ids.

Here is a sample of your test class on insert trigger looks like.

@isTest  
public class TestAM_Update 
{ 
  @testSetup static void insertTestData()
  {
     Account AccountToCreate = new Account();   
     AccountToCreate.Name = 'Test Account';

     // below two lines will let this class to test the trigger. 
     AccountToCreate.Owner_AM__c = 0;
     AccountToCreate.Owner_Role__c ='Affiliate Team'; 

     //make sure to perform null checks before each get call
     AccountToCreate.RecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('your RecordType Name').getRecordTypeId();;

     insert AccountToCreate;
  }
  static testMethod void testMethodOnInsert() 
  {  
     // write a soql query to retrieve the data inserted in testSetup  
     list<Account> accList = [
         select OwnerId, Account_Manager__c 
         from Account 
         where Owner_AM__c= 0 and Owner_Role__c ='Affiliate Team'
         limit 1
         ];

     system.assertNotEquals(accList.size(), 0);
     if(accList.size()>0)
     {
       system.assertEquals(accList[0].Account_Manager__c, accList[0].OwnerId);
     }

  }
}
2
  • quick point - testMethodOnInsert needs to assert false if accList.size() == 0 -- otherwise, there is no way of knowing if testmethod was misconstructed with wrong filter given the way you wrote this
    – cropredy
    Commented Feb 9, 2016 at 23:52
  • @crop1645 Thank you for pointing that. I updated my answer.
    – sam_s
    Commented Feb 10, 2016 at 0:05
0

Using help from sam_s (thanks!) and one of the developers at my company I was able to figure it out. Here it is.

@isTest  
public class TestAM_Update 
{ 

static testMethod void testMethodOnInsertAffilitateTeam() 
{  
 Account accountToCreate = null;
 accountToCreate = new Account();   
 accountToCreate.Name = 'Test Account';
 accountToCreate.RecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Affiliate').getRecordTypeId();

 User ownerUser = [
     select Id, UserRoleId
     from User u
     where UserRole.Name = 'Affiliate Team'
     limit 1];  

 accountToCreate.OwnerId = ownerUser.Id;   

 insert accountToCreate;
 string accountId = accountToCreate.Id;

 // write a soql query to retrieve the data inserted in testSetup  
 Account resultAccount = [
     select OwnerId, Account_Manager__c 
     from Account 
     where Id = :accountId
     limit 1];    

   system.assertEquals(resultAccount.OwnerId, resultAccount.Account_Manager__c);
}

static testMethod void testMethodOnInsertNonAffiliateTeam() 
{
 Account accountToCreate = null;
 accountToCreate = new Account();   
 accountToCreate.Name = 'Test Account';
 accountToCreate.RecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Affiliate').getRecordTypeId();

 User ownerUser = [
     select Id, UserRoleId
     from User u
     where UserRole.Name <> 'Affiliate Team'
     limit 1];  

 accountToCreate.OwnerId = ownerUser.Id;   

 insert accountToCreate;
 string accountId = accountToCreate.Id;

 // write a soql query to retrieve the data inserted in testSetup  
 Account resultAccount = [
     select OwnerId, Account_Manager__c 
     from Account 
     where Id = :accountId
     limit 1];    

   system.assertNotEquals(resultAccount.OwnerId, resultAccount.Account_Manager__c);
}

}

You must log in to answer this question.

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