+ Start a Discussion
Will Jones 18Will Jones 18 

Trigger works but Test Case fails with SystemAssert Fail. What should I fix?

I have the trigger below which updates a lookup field on our account page with a specific user depending on the criteria. It works when I test it in SF through the UI.
 
trigger UpdateSigningManger on Account (after insert, after update ) {

//If a Partnership person or GM, assign the Last Modified By user to Signing Manager when Vendor Stage
//goes to "Agreement of Commercials", "MSA Signed" or "Covered by MSA".
//Otherwise if not Partnership or C-Level, makes the signing manager the markets's GM.
//If the market's GM has not been assigned, assign to the user's GM.
//If all fails assign the current user.


//prevents an infinite loop
if(Recursion2.objectRecursion)
    return;
Recursion2.objectRecursion = TRUE;

List<account> acct_list = [SELECT Id, Vendor_Stage__c, market__c, Signing_Manager_lookup__c, lastmodifiedbyid, lastmodifiedby.General_Manager__c,
createdbyid, createdby.General_Manager__c, lastmodifiedby.profile.name, createdby.profile.name, RecordTypeId FROM Account WHERE Id IN: Trigger.newmap.keyset()];



for(Account acct : acct_list){

//query for the matching venue GM for that market 
string venue_gm = null;

list <market__c> venue_list = [SELECT Venue_General_Manager__c FROM Market__c WHERE Name =: acct.market__c];

if (venue_list.size()>0)
    {venue_gm =[SELECT Id, Name, Venue_General_Manager__c FROM Market__c WHERE Name =: acct.market__c LIMIT 1].Venue_General_Manager__c;}
    
    if(Trigger.isUpdate && acct.RecordTypeId == '012C0000000M8xd'){
    
        // Initialize Boolean variables
        Boolean newAcctIsAgreed = FALSE;
        Boolean oldAcctIsAgreed = FALSE;
        
        //Access the old record by its ID in Trigger.oldmap
        Account oldAcct = Trigger.oldmap.get(acct.Id);
    
        //Trigger.new and Trigger.old values are assigned to boolean variables for comparison
        
        if(oldAcct.RecordTypeId == '012C0000000M8xd'){
            if(oldAcct.Vendor_Stage__c == null){
                oldAcctIsAgreed = FALSE;}
            else{
                oldAcctIsAgreed = oldAcct.Vendor_Stage__c.equals('Agreement of Commercials') || oldAcct.Vendor_Stage__c.equals('MSA Signed') || oldAcct.Vendor_Stage__c.equals('Covered by MSA');
            }
        }    
            if(acct.Vendor_Stage__c != null){
                newAcctIsAgreed = acct.Vendor_Stage__c.equals('Agreement of Commercials') || acct.Vendor_Stage__c.equals('MSA Signed') || Acct.Vendor_Stage__c.equals('Covered by MSA');
            }
        // Check that the field was just changed to the new value.  
            if(!oldAcctIsAgreed && newAcctIsAgreed){
                if(acct.lastmodifiedby.profile.name.equals('Venue Manager') || acct.lastmodifiedby.profile.name.equals('Vendor Manager 2') ||acct.lastmodifiedby.profile.name.equals('C-Level'))
                    {acct.Signing_Manager_lookup__c = acct.lastmodifiedbyid;}
                else if(venue_gm != null)
                    {acct.Signing_Manager_lookup__c = venue_gm;}
                else if(acct.lastmodifiedby.General_Manager__c != null)
                    {acct.Signing_Manager_lookup__c = acct.lastmodifiedby.General_Manager__c;}
                else{acct.Signing_Manager_lookup__c = acct.lastmodifiedbyid;} 
            }
            
     }
            
     else {
         if(acct.RecordTypeId == '012C0000000M8xd'){
             if(acct.Vendor_Stage__c.equals('Agreement of Commercials') || acct.Vendor_Stage__c.equals('MSA Signed')|| acct.Vendor_Stage__c.equals('Covered by MSA'))
                 if(acct.createdby.profile.name.equals('Venue Manager') || acct.createdby.profile.name.equals('Vendor Manager 2') ||acct.createdby.profile.name.equals('C-Level'))
                     {acct.Signing_Manager_lookup__c = acct.createdbyid;}
                 else if(venue_gm != null)
                     {acct.Signing_Manager_lookup__c = venue_gm;} 
                 else if(acct.createdby.General_Manager__c != null)
                     {acct.Signing_Manager_lookup__c = acct.createdby.General_Manager__c;}       
                 else {acct.Signing_Manager_lookup__c = acct.createdbyid;}
         }
     }
    
  
}

if (acct_list.isEmpty()==FALSE){
    database.update(acct_list);
}

}

