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
parth jollyparth jolly 

Please can anyone help me writing test class for this apex class

global with sharing class AccountUtilities {
   
   webservice static String getAssociates(String accid) {

      
      Account accs = 
         [select id,
         (select id,Associates__c,Name from Contacts 
         where Status__c = 'Primary Contact') from Account 
         where id = :accid LIMIT 1];
      
      list<contact> contacttoUpdate = new list<contact>();
      if(accs.Contacts != null && accs.Contacts.size() > 0 )
      {
          
          list<string> Names = new list<string>();
          
          for(Contact con : accs.Contacts)
          {
              Names.add(con.Name);
          }
          
          for(Contact con : accs.Contacts)
          {
              system.debug('Names:'+Names);
              string Associates = '';
              integer ncount = Names.size();
              integer count = 1;
              
              for(string n : Names)
              {
                   
                  system.debug('n:'+n);
                  if(con.Name != n)
                  {
                      count++;
                      if(Associates == '')
                          Associates += n;
                      else
                      {
                          system.debug('nc::'+ncount);
                          system.debug('c::'+count);
                          
                          if(count!= 1 && count == ncount)
                              Associates += ', and '+ n;
                          else
                              Associates += ', '+ n;
                      }
                  }
                  
                     
              }        
              
              
              con.Associates__c = Associates;
              con.Email_Campaign__c = 'Enrolled';
              contacttoUpdate.add(con);
              
          }
      }
      else
      {
          return 'No Primary Contact found for this Account';
      }
      
      
      
      try {
         Database.update(contacttoUpdate);
      }
      catch (DmlException e) {
         return e.getMessage();
      }

      
      return 'Contact associated succesfully';
      
   }
}

 
Best Answer chosen by parth jolly
Harish RamachandruniHarish Ramachandruni
Hi,


You can Copy past  below code .
 
@isTest 
private class AccountUtilitiestest{ 
    
    static testMethod void AccountUtilitiesMethodTest () {
    
    Account TestAcc= new Account();
    TestAcc.name='TestAccountUtil';
    insert TestAcc;
    
    contact Testcon= new Contact();
    Testcon.FirstName='TestFirstname';
    Testcon.LastName='TestFirstname';
    Testcon.Status__c = 'Primary Contact'
    Testcon.Accountid=TestAcc.id;
    insert Testcon;
    
    contact Testcon1= new Contact();
    Testcon1.FirstName='Testsecon';
    Testcon1.LastName='Testsecon';
    Testcon1.Status__c = 'Primary Contact'
    Testcon1.Accountid=TestAcc.id;
    insert Testcon1;
    
    Test.startTest();
    Testcon.LastName='TestFirst';
    update Testcon;
    AccountUtilities.getAssociates(TestAcc.id);
    Test.stopTest();
    }
}

Regards ,
Harish.R.

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

Use below Code, 
 
@isTest 
private class AccountUtilitiesTest { 
    
    static testMethod void AccountUtilitiesMethodTest () {
    
    Account TestAcc= new Account();
    TestAcc.name='TestAccountUtil';
    insert TestAcc;
    
    contact Testcon= new Contact();
    Testcon.FirstName='TestFirstname';
    Testcon.LastName='TestFirstname';
    Testcon.Accountid=TestAcc.id;
    insert Testcon;
    
    contact Testcon1= new Contact();
    Testcon1.FirstName='Testsecon';
    Testcon1.LastName='Testsecon';
    Testcon1.Accountid=TestAcc.id;
    insert Testcon1;
    
    Test.startTest();
    Testcon.LastName='TestFirst';
    update Testcon;
    AccountUtilities.getAssociates(TestAcc.id);
    Test.stopTest();
    }
}

Hope this will help you, 

Thanks
karthik
 
parth jollyparth jolly
Hi Kartik,

thanks forthe reply but the code you have commented is just covering 21% code coverage .please see attched screenshot User-added image
Thanks 
Paarth
Harish RamachandruniHarish Ramachandruni
Hi,


