+ Start a Discussion
John Neilan 2John Neilan 2 

Error for Test Class on Controller

Hello,

I have a pretty simple test class on a controller, which worked fine in my developer environment. However, I am now moving it to a sandbox and I'm running into an error:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of BeforeInsert caused by: System.NullPointerException: Argument cannot be null.
Class.TriggerHandlerAccount.mapRegions: line 94, column 1
Class.TriggerHandlerAccount.beforeInsert: line 19, column 1
Trigger.AccountTrigger: line 6, column 1:


I know it's being caused by a trigger class that was created by a vendor for my organization, but I can'f figure out what I need to change in my test classes to avoid the error. Can anyone help?

Test Class for Controller:
@Istest
private class TestControllerShortForm
{
    static testMethod void testSFController1()
    {    
        Account acct1 = TestCreateRecords.createAcct(0);
        insert acct1;

        Opportunity opp1 = TestCreateRecords.createOppNewSF(acct1.Id);
        insert opp1;

        Short_Form__c ESF1 = new Short_Form__c();
            ESF1.Sales_Representative__c = acct1.AM_owner__c;
            ESF1.Opportunity__c = opp1.Id;
            ESF1.Partner_Contact__c = '00337000006fBvQ';
            ESF1.Term_Length__c = 12;
            ESF1.Term_Metric__c = 'Months';
            ESF1.Additional_Terms__c = 'Test Test Test';

        ApexPages.StandardController SF1 = new ApexPages.standardController(opp1);
        VF_ShortFormController SFCont1 = new VF_ShortFormController(SF1);
        SFCont1.ESF.add(ESF1);
        SFCont1.ShortForm();
        SFCont1.save();

    }
}
Class Creating Records for Test Class:
// create and insert a new Account record.
    public static Account createAcct(Integer i){ 

    Account acct = new Account();
        acct.Name = 'Test' + i;
        acct.Language__c = 'English';
        acct.Industry = 'Automotive';
        acct.CurrencyIsoCode = 'USD';
        acct.Legal_Name__c = 'Test' + i;
        acct.Region__c = 'NA-US-NE';
        acct.BillingCity = 'New York';
        acct.BillingState = 'New York';
        acct.BillingCountry = 'United States';

    return acct;

    }


// create and insert a new Opportunity record that has the newly created Account record as its master record.
    public static Opportunity createOppNew (Id acctId){ 
        Opportunity opp2 = new Opportunity();
            opp2.AccountId = acctId;
            opp2.Name = 'Test Opportunity - ';
            opp2.StageName = System.Label.Eng_OppStage3;
            opp2.CloseDate = date.newinstance(2020,1,31);
            opp2.Amount = 1000;
            opp2.Region__c = 'NA-US-NE';
            opp2.Type = 'New Customer';
            opp2.Website_s__c = 'www.test.com; www.test2.com; www.test3.com';
        return opp2;
    }

// create and insert a new Opportunity record that has the newly created Account record as its master record. Used for Short Form.
    public static Opportunity createOppNewSF (Id acctId){ 
        Opportunity opp2a = new Opportunity();
            opp2a.AccountId = acctId;
            opp2a.Name = 'Test Opportunity - ';
            opp2a.StageName = System.Label.Eng_OppStage3;
            opp2a.CloseDate = date.newinstance(2020,1,31);
            opp2a.Amount = 1000;
            opp2a.Region__c = 'NA-US-NE';
            opp2a.Type = 'New Customer';
            opp2a.Website_s__c = 'www.test.com; www.test2.com; www.test3.com';
        return opp2a;
    }
}

