You need to sign in to do that
Don't have an account?
Abhilash Daslal
how to cover the lines of method having multiple nested if statements
Hi Guys,
Can anyone give the test method for the following
public static List<Contact> updateContactList(Set<ID> setAccountID, Map<ID,Contact> mapContact,List<Contact> listNewContacts,List<Contact> listExistingContacts){
try{
Contact newContact = new Contact();
if(!setAccountID.isEmpty()){
listExistingContacts = [select id,LastName,Phone from Contact where Account in : setAccountID];
//Update existing Contact Records
if(listExistingContacts != null && !listExistingContacts.isEmpty()){
for(Contact existingContact :listExistingContacts){
newContact = mapContact.get(existingContact.Account);
if( existingContact.Required__c = true && (newContact.Account == existingContact.Account) ){
//No need to insert a new Contact record
if (listNewContacts.indexOf(newContact) !=-1 ){
listNewContacts.remove(listNewContacts.indexOf(newContact));
}else{}
}else if( existingContact.Required__c = true && (newContact.Account != existingContact.Account )){
existingContact.Required__c = false;
}
}
}
}
}catch(Exception ex){ex.getMessage();}
return listExistingContacts;
}
Can anyone give the test method for the following
public static List<Contact> updateContactList(Set<ID> setAccountID, Map<ID,Contact> mapContact,List<Contact> listNewContacts,List<Contact> listExistingContacts){
try{
Contact newContact = new Contact();
if(!setAccountID.isEmpty()){
listExistingContacts = [select id,LastName,Phone from Contact where Account in : setAccountID];
//Update existing Contact Records
if(listExistingContacts != null && !listExistingContacts.isEmpty()){
for(Contact existingContact :listExistingContacts){
newContact = mapContact.get(existingContact.Account);
if( existingContact.Required__c = true && (newContact.Account == existingContact.Account) ){
//No need to insert a new Contact record
if (listNewContacts.indexOf(newContact) !=-1 ){
listNewContacts.remove(listNewContacts.indexOf(newContact));
}else{}
}else if( existingContact.Required__c = true && (newContact.Account != existingContact.Account )){
existingContact.Required__c = false;
}
}
}
}
}catch(Exception ex){ex.getMessage();}
return listExistingContacts;
}
to cover the mentioned method you need to invoke this
updateContactList(Set<ID> setAccountID, Map<ID,Contact> mapContact,List<Contact> listNewContacts,List<Contact> listExistingContacts)
from the test class, by passing the valid values,
setAccountID - insert the Account and pass the ID
mapContact - insert the contacts and assign to the Account created in the previous step, add them to Map and while inserting the Contact do this
for eg
Contact newCon = new Contact(LastName ='test', Required__c =true);
insert newCon;