The test class below does not cover all cases yet, but I can't even get the first few test cases to work. Anywhere that I use the Signing Manager field in the assertion it is always null. I get the error "Assertion Failed: Expected: null, Actual: 'whatever the other value is' ". Why is my test class not populating the Signing Manager field but when I actually use the UI it works perfectly?
 
@isTest

private class Account_SigningManager_3 {

static testMethod void SigningManagerTest_3() {

Profile profilename = [select ID, name from Profile where Name = 'Account Manager' limit 1];

User u1 = new User();
      
       u1.FirstName = 'Nick';
       u1.LastName  = 'Jones';
       u1.Email     = 'nickjones+kapow@gmail.com';
       u1.Username  = 'nick-kapowtest1@gmail.com';
       u1.Alias     = 'auser';
       u1.ProfileId = profilename.ID;
       u1.TimeZoneSidKey    = 'America/Denver';
       u1.LocaleSidKey      = 'en_US';
       u1.EmailEncodingKey  = 'UTF-8';
       u1.LanguageLocaleKey = 'en_US';
       u1.isactive = TRUE;
       
       insert u1;
       

 Profile profilename2 = [select ID, name from Profile where Name = 'Venue Manager' limit 1];      
User u2 = new User();
       

       u2.FirstName = 'Anna';
       u2.LastName  = 'Smith';
       u2.Email     = 'annasmith+kapow@gmail.com';
       u2.Username  = 'sfdc-kapowtest2@gmail.com';
       u2.Alias     = 'buser';
       u2.ProfileId = profilename2.ID;
       u2.TimeZoneSidKey    = 'America/Denver';
       u2.LocaleSidKey      = 'en_US';
       u2.EmailEncodingKey  = 'UTF-8';
       u2.LanguageLocaleKey = 'en_US';
       u2.General_Manager__c = u1.id;
       u2.isactive = TRUE;
       insert u2;
       


       
User u3 = new User();
       

       u3.FirstName = 'Tim';
       u3.LastName  = 'West';
       u3.Email     = 'timwest+kapow@gmail.com';
       u3.Username  = 'sfdc-kapowtest3@gmail.com';
       u3.Alias     = 'cuser';
       u3.ProfileId = profilename2.ID;
       u3.TimeZoneSidKey    = 'America/Denver';
       u3.LocaleSidKey      = 'en_US';
       u3.EmailEncodingKey  = 'UTF-8';
       u3.LanguageLocaleKey = 'en_US';
       u3.General_Manager__c = u1.id;
       u3.isactive = TRUE;
       insert u3;
          

// Create Test Market
Market__c m1 = new Market__c(name='Dallas', Venue_General_Manager__c = u3.id );
insert m1;

// Create Test Market
Market__c m2 = new Market__c(name='Chicago', Venue_General_Manager__c = u2.id );
insert m2;




// Create Test Account
Account a1 = new Account(name='sample2', RecordTypeId = '012C0000000M8xd', Converted_From_Lead__c = TRUE, Vendor_Stage__c = 'One off event', Close_Date__c = date.Today(), Margin_Type__c = 'Standard', Target_Margin__c = 20, Vendor_Type__c = 'Activity', Website = 'www.test.com', market__c='Dallas');

// New Account 
insert a1; 


System.RunAs(u1){




//Test # 1 - update to MSA Signed, a Signing Manager stage


a1.Vendor_Stage__c = 'MSA Signed';
update a1;



Account result = [select createdbyId, lastmodifiedbyid, OwnerId, Signing_Manager_lookup__c, market__c, Vendor_Stage__c from Account where Id =: a1.Id limit 1]; 


system.debug('Stage is ' + result.Vendor_Stage__c);
system.debug('Market is ' + result.market__c);
system.debug('Venue Manager by ID is ' + u3.id);
system.debug('Created by ID is ' + result.createdbyId);
system.debug('Signing Manager by ID is ' + result.Signing_Manager_lookup__c);
system.debug('Owner by ID is ' + result.OwnerId);
System.assertEquals(m1.Venue_General_Manager__c, u3.id);
System.assertEquals(result.Signing_Manager_lookup__c, u3.id);


}

// Create Test Account
Account a2 = new Account(name='sample3', RecordTypeId = '012C0000000M8xd', Converted_From_Lead__c = TRUE, Vendor_Stage__c = 'One off event', Close_Date__c = date.Today(), Margin_Type__c = 'Standard', Target_Margin__c = 20, Vendor_Type__c = 'Activity', Website = 'www.test.com', market__c='Chicago');

// New Account 
insert a2; 


System.RunAs(u2){




//Test # 2 - update to MSA Signed, a Signing Manager stage


a2.Vendor_Stage__c = 'MSA Signed';
update a2;



Account result2 = [select createdbyId, lastmodifiedbyid, OwnerId, Signing_Manager_lookup__c, market__c, Vendor_Stage__c from Account where Id =: a2.Id limit 1]; 

system.debug('User 2 is ' + u2.id);
system.debug('User 2 is ' + result2.lastmodifiedbyid);
system.debug('Stage is ' + result2.Vendor_Stage__c);
system.debug('Market is ' + result2.market__c);
system.debug('Venue Manager by ID is ' + u3.id);
system.debug('Created by ID is ' + result2.createdbyId);
system.debug('Signing Manager by ID is ' + result2.Signing_Manager_lookup__c);
system.debug('Owner by ID is ' + result2.OwnerId);
System.assertEquals(m2.Venue_General_Manager__c, result2.lastmodifiedbyid); 
System.assertEquals(result2.Signing_Manager_lookup__c, result2.lastmodifiedbyid); 


}

System.RunAs(u2){    
// Create Test Account
Account a3 = new Account(name='sample4', RecordTypeId = '012C0000000M8xd', Converted_From_Lead__c = TRUE, Vendor_Stage__c = 'MSA Signed', Close_Date__c = date.Today(), Margin_Type__c = 'Standard', Target_Margin__c = 20, Vendor_Type__c = 'Activity', Website = 'www.test.com', market__c='Chicago');

// New Account 
insert a3; 







//Test # 3 - insert to MSA Signed, a Signing Manager stage






Account result3 = [select createdbyId, lastmodifiedbyid, OwnerId, Signing_Manager_lookup__c, market__c, Vendor_Stage__c from Account where Id =: a3.Id limit 1]; 

system.debug('User 2 is ' + u2.id);
system.debug('User 2 is ' + result3.lastmodifiedbyid);
system.debug('Stage is ' + result3.Vendor_Stage__c);
system.debug('Market is ' + result3.market__c);
system.debug('Venue Manager by ID is ' + u3.id);
system.debug('Created by ID is ' + result3.createdbyId);
system.debug('Signing Manager by ID is ' + result3.Signing_Manager_lookup__c);
system.debug('Owner by ID is ' + result3.OwnerId);
System.assertEquals(m2.Venue_General_Manager__c, result3.createdbyid); 



}




}

 
SonamSonam (Salesforce Developers) 
When creating a new user, you are pulling profile from existing ORG data, however, you haven't user seealldata = True.I think the user is not being created as the profile is not fetched and user cannot be created without a profile..please check and confirm:

reference:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_seealldata_using.htm