Skip to main content
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;

    }   
1 answer
  1. Apr 4, 2018, 9:01 AM
    Hi Abhilash,

    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;   

     
0/9000