You need to sign in to do that
Don't have an account?
Surya P
Test class for Insert and Update of multiple contacts
I have wrote a test class for an Apex class,but the code coverage is 42%.
Kindly help me where I'm wrong in the code.
Apex Class:
Test Class:
Thanks in Advance
Regards
Surya
Kindly help me where I'm wrong in the code.
Apex Class:
public class InsertContactsToAccount { @AuraEnabled public static void saveContacts(List<Contact> listContact, Id recordId) { list<contact> conlist = new list<contact>(); Insert listContact; for(Contact a:listContact){ a.AccountId = recordId; conlist.add(a); } update conlist; } }
Test Class:
@isTest public class InsertContactsToAccountTest { static testMethod void saveContactsTest(){ Account a = new Account(); a.Name ='Test'; insert a; Contact c = new Contact(); c.LastName = 'Test'; insert c; c = [select id from Contact where id =:c.id]; c.AccountId = a.Id; update c; List<Contact> con = [select id from Contact where id =:c.id]; InsertContactsToAccount.saveContacts(con,a.id); } }
Thanks in Advance
Regards
Surya
When creating unit test you just have to provide all the data or records needed for you saveContacts method and call that method.
Here your save contact method has List<Contact> (list of contacts) which are not inserted before and second parameter id (which is account id).
So you have to create List<Contact> and create an account and pass its id.
Try this code.
Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.
Thanks and Regards
All Answers
When creating unit test you just have to provide all the data or records needed for you saveContacts method and call that method.
Here your save contact method has List<Contact> (list of contacts) which are not inserted before and second parameter id (which is account id).
So you have to create List<Contact> and create an account and pass its id.
Try this code.
Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.
Thanks and Regards
Thank you so much for the clear explanation.
Regards.
Surya