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
Allen2Allen2 

Here is the apex class, writing a test class for the same but not able to cover most part. Please, could anyone help me out?

APEX CLASS

Public Class P_AccountTrigOperations{
    
    public static void upsertLocationOnAccount(List<Account> accountList){
        Set<id> accountIdSet =  new Set<id>();
        Map <id,S_Site__c> locationByAccountMap = new Map<id,S_Site__c>();
        List<S_Site__c> locationToUpdateList = new List<S_Site__c>();
        List<S_Site__c> locationToInsertList = new List<S_Site__c>();
        List<Account> AccountFilteredList=new List<Account>();
       
        List<S_Org_Region_and_Country__c> settingValues = S_Org_Region_and_Country__c.getall().values();
        Set<String> CountrySet = new Set<String>();
        for(S_Org_Region_and_Country__c settings : settingValues){
            if(settings.Application__C == 'GP')
            CountrySet.add(settings.Country__c);
        } 
        
        for(Account accObj : accountList){
            if(CountrySet.contains(accObj.BillingCountry)){
                    accountIdSet.add(accObj.id);
                    AccountFilteredList.add(accObj);
            }
        }
        
        for(S_Site__c locationObj :[Select Name,PX_COUNTRY_CODE__c,PX_CUSTOMER_ACCOUNT_GROUP__c,PX_DMT_AVAIL_HOURS__c,PX_LOCATION_ACCOUNT_ID__c,
                                        S_Account__c,S_City__c,S_Country__c,S_IsPartnerRecord__c,S_Latitude__c,S_Longitude__c,S_Site_Fax__c,S_Site_Phone__c,
                                        S_State__c,S_Street__c,S_Web_site__c,S_Zip__c,CurrencyCode from S_Site__c where S_Account__c in:accountIdSet]){
            locationByAccountMap.put(locationObj.S_Account__c,locationObj);
        }
        
        for(Account accObj : AccountFilteredList){
            if(!locationByAccountMap.isEmpty()){
                if(locationByAccountMap.containsKey(accObj.id)){
                    S_Site__c locToInsertOrUpdate = locationByAccountMap.get(accObj.id);
                    if(locToInsertOrUpdate!=null){
                        //assign values from account to location after confirming the matching fields on account
                        if(accObj.CurrencyCode!=null) locToInsertOrUpdate.CurrencyIsoCode    = accObj.CurrencyCode;
                        if(accObj.Name!=null) locToInsertOrUpdate.Name = accObj.Name;
                        if(accObj.PX_COUNTRY_CODE__c!=null) locToInsertOrUpdate.PX_COUNTRY_CODE__c    = accObj.PX_COUNTRY_CODE__c;
                        if(accObj.PX_CUSTOMER_ACCOUNT_GROUP__c!=null) locToInsertOrUpdate.PX_CUSTOMER_ACCOUNT_GROUP__c = accObj.PX_CUSTOMER_ACCOUNT_GROUP__c;

                        if(accObj.id!=null) locToInsertOrUpdate.S_Account__c = accObj.id;
                        if(accObj.BillingCity!=null) locToInsertOrUpdate.S_City__c = accObj.BillingCity;
                        if(accObj.BillingCountry!=null) locToInsertOrUpdate.S_Country__c =   accObj.BillingCountry;
                        if(accObj.S_Latitude__c!=null) locToInsertOrUpdate.S_Latitude__c = accObj.S_Latitude__c;
                        if(accObj.S_Longitude__c!=null) locToInsertOrUpdate.S_Longitude__c = accObj.S_Longitude__c;
                        if(accObj.PX_Fax__c!=null) locToInsertOrUpdate.S_Site_Fax__c = accObj.PX_Fax__c;
                        if(accObj.PX_Phone__c!=null) locToInsertOrUpdate.S_Site_Phone__c = accObj.PX_Phone__c;
                        if(accObj.BillingStreet!=null) locToInsertOrUpdate.S_Street__c = accObj.BillingStreet;

                        if(accObj.BillingPostalCode!=null) locToInsertOrUpdate.S_Zip__c =    accObj.BillingPostalCode;
                        locationToUpdateList.add(locToInsertOrUpdate);
                    }
                }
            }
            else{
                //assign values from account to location after confirming the matching fields on account
                S_Site__c locToInsertOrUpdate2 = new S_Site__c();
                if(accObj.CurrencyCode!=null) locToInsertOrUpdate2.CurrencyIsoCode   = accObj.CurrencyCode;
                if(accObj.Name!=null) locToInsertOrUpdate2.Name = accObj.Name;
                if(accObj.PX_COUNTRY_CODE__c!=null) locToInsertOrUpdate2.PBSMAX_COUNTRY_CODE_ISO2__c   = accObj.PBSMAX_COUNTRY_CODE_ISO2__c;
                if(accObj.PX_CUSTOMER_ACCOUNT_GROUP__c!=null) locToInsertOrUpdate2.PBSMAX_CUSTOMER_ACCOUNT_GROUP__c = accObj.PBSMAX_CUSTOMER_ACCOUNT_GROUP__c;

                if(accObj.id!=null) locToInsertOrUpdate2.S_Account__c =    accObj.id;
                if(accObj.BillingCity!=null) locToInsertOrUpdate2.S_City__c = accObj.BillingCity;
                if(accObj.BillingCountry!=null) locToInsertOrUpdate2.S_Country__c =  accObj.BillingCountry;
                if(accObj.S_Latitude__c!=null) locToInsertOrUpdate2.S_Latitude__c = accObj.S_Latitude__c;
                if(accObj.S_Longitude__c!=null) locToInsertOrUpdate2.S_Longitude__c = accObj.S_Longitude__c;
                if(accObj.PX_Fax__c!=null) locToInsertOrUpdate2.S_Site_Fax__c = accObj.PX_Fax__c;
                if(accObj.PX_Phone__c!=null) locToInsertOrUpdate2.S_Site_Phone__c =    accObj.PX_Phone__c;
                if(accObj.BillingStreet!=null) locToInsertOrUpdate2.S_Street__c =    accObj.BillingStreet;

                if(accObj.BillingPostalCode!=null) locToInsertOrUpdate2.S_Zip__c =   accObj.BillingPostalCode; 
                locationToInsertList.add(locToInsertOrUpdate2);
            }
        }
        
        if(!locationToUpdateList.isEmpty()){
            try{
                update locationToUpdateList;
            }
         catch(Exception e){
                    System.debug('@@@Exception'+e);
                }
         }
        if(!locationToInsertList.isEmpty()){
            try{
            insert locationToInsertList;
            }
             catch(Exception e){
                    System.debug('@@@Exception'+e);
                }

    }
}
}