Class Throwing Error:​
public with sharing class TriggerHandlerAccount {
    private boolean isExecuting = false;

    public TriggerHandlerAccount(boolean initIsExecuting) {
        isExecuting = initIsExecuting;
    }

    public void beforeInsert(List<Account> newAccounts) {
        setType(newAccounts);
        mapRegions(newAccounts);
        UpdateEnglishName(newAccounts, null);
    }

    public void beforeUpdate(List<Account> newAccounts, map<id, Account> oldAccountMap) {
        mapRegions(newAccounts);
        UpdateEnglishName(newAccounts, oldAccountMap);
    }

    public void afterInsert(List<Account> newAccounts) {
        updateRegionField(newAccounts, 'isInsert', newAccounts.size());
        accountHierarchyLeadSourceCalculation(new List<Account>(), newAccounts, 'isInsert', newAccounts.size());
    }


    private void UpdateEnglishName (List<Account> newAccounts, map<id, Account> oldAccountMap){
        for (Account account : newAccounts) {
            system.debug('oldAccountMap = ' + oldAccountMap);
            system.debug('oldAccountMap = ' + account.English_Account_Name__c);
            if ((oldAccountMap == null && account.English_Account_Name__c == null) ||
            (oldAccountMap != null && account.Name != oldAccountMap.get(account.id).Name && account.English_Account_Name__c == oldAccountMap.get(account.id).English_Account_Name__c))
            FillUpEnglishName(account);
        }
    }

    private void FillUpEnglishName (Account account) {
        if (account.Name == null || account.Name.trim() == '') return;
        String[] chars = account.Name.split('');
        // the 1st element in an Apex '' split is garbage; remove it:
        chars.remove(0);
        System.debug(chars);
        // change a char:
        string SC = SpecialCharacters__c.getInstance().Characters__c;
        boolean english = true;
        for (String c : chars) {
            if (!c.containsAny(SC))
                english = false;
        }
        system.debug('english = ' + english);
        if (english)
            account.English_Account_Name__c = account.Name;
    }


    private void setType(List<Account> newAccounts) {
        // If the Account is created with no Type but was converted from a Lead that had
        //    a Lead Type, set the Type from the Lead Type

        for (Account newAccount: newAccounts) {
            if (newAccount.Lead_Type__c != Null && newAccount.Type == Null) {
                newAccount.Type = newAccount.Lead_Type__c;
            }
        }
    }

    private void mapRegions(List<Account> newAccounts) {
        AccountRegionManager regionManager = new AccountRegionManager();
        Region_Mapper_Settings__c errorMessage = Region_Mapper_Settings__c.getInstance();
        for (Account newAccount: newAccounts) {
            String region = regionManager.getRegion(newAccount.BillingCountry, newAccount.BillingState);
            if (region != null) {
                newAccount.Region__c = region;
            } else {
                newAccount.addError(errorMessage.Invalid_Mapping_Message__c);
            }
        }
    }

    public static Boolean accountHierarchyLeadSourceCalculationEnable = true; 

     private void updateRegionField(List<Account> newAccounts, String triggerEvent, Integer triggerSize) {

        Boolean needUpdateRegion = false;
        map<Id , Account> accountsmap =  new map<Id, Account>();
        List<Account> accounts = new List<Account>();

         for(Integer i = 0; i < triggerSize; i++){
            Account tempaccount = new Account(id = newAccounts[i].id);
            if(triggerEvent == 'isInsert') 
                needUpdateRegion = newAccounts[i].Region__c == '' || newAccounts[i].Region__c == ' ' || newAccounts[i].Region__c == null;

            if (needUpdateRegion) 
            {
                accountsmap.put(newAccounts[i].OwnerId , newAccounts[i]);
            }
         }

         for(User u : [SELECT Id, Region__c FROM User WHERE Id IN :accountsmap.keyset()]){
                   Account tempaccount = new Account(id = accountsmap.get(u.id).id) ; 
                   tempaccount.Region__c = u.Region__c;
                   system.debug('Region to update: '+ tempaccount.Region__c);
                   accounts.add(tempaccount);
         }

         if (!accounts.isempty())
            update accounts;
     }
}

 
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey,

You need to create custtom setting in Test Class
for this line Region_Mapper_Settings__c errorMessage = Region_Mapper_Settings__c.getInstance();

Some thing like this :
insert new CustomSetting__c(Field__c = 'Value')
Hope this helps

--
Thanks,
Swayam

 
John Neilan 2John Neilan 2
Hi Swayam,

Can you elaborate?  I'm not quote sure what you mean by creating a custom setting.  Thanks,
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

Check this class TriggerHandlerAccount 

Here your are getting 

Region_Mapper_Settings__c errorMessage = Region_Mapper_Settings__c.getInstance(); This from custom setting, which is similar to custom object which you need to create in Test Class as you created the account and other object in Test Data

Hope this helps

--
Thanks,
Swayam
Ansh CoderAnsh Coder
Hi,

This error is causing due to a trigger named 'AccountTrigger' available either in sandbox or production.
Please make sure both have the same trigger with exact same code and also fields of Account object should also be same.

Hope this helps you.

Thanks,
Anand Sharma
John Neilan 2John Neilan 2
Thanks Swayam. I didn't realize that was a custom setting.  I added the line:
 
insert new Region_Mapper_Settings__c(Name='Test');

to my controller test class before the Account creation line, but I still get the errors.  Am I adding that correctly?
John Neilan 2John Neilan 2
Thanks Ansh.  That trigger simply calls the class above that is throwing the error, which is why I did not post the trigger.
Ansh CoderAnsh Coder
@john
Please provide the code or snippet of the trigger.
Thanks