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
sam patilsam patil 

help me to write test class on conatct trigger

public static void setEmailDomain(List<Contact> contactNewList) {
        
        Set<Id> accountIds = new Set<Id>();
        
        for(Contact con : contactNewList) {
            if(con.AccountId != null) {
                accountIds.add(con.AccountId);
            }
        }
        
        List<Account> accountList = new List<Account>();
        List<Account> accountToUpdate = new List<Account>();
        
        if(!accountIds.isEmpty()) {
            accountList = [SELECT Id, Email_Domain__c, (SELECT Id, Email FROM Contacts WHERE Email != null) 
                           FROM Account WHERE Id IN : accountIds];
            
            for(Account acc : accountList) {
                String emailDomainString = '';
                
                if(!acc.Contacts.isEmpty()) {
                    for(Contact con : acc.Contacts){
                        if(emailDomainString.contains(con.Email.subStringAfter('@'))) {
                            emailDomainString = emailDomainString + con.Email.subStringAfter('@') + ', ';
                        }
                    }
                    acc.Email_Domain__c = emailDomainString;
                    accountToUpdate.add(acc);
                }
            }
            
            if(!accountToUpdate.isEmpty()) {
                update accountToUpdate;
            }
        }
    }
}
Best Answer chosen by sam patil
CharuDuttCharuDutt
Hii Sam
Try Below Code
@istest
public class setEmailDomaintest2 {
@istest 
    public static void unitTest(){
        Account Acc = new Account();
        Acc.Name = 'Tesst Account';
        insert Acc ;
        Contact con = new Contact();
        con.LastName = 'Test Contact';
        Con.AccountId =Acc.id;
        Con.Email = 'Test123@test.com';
        insert Con;
        setEmailDomaintest.setEmailDomain(new list<Contact>{Con});
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!