TEST CLASS


@isTest (Seealldata =False)
public class P_AccountTrigOperationsTest{


   public static  Account createAccount (){
    Account acc=new Account();
    acc.Name = 'Test Account';
    acc.AccountNumber = 'T06';
    acc.Customer_Status__c = 'Active';
    acc.BillingCountry = 'Canada';
    acc.CurrencyIsoCode = 'USD';
    acc.Customer_Status__c = 'Active';
    acc.PX_COUNTRY_CODE__c='IM';
    acc.PX_CUSTOMER_ACCOUNT_GROUP__c='IIN';
    acc.S_Latitude__c=34.999999;
    acc.S_Longitude__c=09.989898;
    acc.PX_Fax__c='PBITR';
    acc.PX_Phone__c='PBI';
    acc.BillingPostalCode='MH';
    return acc;
    }
        
static testMethod void upsertLocationOnAccountTest(){

Test.startTest();
List<Account> acclist = new List<Account>();         
Account acc = createAccount ();
acclist.add(acc);
insert acclist;

        S_Site__c Location=new S_Site__c();
        Location.Name='TestPb';
        Location.S_Street__c='270/8 KC';
        Location.S_Zip__c='1234';
        Location.S_Country__c='Algeria';
        Location.S_Longitude__c=98.000000;
        Location.S_Latitude__c=97.999999;
        Location.S_City__c='PB';
        Location.S_State__c='PB';
        Location.S_Account__c  = acc.id;
        insert Location;
S_Site__c Loc= [SELECT Id,S_Account__c  FROM S_Site__c WHERE S_Account__c  =:acc.id LIMIT 1];
   
System.assertEquals(acc.Id,Loc.S_Account__c  );

acc.Name = 'Test Account1';
update acc;

P_AccountTrigOperations ato = new P_AccountTrigOperations();
P_AccountTrigOperations.upsertLocationOnAccount(acclist);
Location.S_Account__c  = null;
update Location;
P_AccountTrigOperations.upsertLocationOnAccount(acclist);
Test.stopTest();
}
}

The bold part is anable to cover. please help me out
Steven NsubugaSteven Nsubuga
You have not created any custom settings for use, and so locationByAccountMap is empty, that is why. Try this, and make any necessary changes
@isTest (Seealldata =False)
public class P_AccountTrigOperationsTest{


