You need to sign in to do that
Don't have an account?
parth 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'; } }
You can Copy past below code .
Regards ,
Harish.R.
All Answers
Use below Code,
Hope this will help you,
Thanks
karthik
thanks forthe reply but the code you have commented is just covering 21% code coverage .please see attched screenshot
Thanks
Paarth
You can Copy past below code .
Regards ,
Harish.R.
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
Hello Kartik
The above code is covering 81% .Can you please post your code which can cover above 90% code coverage.
Thanks
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(); }
Hopw this will clear.
Hope this will help you,
Mark ir Best ANSWER if its get more coverage,
Thanks
karthik