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
Parmeshwar Bhore 1010011Parmeshwar 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;
    }
}
Best Answer chosen by Parmeshwar Bhore 1010011
Parmeshwar Bhore 1010011Parmeshwar Bhore 1010011
Thanks Anutej..

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Parmeshwar,

The above seems to be a helper class for trigger if that is the case then for you can try checking this below snippet:
 
@isTest 
public class ContactNameUpdateTest {
    static testMethod void testMethod1() 
    {
		List<Account> alist= new List<Account>();
        Set<id> idset = new set<id>();
        Account a= new Account();
        a.name= 'test';
        alist.add(a);
        Account b= new Account();
        b.name='test test';
        alist.add(b);
        insert alist;
        idset.add(a.id);
        idset.add(b.id);
        list<Contact> clist = new list<Contact>();
        clist= [select id from contact where AccountId in :idset ];
        System.assertEquals(2, clist.size(), 'check the 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.
Parmeshwar Bhore 1010011Parmeshwar Bhore 1010011
Thanks Anutej..
This was selected as the best answer
ANUTEJANUTEJ (Salesforce Developers) 
I would really appreciate it if you could choose my answer with the code as the best answer so that it can be useful for others in the future.