You need to sign in to do that
Don't have an account?
Parmeshwar Bhore 1010011
Write A test Class for below Code
public class ContactNameUpdate {
// Method to handle the contact inserts.
public static void newContactCreated(List<Account> accList){
List<Contact> conList = new List<Contact>();
for(Account acc : accList) {
Contact con = new Contact(AccountId = acc.Id);
List<String> nameStr = acc.Name.split(' ');
if(nameStr.size()>0)
con.LastName = nameStr[0];
if(nameStr.size()>1)
con.FirstName = nameStr[1];
con.Email = 'info@websitedomain.tld';
con.Only_Default_Contact__c = TRUE;
conList.add(con);
}
insert conList;
}
public static void updateCheckboxOnAccount(List<Contact> contactList){
Set<Id> accountIds = new Set<Id>();
for(Contact con : contactList) {
accountIds.add(con.AccountId);
}
List<Account> updatedAccounts = new List<Account>();
for(AggregateResult ar : [select count(id) , AccountId from Contact where AccountId IN :accountIds group by AccountId having count(id) >1 ]){
updatedAccounts.add(new Account(Id = (Id)ar.get('AccountId'), Only_Default_Contact__c=false));
}
if(!updatedAccounts.isEmpty())
update updatedAccounts;
}
}
// Method to handle the contact inserts.
public static void newContactCreated(List<Account> accList){
List<Contact> conList = new List<Contact>();
for(Account acc : accList) {
Contact con = new Contact(AccountId = acc.Id);
List<String> nameStr = acc.Name.split(' ');
if(nameStr.size()>0)
con.LastName = nameStr[0];
if(nameStr.size()>1)
con.FirstName = nameStr[1];
con.Email = 'info@websitedomain.tld';
con.Only_Default_Contact__c = TRUE;
conList.add(con);
}
insert conList;
}
public static void updateCheckboxOnAccount(List<Contact> contactList){
Set<Id> accountIds = new Set<Id>();
for(Contact con : contactList) {
accountIds.add(con.AccountId);
}
List<Account> updatedAccounts = new List<Account>();
for(AggregateResult ar : [select count(id) , AccountId from Contact where AccountId IN :accountIds group by AccountId having count(id) >1 ]){
updatedAccounts.add(new Account(Id = (Id)ar.get('AccountId'), Only_Default_Contact__c=false));
}
if(!updatedAccounts.isEmpty())
update updatedAccounts;
}
}
All Answers
The above seems to be a helper class for trigger if that is the case then for you can try checking this below snippet:
I have written for the first method you can extend this for the second snippet and upon running you would be achieving the necessary code coverage after making changes as per your use case.
Additionally, I would suggest you check this blog post regarding the test classes and the best practices you can follow:
>> Link to blog. (http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html)
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.