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
sgsssgss 

test class required for trigger on contact

Problem:
Match the contact email field of newly created contact to the website field of ACCOUNT  and assign that contact to account using trigger.

////Can anyone provide test class considering all the cases positive negative bulk for this trigger.

-------Trigger------
trigger assign contact on Contact(before insert, before update){
  assignContact.triggerClass(Trigger.New);
}

--------related class-------------
public class assignContact {
    
    public static void triggerClass(List<Contact> ContactList) {
        Map<String, List<Contact>> domainVsContactMap = new Map<String, List<Contact>>();
        
        for(Contact c: ContactList) {
            if(c.Email != null) {
                List<String> domainsplit=c.email.split('@',2);
                String domain=domainsplit[1];
                                system.debug('Domain---->>>: '+domain);

                if(domainVsContactMap.containsKey(domain)) {
                    List<Contact>temp=domainVsContactMap.get(domain);
                    
                    temp.add(c);
                    domainVsContactMap.put(domain, temp);
                }
                else {
                    domainVsContactMap.put(domain, new List<Contact>{c});
                }
            }    
        }
        
        if(!domainVsContactMap.isEmpty()) {
            system.debug('Not Empty');
            List<Account> accList=new List<Account>();
            accList=[SELECT Id, Website FROM Account WHERE Website In:domainVsContactMap.keySet()];
            system.debug('accList'+accList);
            for(Account acc : accList) {
                system.debug('Account: '+acc);
                List<Contact>conList=new List<Contact>();
                conlist=domainVsContactMap.get(acc.Website);
                for(Contact c : conList) {
                                    system.debug('Contact: '+ c);

                    c.AccountId = acc.Id;
                }
            }
        }
    }
}

 
Raj VakatiRaj Vakati
@isTest
public with sharing class ContactTestClass {
	@isTest
	public static void runTest(){
	
	 String orgId = UserInfo.getOrganizationId(); 
        // create system admin user
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        User adminUser=new User(firstname = 'admin', 
                         lastName = 'User', 
                         email =  'admin@test'  + orgId + '.org', 
                         Username = 'admin@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1',
                         TimeZoneSidKey = 'America/Los_Angeles',
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         Alias = 'admin', 
                         ProfileId = pf.Id
                        );  
        insert adminUser;
        
		
		System.runAs(adminUser){
	Account acc = new Account();
	acc.Name ='Test';
	acc.Website='test.com';
	insert acc ; 
	
		String firstname = 'first';
		String lastname = 'last';
		String email = 'firstlast@test.com';
		
		//Create contact
        Contact c = new Contact(firstname=firstname, lastname=lastname, email=email);
		c.AccountId =acc.Id ;
        insert c;
		
		//Create contact
        Contact c1 = new Contact(firstname=firstname, lastname=lastname, email=email);
		c1.AccountId =acc.Id ;
        insert c1;
        
        }
       
	}
}

 
sgsssgss
Can you please provide separate test data factory and separate testClass??
Akshay_DhimanAkshay_Dhiman
Hi Supriya,

Try the Below code.
*************************************   class   *********************************
 
public class assignContact {
    
    public static void triggerClass(List<Contact> ContactList)
    {
        Map<String, List<Contact>> domainVsContactMap = new Map<String, List<Contact>>();
        
        for(Contact c: ContactList) {
            if(c.Email != null) {
                List<String> domainsplit=c.email.split('@',2);
                String domain=domainsplit[1];
                system.debug('Domain---->>>: '+domain);
                
                if(domainVsContactMap.containsKey(domain)) {
                    List<Contact> temp=domainVsContactMap.get(domain);
                    
                    temp.add(c);
                    domainVsContactMap.put(domain, temp);
                }
                else {
                    domainVsContactMap.put(domain, new List<Contact>{c});
                }
            }    
        }
        
        if(!domainVsContactMap.isEmpty()) 
        {
            system.debug('Not Empty');
            List<Account> accList=new List<Account>();
            accList=[SELECT Id, Website FROM Account WHERE Website In:domainVsContactMap.keySet()];
            system.debug('accList'+accList);
            for(Account acc : accList) {
                system.debug('Account: '+acc);
                List<Contact>conList=new List<Contact>();
                conlist=domainVsContactMap.get(acc.Website);
                for(Contact c : conList) {
                    system.debug('Contact: '+ c);
                    
                    c.AccountId = acc.Id;
                }
            }
        }
    }
}


***************************************** Test Class **************************************
@isTest
public with sharing class assignContact_Test
{
    @isTest
    public static void triggerClass_Test()
    {
        
        List<Account> accList=new List<Account>();
        for(Integer i=0;i<5;i++)
        {
            Account accObj=new Account();
            accObj.name='Sandy'+i;
            accObj.Website='gmail.com';
            accList.add(accObj); 
        }
        insert accList;
        List<Contact> cponList=new List<Contact>();
        for(Integer i=0;i<5;i++)
        {
            Contact conObj=new Contact();
            conObj.lastname='Sharma'+i;
            conObj.Email='abcds@gmail.com';
            cponList.add(conObj); 
        }
        insert cponList;
        Test.startTest();
        assignContact.triggerClass(cponList);
        Test.stopTest();
        
    }
}


Thanks.
Akshay