	public static  Account createAccount (){
		Account acc=new Account();
		acc.Name = 'Test Account';
		acc.AccountNumber = 'T06';
		acc.Customer_Status__c = 'Active';
		acc.BillingCountry = 'Canada';
		acc.CurrencyIsoCode = 'USD';
		acc.Customer_Status__c = 'Active';
		acc.PX_COUNTRY_CODE__c='IM';
		acc.PX_CUSTOMER_ACCOUNT_GROUP__c='IIN';
		acc.S_Latitude__c=34.999999;
		acc.S_Longitude__c=09.989898;
		acc.PX_Fax__c='PBITR';
		acc.PX_Phone__c='PBI';
		acc.BillingPostalCode='MH';
		return acc;
    }
        
	static testMethod void upsertLocationOnAccountTest(){

		// Create custom setting for use in testing
		S_Org_Region_and_Country__c> settings = new S_Org_Region_and_Country__c();
		settings.Application__C = 'GP';
		settings.Country__c = 'Canada';
		settings.Name = 'Something';
		insert settings;
		
		Test.startTest();
		List<Account> acclist = new List<Account>();         
		Account acc = createAccount ();
		acclist.add(acc);
		insert acclist;

				S_Site__c Location=new S_Site__c();
				Location.Name='TestPb';
				Location.S_Street__c='270/8 KC';
				Location.S_Zip__c='1234';
				Location.S_Country__c='Algeria';
				Location.S_Longitude__c=98.000000;
				Location.S_Latitude__c=97.999999;
				Location.S_City__c='PB';
				Location.S_State__c='PB';
				Location.S_Account__c  = acc.id;
				insert Location;
		S_Site__c Loc= [SELECT Id,S_Account__c  FROM S_Site__c WHERE S_Account__c  =:acc.id LIMIT 1];
		   
		System.assertEquals(acc.Id,Loc.S_Account__c  );

		acc.Name = 'Test Account1';
		update acc;

		P_AccountTrigOperations ato = new P_AccountTrigOperations();
		P_AccountTrigOperations.upsertLocationOnAccount(acclist);
		Location.S_Account__c  = null;
		update Location;
		P_AccountTrigOperations.upsertLocationOnAccount(acclist);
		Test.stopTest();
	}
}


 
Allen2Allen2
Thanks Steven. Could we do something to cover the catch exception  part.
Steven NsubugaSteven Nsubuga
After looking at your code, I believe that it is not necessary for you to use the try catch sections.
There is nothing specific that you are doing in the catch, so just remove it. 
Public Class P_AccountTrigOperations{
    
    public static void upsertLocationOnAccount(List<Account> accountList){
        Set<id> accountIdSet =  new Set<id>();
        Map <id,S_Site__c> locationByAccountMap = new Map<id,S_Site__c>();
        List<S_Site__c> locationToUpdateList = new List<S_Site__c>();
        List<S_Site__c> locationToInsertList = new List<S_Site__c>();
        List<Account> AccountFilteredList=new List<Account>();
       
        List<S_Org_Region_and_Country__c> settingValues = S_Org_Region_and_Country__c.getall().values();
        Set<String> CountrySet = new Set<String>();
        for(S_Org_Region_and_Country__c settings : settingValues){
            if(settings.Application__C == 'GP')
            CountrySet.add(settings.Country__c);
        } 
        
        for(Account accObj : accountList){
            if(CountrySet.contains(accObj.BillingCountry)){
                    accountIdSet.add(accObj.id);
                    AccountFilteredList.add(accObj);
            }
        }
        
        for(S_Site__c locationObj :[Select Name,PX_COUNTRY_CODE__c,PX_CUSTOMER_ACCOUNT_GROUP__c,PX_DMT_AVAIL_HOURS__c,PX_LOCATION_ACCOUNT_ID__c,
                                        S_Account__c,S_City__c,S_Country__c,S_IsPartnerRecord__c,S_Latitude__c,S_Longitude__c,S_Site_Fax__c,S_Site_Phone__c,
                                        S_State__c,S_Street__c,S_Web_site__c,S_Zip__c,CurrencyCode from S_Site__c where S_Account__c in:accountIdSet]){
            locationByAccountMap.put(locationObj.S_Account__c,locationObj);
        }
        
        for(Account accObj : AccountFilteredList){
            if(!locationByAccountMap.isEmpty()){
                if(locationByAccountMap.containsKey(accObj.id)){
                    S_Site__c locToInsertOrUpdate = locationByAccountMap.get(accObj.id);
                    if(locToInsertOrUpdate!=null){
                        //assign values from account to location after confirming the matching fields on account
                        if(accObj.CurrencyCode!=null) locToInsertOrUpdate.CurrencyIsoCode    = accObj.CurrencyCode;
                        if(accObj.Name!=null) locToInsertOrUpdate.Name = accObj.Name;
                        if(accObj.PX_COUNTRY_CODE__c!=null) locToInsertOrUpdate.PX_COUNTRY_CODE__c    = accObj.PX_COUNTRY_CODE__c;
                        if(accObj.PX_CUSTOMER_ACCOUNT_GROUP__c!=null) locToInsertOrUpdate.PX_CUSTOMER_ACCOUNT_GROUP__c = accObj.PX_CUSTOMER_ACCOUNT_GROUP__c;

                        if(accObj.id!=null) locToInsertOrUpdate.S_Account__c = accObj.id;
                        if(accObj.BillingCity!=null) locToInsertOrUpdate.S_City__c = accObj.BillingCity;
                        if(accObj.BillingCountry!=null) locToInsertOrUpdate.S_Country__c =   accObj.BillingCountry;
                        if(accObj.S_Latitude__c!=null) locToInsertOrUpdate.S_Latitude__c = accObj.S_Latitude__c;
                        if(accObj.S_Longitude__c!=null) locToInsertOrUpdate.S_Longitude__c = accObj.S_Longitude__c;
                        if(accObj.PX_Fax__c!=null) locToInsertOrUpdate.S_Site_Fax__c = accObj.PX_Fax__c;
                        if(accObj.PX_Phone__c!=null) locToInsertOrUpdate.S_Site_Phone__c = accObj.PX_Phone__c;
                        if(accObj.BillingStreet!=null) locToInsertOrUpdate.S_Street__c = accObj.BillingStreet;

                        if(accObj.BillingPostalCode!=null) locToInsertOrUpdate.S_Zip__c =    accObj.BillingPostalCode;
                        locationToUpdateList.add(locToInsertOrUpdate);
                    }
                }
            }
            else{
                //assign values from account to location after confirming the matching fields on account
                S_Site__c locToInsertOrUpdate2 = new S_Site__c();
                if(accObj.CurrencyCode!=null) locToInsertOrUpdate2.CurrencyIsoCode   = accObj.CurrencyCode;
                if(accObj.Name!=null) locToInsertOrUpdate2.Name = accObj.Name;
                if(accObj.PX_COUNTRY_CODE__c!=null) locToInsertOrUpdate2.PBSMAX_COUNTRY_CODE_ISO2__c   = accObj.PBSMAX_COUNTRY_CODE_ISO2__c;
                if(accObj.PX_CUSTOMER_ACCOUNT_GROUP__c!=null) locToInsertOrUpdate2.PBSMAX_CUSTOMER_ACCOUNT_GROUP__c = accObj.PBSMAX_CUSTOMER_ACCOUNT_GROUP__c;

                if(accObj.id!=null) locToInsertOrUpdate2.S_Account__c =    accObj.id;
                if(accObj.BillingCity!=null) locToInsertOrUpdate2.S_City__c = accObj.BillingCity;
                if(accObj.BillingCountry!=null) locToInsertOrUpdate2.S_Country__c =  accObj.BillingCountry;
                if(accObj.S_Latitude__c!=null) locToInsertOrUpdate2.S_Latitude__c = accObj.S_Latitude__c;
                if(accObj.S_Longitude__c!=null) locToInsertOrUpdate2.S_Longitude__c = accObj.S_Longitude__c;
                if(accObj.PX_Fax__c!=null) locToInsertOrUpdate2.S_Site_Fax__c = accObj.PX_Fax__c;
                if(accObj.PX_Phone__c!=null) locToInsertOrUpdate2.S_Site_Phone__c =    accObj.PX_Phone__c;
                if(accObj.BillingStreet!=null) locToInsertOrUpdate2.S_Street__c =    accObj.BillingStreet;

                if(accObj.BillingPostalCode!=null) locToInsertOrUpdate2.S_Zip__c =   accObj.BillingPostalCode; 
                locationToInsertList.add(locToInsertOrUpdate2);
            }
        }
        
        if(!locationToUpdateList.isEmpty()){
           
                update locationToUpdateList;
           
         }
        if(!locationToInsertList.isEmpty()){
            
            insert locationToInsertList;
             
    }
}
}