1

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);
    }
 }

UPDATE - checkRecursive class:

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}
  • What is the value of: checkRecursive.runOnce() – dphil Dec 4 '14 at 20:50
  • Hi dphil - I have added the checkRecursive class in my original post. Thanks in advance! – sfdcIndicat0r Dec 4 '14 at 21:12
  • Confirmed in a deleted answer comment thread that the test is being run using a different User ID than the 3 the trigger excludes. – Keith C Dec 4 '14 at 21:24
  • dphil - I have just taken out of the checkRecursive call in the trigger and the test class passed, but i know that this is going to cause recursive issues later on... How can I keep calling checkRecursive and still able to fire/invoke the trigger from test class? Is that possible?? – sfdcIndicat0r Dec 4 '14 at 21:44
  • @MarkLiu Are there other places in code that calls your checkRecursive? Like does the insert call it or any of the Contact triggers? – dphil Dec 4 '14 at 21:47
2

You might need to change your recursive class then:

public Class checkRecursive{
  public static boolean run = true;
  public static boolean runOnce(){
  if(run){
     run = false;
     return true;
    }else{
        return run;
    }
  }
}

Then at the end of your test class:

ACC.Move_out_of_view__c = True;
checkRecursive.run = true;
update ACC;

Your contact class is setting it and you have nothing that resets it. You could add like a reset method instead of making the variable public. I just did that for simplicity.

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.