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
maiyakumaiyaku 

Loop not work in Test Class

i'm wright test class full loop , because problem for loop

 

trigger MarkforDelAllinCon on Contact(before Update) {
set<ID> setContactID = new set<ID>();
    
        for(Contact Con: Trigger.new){
        if(Con.MarkforDelete__c == true){   
            setContactID.Add(Con.id);  
        }
        }
             
        List<Relationship__c > lstRel = [Select id,Mark_for_Delete__c,Contact_ID__c,Account_ID__c,Rev_Account_ID__c,Rev_Contact_ID__c  from Relationship__c Where  Contact_ID__c in :   setContactID and Mark_for_Delete__c !=: True];
        List<Relationship__c> Rel = new List<Relationship__c>(); 
        for(Contact Con: Trigger.new){
            if(lstRel.size() != 0 ){
                     for(Relationship__c Res : lstRel){ 
                        Res.Contact_ID__c  = Con.id;
                        Res.Rev_Contact_ID__c  = Con.id;            
                        Res.Mark_for_Delete__c = true;
                        Rel.Add(Res);
                     } 
            update Rel;                          
            }
}
}

        for(Relationship__c Res : lstRel){
                        Res.Contact_ID__c  = Con.id;
                        Res.Rev_Contact_ID__c  = Con.id;           
                        Res.Mark_for_Delete__c = true;
                        Rel.Add(Res);
                     }
            update Rel;                         
            }

 

 

 

 

@isTest
private class TestMarkforDelAllinCon {
    static testMethod void myTest() {
    Test.StartTest();
    Account objAcc = new Account();
    objAcc.Name = 'testAcc1';
    objAcc.AccountNumber = 'TestA00001';
    insert objAcc;
    
    Account objAccRev = new Account();
    objAccRev.Name = 'testAcc2';
    objAccRev.AccountNumber = 'TestA00001';
    insert objAccRev;
    
    Contact con = new Contact();
    con.AccountID = objAcc.id;
    con.LastName = 'TestCon1';
    con.MarkforDelete__c = True;
    insert con;
    update con;
    
    Contact ConRev = new Contact();
    ConRev.AccountID = objAcc.id;
    ConRev.LastName = 'TestCon2';
    ConRev.MarkforDelete__c = True;
    insert conRev;
    update conRev;
    
    
    Relationship__c  Rel = new Relationship__c();
    List<Relationship__c > lstRel = [Select id,Mark_for_Delete__c,Contact_ID__c,Account_ID__c,Rev_Account_ID__c,Rev_Contact_ID__c from Relationship__c Where Contact_ID__c =: con.id and Mark_for_Delete__c !=: True];
    if(lstRel.size() != 0){
        
   
        ApexPages.currentPage().getParameters().put('id',con.id);
        Rel.Contact_ID__c = Con.id;
        Rel.Rev_Contact_ID__c = Con.id;
        Rel.Mark_for_Delete__c = true;
        update Rel ; 
       
     }
     Test.StopTest(); 
}
}

 

a red line is problem

thank you so much

Ankit AroraAnkit Arora

Try changing this section :

 

Contact ConRev = new Contact();
    ConRev.AccountID = objAcc.id;
    ConRev.LastName = 'TestCon2';
    ConRev.MarkforDelete__c = True;
    insert conRev;
    update conRev;
    
    
    Relationship__c  Rel = new Relationship__c();
    List<Relationship__c > lstRel = [Select id,Mark_for_Delete__c,Contact_ID__c,Account_ID__c,Rev_Account_ID__c,Rev_Contact_ID__c from Relationship__c Where Contact_ID__c =: con.id and Mark_for_Delete__c !=: True];

 With this :

 

Contact ConRev = new Contact();
    ConRev.AccountID = objAcc.id;
    ConRev.LastName = 'TestCon2';
    ConRev.MarkforDelete__c = True;
    insert conRev;
    
    
    Relationship__c  Rel = new Relationship__c(Mark_for_Delete__c = true , Contact_ID__c = conRev.id);
    insert Rel ;

    update conRev;

 Please fill all mandatory fields in Rel before insert. Also notice I have updated conRev after the Rel insert.

 

Let me know if it is not clear of you face any problem.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

Before you start testing I would suggest you to change your trigger, it will not work properly in bulk handling case.

 

Mistakes :

1) In your trigger you are using update rel; dml statement in for loop

2) You will update all relationships for all contacts, so only last contact will be effective. You will see Id of last  contact in all relationship. You should only update relationship which are related to one contact.

 

Your trigger should be like this, use this

 

trigger MarkforDelAllinCon on Contact(before Update) {
set<ID> setContactID = new set<ID>();
    
        for(Contact Con: Trigger.new){
        if(Con.MarkforDelete__c == true){   
            setContactID.Add(Con.id);  
        }
        }
        MAP<ID , List<Relationship__c>> mapRelationship = new MAP<ID , List<Relationship__c>>(); 
        List<Relationship__c> tempList = new List<Relationship__c>();    
        for(Relationship__c relObj : [Select id,Mark_for_Delete__c,Contact_ID__c,Account_ID__c,Rev_Account_ID__c,Rev_Contact_ID__c  from Relationship__c Where  Contact_ID__c in :   setContactID and Mark_for_Delete__c !=: True])
            {
                 if(mapRelationship.containsKey(relObj.Contact_ID__c))
                  {
                        tempList = mapRelationship.get(relObj.Contact_ID__c)
                  }
                  else
                 {
                     tempList = new List<Relationship__c>();
                 }
                 tempList.add(relObj); 
                 mapRelationship.put(relObj.Contact_ID__c , relObj);
        
            }
        List<Relationship__c> Rel = new List<Relationship__c>(); 
        for(Contact Con: Trigger.new)
            {
            if(mapRelationship.containsKey(con.id))
               {
                     for(Relationship__c Res : mapRelationship.get(con.id))
                     { 
                        Res.Contact_ID__c  = Con.id;
                        Res.Rev_Contact_ID__c  = Con.id;            
                        Res.Mark_for_Delete__c = true;
                        Rel.Add(Res);
                     } 
               } 
                                      
            }
         update Rel;  
}

 Now your test class, here it is

 

@isTest
private class TestMarkforDelAllinCon {
    static testMethod void myTest() {
    Test.StartTest();
    Account objAcc = new Account();
    objAcc.Name = 'testAcc1';
    objAcc.AccountNumber = 'TestA00001';
    insert objAcc;
    
    Account objAccRev = new Account();
    objAccRev.Name = 'testAcc2';
    objAccRev.AccountNumber = 'TestA00001';
    insert objAccRev;
    
    List<Contact> listToUpdate =  new List<Contact>();
    Contact con = new Contact();
    con.AccountID = objAcc.id;
    con.LastName = 'TestCon1';
    con.MarkforDelete__c = True;
    insert con;
    listToUpdate.add(con);  
    
    
    Contact ConRev = new Contact();
    ConRev.AccountID = objAcc.id;
    ConRev.LastName = 'TestCon2';
    ConRev.MarkforDelete__c = True;
    insert conRev;
    update conRev;
    listToUpdate.add(conRev);

    Relationship__c  Rel = new Relationship__c();    
    for(Integer i = 0 ; i < listToUpdate.size() ; i++)
     {
       rel.Contact_ID__c = listToUpdate.get(i);
       //Fill all other required field if any
     }
    insert Rel;
     
    Update listToUpdate;         
    Test.StopTest(); 
}

 I hope it will help you.