You need to sign in to do that
Don't have an account?
maiyaku
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
Try changing this section :
With this :
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
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
Now your test class, here it is
I hope it will help you.