You can Copy past  below code .
 
@isTest 
private class AccountUtilitiestest{ 
    
    static testMethod void AccountUtilitiesMethodTest () {
    
    Account TestAcc= new Account();
    TestAcc.name='TestAccountUtil';
    insert TestAcc;
    
    contact Testcon= new Contact();
    Testcon.FirstName='TestFirstname';
    Testcon.LastName='TestFirstname';
    Testcon.Status__c = 'Primary Contact'
    Testcon.Accountid=TestAcc.id;
    insert Testcon;
    
    contact Testcon1= new Contact();
    Testcon1.FirstName='Testsecon';
    Testcon1.LastName='Testsecon';
    Testcon1.Status__c = 'Primary Contact'
    Testcon1.Accountid=TestAcc.id;
    insert Testcon1;
    
    Test.startTest();
    Testcon.LastName='TestFirst';
    update Testcon;
    AccountUtilities.getAssociates(TestAcc.id);
    Test.stopTest();
    }
}

Regards ,
Harish.R.
This was selected as the best answer
karthikeyan perumalkarthikeyan perumal
Hello 


I did't use statuc__c field in I. My test class
I thought you will add it those field while testing .and I forget to add the note also..
assign the value for this field like below.

    Testcon.Status__c ='Primary Contact';
     Testcon1.Status__c ='Primary Contact';

so the list size will not null it's give more then 90 percent coverage 


thanks
karthik

parth jollyparth jolly

Hello  Kartik 

The above code is covering 81% .Can you please post your code which can cover above 90% code coverage.

Thanks
 

 

karthikeyan perumalkarthikeyan perumal
Hello

Here is the code for 84%
for thses lines could not be get coverage Coz , Associates  is always null loop inside loop and conditions,  so you can't get the coverage for this. 

"if(count!= 1 && count == ncount)
                             Associates += ', and '+ n;
                       else
                             Associates += ', '+ n;" 

alos for catch block you can't get the coverage, 

catch (DmlException e) {
         return e.getMessage(); }

User-added image

Hopw this will clear. 
@isTest 
private class AccountUtilitiesTest { 
    
    static testMethod void AccountUtilitiesMethodTest () {
    
    Account TestAcc= new Account();
    TestAcc.name='TestAccountUtil';
    insert TestAcc;
    
    contact Testcon= new Contact();
    Testcon.FirstName='TestFirstname';
    Testcon.LastName='TestFirstname';
    Testcon.Status__c='Primary Contact';
    Testcon.Accountid=TestAcc.id;
    insert Testcon;
    
    contact Testcon1= new Contact();
    Testcon1.FirstName='Testsecon';
    Testcon1.LastName='Testsecon';
    Testcon1.Status__c='Primary Contact';
    Testcon1.Accountid=TestAcc.id;
    insert Testcon1;
    
    Test.startTest();
    Testcon.LastName='TestFirst';
    update Testcon;   
    AccountUtilities.getAssociates(TestAcc.id);
    
    Test.stopTest();
    }
    
      static testMethod void AccountUtilitiesMethodTest1 () {
    
    Account TestAcc= new Account();
    TestAcc.name='TestAccountUtil';
    insert TestAcc;
    
    contact Testcon= new Contact();
    Testcon.FirstName='TestFirstname';
    Testcon.LastName='TestFirstname';
    Testcon.Status__c='Primary';
    Testcon.Accountid=TestAcc.id;
    insert Testcon;
    
    contact Testcon1= new Contact();
    Testcon1.FirstName='Testsecon';
    Testcon1.LastName='Testsecon';
    Testcon1.Status__c='Primary';
    Testcon1.Accountid=TestAcc.id;
    insert Testcon1;
    
    Test.startTest();
    Testcon.LastName='Test';
   
    update Testcon; 
      
    AccountUtilities.getAssociates(TestAcc.id);
     
    Test.stopTest();
    }
   
   
}


Hope this will help you, 

Mark ir Best ANSWER if its get more coverage, 

Thanks
karthik