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
Mark LiuMark Liu 

Trigger is not firing from Test Classes

Hi all,

I have tried to search up on this for a while now but couldn't find any answer to my question... I have wrote a simple trigger that when an Account record is moved out of view (Moved out of view = true), then all the contacts associated with that specific account will be automatically marked as moved out of view (Moved out of view = true). Trigger works in sandbox and also before this incident, I was able to deploy this into production, but now as I was trying to add onto the same existing test class, I have noticed through System.assertEquals() that the trigger is not firing... Can someone please help explain what's going on? Thanks in advance...

Trigger:
trigger moveContactsOutOfView on Account (After Update) {
	if(checkRecursive.runOnce()){
  set<String> notAccId = New Set<String>();		
  set<String> accId = New Set<string>();
  List<Contact> conUpdate = new List<Contact>();
  List<Contact> con1Update = new list<Contact>();
  List<AccountTeamMember> d1 = new List<AccountTeamMember>();
  
      for (Account a1: trigger.new){
      if (a1.ID != null && a1.Move_out_of_view__c == True){
          accId.Add(a1.ID);
          }
      else {
      	notAccId.add(a1.ID);
      	}
      }
                                   
  LIST<Contact> cY = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: AccId];
  LIST<Contact> cN = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: notAccId];
  List<AccountTeamMember> toBeDeleted =[SELECT ID, LastModifiedById FROM AccountTeamMember WHERE AccountID In: AccId];

       for (Account a1: trigger.new){
          	for (Contact c1:cY){
          		if (c1.LastModifiedById != '00500000006xdMm' && c1.LastModifiedById != '00500000006wnoE' && c1.LastModifiedById != '00500000006wsIm'){
            		c1.Move_out_of_view__c = True;
              		conUpdate.add(c1);
          		}
            }
          	for (Contact c2:cN){
          		if (c2.LastModifiedById != '00500000006xdMm'&& c2.LastModifiedById != '00500000006wnoE' && c2.LastModifiedById != '00500000006wsIm'){
          	  		c2.Move_out_of_view__c = False;
          	  		con1Update.add(c2);
          		}
          	}
       }
          for (AccountTeamMember t1:toBeDeleted){
          		if (t1.LastModifiedById != '00500000006xdMm' && t1.LastModifiedById != '00500000006wnoE' && t1.LastModifiedById != '00500000006wsIm'){
          			d1.add(t1);
          		}
          }
    
update conUpdate;
update con1Update;
delete d1;
	}
}

test class:
@isTest
public class TestPrimaryLiaisonCount2{
    static testMethod void insertNewContact(){
    
    //NEW Account record
    Account ACC = new Account();
    ACC.recordtypeID = '012000000000j8S';
    ACC.Name = 'Liu Inc';
    ACC.Industry = 'Advertising';
    ACC.NumberOfEmployees = 500;
    ACC.Center__c = 'Full Service';
    ACC.Move_out_of_view__c = False;
    insert ACC;
    
    string accId = ACC.id;
    
    //NEW Contact record for 'ACC'
    Contact ACDC = new Contact();
    ACDC.FirstName = 'Testing';
    ACDC.LastName = 'DEV';
    ACDC.Primary_Liaison__c = 'Center';
    ACDC.AccountID = ACC.Id;
    insert ACDC;
    
    ACDC = [SELECT Id, AccountID, Move_out_of_view__c FROM Contact WHERE AccountID =: accId limit 1];
    system.assertEquals(false, ACDC.Move_out_of_view__c);
    
    ACC.Move_out_of_view__c = True;
    update ACC;
    
    ACDC = [SELECT Id, AccountID, Move_out_of_view__c FROM Contact WHERE AccountID =: accId limit 1];
    system.assertEquals(true, ACDC.Move_out_of_view__c);
    }
 }

 
Mark LiuMark Liu
P.S. Since adding system.assertEquals() method, the test class has been failing :(
James LoghryJames Loghry
I'd throw some system debugs in your code to see where the issue is.  Start by looking at the checkRecursive call.  Depending on how that class is written, your trigger may be running twice in your unit test (see the insert and update of your account), and preventing the trigger from running when it's updated.

That should also fix your System.assertEquals issue once you figure out why your trigger is not working.
Mark LiuMark Liu
Hey James - thanks.

Here's the checkRecursive class below. This is to prevent recursions. I need this in order to make the trigger function properly.
 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

 
James LoghryJames Loghry
Mark, that's your issue right there.  Since you're issuing 2 separate DMLs on Account, then the second run never happens.  You'll probably need to add additional logic into your check recursive method to reset run = true, in order to run your update call.  You could do this by changing your run variable to public, and then before you call "update ACC;" you would set run to true.
Art SmorodinArt Smorodin
Hi Mark, 

have you tried using system.assert() instead of system.assertEquals()?
According to the article (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm#apex_System_System_assert) you should use system.assert()  when working with Boolean values. I have tried that for your code and it looks like Test class passes just fine. (did some minor adjustments as well)
 
@isTest
public class TestPrimaryLiaisonCount2{
    static testMethod void insertNewContact(){
    
    //NEW Account record
    Account ACC = new Account();
    ACC.recordtypeID = '012000000000j8S';
    ACC.Name = 'Liu Inc';
    ACC.Industry = 'Advertising';
    ACC.NumberOfEmployees = 500;
    ACC.Center__c = 'Full Service';
    ACC.Move_out_of_view__c = False;
    insert ACC;
    
    string accId = ACC.id;
    
    //NEW Contact record for 'ACC'
    Contact ACDC = new Contact();
    ACDC.FirstName = 'Testing';
    ACDC.LastName = 'DEV';
    ACDC.Primary_Liaison__c = 'Center';
    ACDC.AccountID = ACC.Id;
    insert ACDC;
    
    ACDC = [SELECT Id, AccountID, Move_out_of_view__c FROM Contact WHERE AccountID =: accId limit 1];
    system.assertEquals(false, ACDC.Move_out_of_view__c);
    
    ACC.Move_out_of_view__c = Boolean.valueOf('true');
    update ACC;
    
    //ACDC = [SELECT Id, AccountID, Move_out_of_view__c FROM Contact WHERE AccountID =: accId limit 1];
    
    system.assert(True,ACDC.Move_out_of_view__c);
    }
 }

Please give it a try and see if it works for you as well. 

-Art.