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
shivi Tanwarshivi Tanwar 

facing issue in creating test calss fro handler class

Facing issue in creating test class

My trigger will create custom account  lookup from text field on contact object and if account doesnt exist it will first create account then creates lookup.
I am beginner and i am facing issue in creating test class plz help me out.

Here is my trigger
public class DonarMappingFeildContactHandler {
    
    
    public static void CreatePrimaryAffiliation(List<Contact> conList){
        
        Map<String,Id> conAccExist = new Map<String, Id>();
        Map<String,Contact> mapVal = new Map<String,Contact>();
        Set<String> fieldVal = new Set<String>();
        List<Account> newAccList = new List<Account>(); 
        
        try{
        for(Contact con : conList)
        {  //system.debug('contact detail'+ con);  
         if(con.Primary_affliation_text__c != null) {
           system.debug('check primary aff' + con.Primary_affliation_text__c);
            mapVal.put(con.Primary_affliation_text__c, con);
            fieldVal.add(con.Primary_affliation_text__c);
            system.debug('value entered' + con.Primary_affliation_text__c);
         }
        } 
        if(fieldVal != null && fieldVal.size() > 0)
        {
            for(Account acc: [SELECT Id,Name FROM Account WHERE Name In :fieldVal])
            {
                conAccExist.put(acc.name, acc.Id);
            }
            
            for(String str: fieldVal)
            {
                if(!conAccExist.containsKey(str))
                {
                    Account acc = new Account();
                    acc.Name = str;
                    
                    newAccList.add(acc);
                    
                }
            }system.debug('matc value'+ conAccExist);
            
            if(newAccList != null && newAccList.size() > 0)
            {
                insert newAccList;
             
             for(Account accObj : newAccList)
             {
                 if(!conAccExist.containsKey(accObj.Name)){
                     conAccExist.put(accObj.Name, accObj.Id);
                 }
             }
             system.debug('matc value 2'+ conAccExist);
            }
            
            for(Contact cObj : conList){
                if(String.isNotBlank(cObj.Primary_affliation_text__c)){
                    cObj.npsp__Primary_Affiliation__c = conAccExist.get(cObj.Primary_affliation_text__c);
                }
            }      
        }
       }  
        catch(Exception e){
            system.debug('error in rigger');
        }
    }
    
}

my trigger 
trigger DonarMappingFieldContactTrigger on Contact (before insert) {
    DonarMappingFeildContactHandler.CreatePrimaryAffiliation(Trigger.new);
     
}

my test class
@isTest
public class contactTriggerTest {
    
    public static testmethod void testPrimaryAffliation() {
        List<Account> acctlist = new List<Account>();
        Account acct = new Account(name='acct1');
        insert acctList;
        
        List<Contact> conList = new List<Contact>();
        Contact con = new Contact();
        con.LastName = 'newCont';
        con.npsp__Primary_Affiliation__c = acct.Id;
        con.Primary_affliation_text__c = 'acct1';
        insert conlist;
        
        test.startTest();
        DonarMappingFeildContactHandler.CreatePrimaryAffiliation(conlist);
        test.stopTest();
    }
}

it is giving me only 25% covergae plz help me out
CharuDuttCharuDutt
Hii Shivi Tanwar
Try Below Code Made Some Changes As You Forgot To Add Contact Record In ConList in Test Class  Changes Are In Bold
@isTest
public class contactTriggerTest {
    
    public static testmethod void testPrimaryAffliation() {
        List<Account> acctlist = new List<Account>();
        Account acct = new Account(name='acct1');
        insert acctList;
        
        List<Contact> conList = new List<Contact>();
        Contact con = new Contact();
        con.LastName = 'newCont';
        con.npsp__Primary_Affiliation__c = acct.Id;
        con.Primary_affliation_text__c = 'acct1';

        conlist.add(con);

        insert conlist;
        
        test.startTest();
        DonarMappingFeildContactHandler.CreatePrimaryAffiliation(conlist);
        test.stopTest();
    }
}
Please Mark It As Best Answer If it Helps
Thank You!