0

I'm looking to see if anyone could provide any pointers on the test class I'm trying to write for the following batch class:

public with sharing class TerritoryReassignmentAccounts implements Database.Batchable<SObject> {
    
    // flow calls method and passes to it a list of instances of TerritoryReassignmentVariables class
    // Iterates through the list and make a batch call to itself
    @InvocableMethod(label = 'Territory Reassignment')
    public static void updateAccounts(List<TerritoryReassignmentVariables> IDValues){
        for(TerritoryReassignmentVariables itemFromFlow: IDValues) {
            Database.executeBatch(new TerritoryReassignmentAccounts(itemFromFlow));
        }
    }
    // State to update records //
    TerritoryReassignmentVariables item;
    TerritoryReassignmentAccounts(TerritoryReassignmentVariables itemParameter) {
        this.item = itemParameter;
    }
    public Database.QueryLocator start(Database.BatchableContext ctx) {
        return Database.getQueryLocator([SELECT Id FROM Account WHERE X2020_New_Territory_Text__c = :item.old2020NTT]);
    }

    // Queueable Context execution //
    public void execute(Database.BatchableContext ctx, Account[] scope) {
        for(Account record: scope) {
            record.OwnerId = item.newOwnerId;
            record.sundog_deprm2__Territory__c = item.newTerritoryID;
            record.X2020_New_Territory_Text__c = item.new2020NTT;
        }
        update scope;
    }
    public void finish (Database.BatchableContext BC){
        Database.executeBatch(new TerritoryReassignInstitutions(item));
    }
}

Not sure how useful this will be, but the "updateAccounts" method parameter in the above is coming from this class, whose variable values are being set via a flow:

public with sharing class TerritoryReassignmentVariables {
    
    @InvocableVariable
    public String oldOwnerId;

    @InvocableVariable
    public String newOwnerId;

    @InvocableVariable
    public String oldTerritoryID;

    @InvocableVariable
    public String newTerritoryID;

    @InvocableVariable
    public String old2020NTT;

    @InvocableVariable
    public String new2020NTT;

    @InvocableVariable
    public String newVendorAcctID;

    @invocableVariable
    public String stateAbbreviations;

}

And here's my attempt at the test class. I've tried a lot of different attempts and the error that pops up most often is "Invalid type: TerritoryReassignmentAccounts.updateAccounts". I think the String List I'm creating and then using as the parameter for the test is incorrect but I'm not sure what else to try. Am I missing something obvious?

@isTest 
global class AccountUpdateBatchJobTest {
    static testMethod void testMethod1() {
    String oldOwnerId = '0052h0000018iqH'; //test user2
    String newOwnerId = '0052h0000018iq2'; //test user1
    String oldTerritoryID = 'a0C2h000001K4MW'; //test user2
    String newTerritoryID = 'a0C2h000001K4MR'; //test user1
    String old2020NTT = 'AM-Coverage'; 
    String new2020NTT = 'Testing1234'; 
    String newVendorAcctID = '0012h00000McL4E'; //test user1
    String stateAbbreviations = 'NC,FL'; 

    List<String> IdValues = new List<String>();
    IdValues.add(oldOwnerId);
    IdValues.add(newOwnerId);
    IdValues.add(oldTerritoryID);
    IdValues.add(newTerritoryID);
    IdValues.add(old2020NTT);
    IdValues.add(new2020NTT);
    IdValues.add(newVendorAcctID);
    IdValues.add(stateAbbreviations);


        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++) {
            Account acc = new Account();
            acc.Name ='Name'+i;
            acc.X2020_New_Territory_Text__c = 'Testing1234';
            lstAccount.add(acc);
        }
        
        insert lstAccount;
        
        Test.startTest();

            TerritoryReassignmentAccounts obj = new TerritoryReassignmentAccounts.updateAccounts(IdValues);
            Database.executeBatch(obj); 
        
        Test.stopTest();
    }
}

1 Answer 1

0

TerritoryReassignmentAccounts.updateAccounts is expecting an input variable of type List<TerritoryReassignmentVariables>, but in your test class you are passing a List<String>. Replace this code in you test class

List<String> IdValues = new List<String>();
IdValues.add(oldOwnerId);
IdValues.add(newOwnerId);
IdValues.add(oldTerritoryID);
IdValues.add(newTerritoryID);
IdValues.add(old2020NTT);
IdValues.add(new2020NTT);
IdValues.add(newVendorAcctID);
IdValues.add(stateAbbreviations);

with the following:

List<TerritoryReassignmentVariables> trvList = new List<TerritoryReassignmentVariables>();
TerritoryReassignmentVariables trv = new TerritoryReassignmentVariables();
trv.oldOwnerId = oldOwnerId;
trv.newOwnerId = newOwnerId;
trv.oldTerritoryID = oldTerritoryID;
trv.newTerritoryID = newTerritoryID;
trv.old2020NTT = old2020NTT;
trv.new2020NTT = new2020NTT;
trv.newVendorAcctID = newVendorAcctID;
trv.stateAbbreviations = stateAbbreviations;
trvList.add(trv);

And, replace this code

TerritoryReassignmentAccounts obj = new TerritoryReassignmentAccounts.updateAccounts(IdValues);
Database.executeBatch(obj); 

with the following:

 TerritoryReassignmentAccounts.updateAccounts(trvList);
6
  • Thanks arut. Unfortunately I'm still getting the following error: "Invalid type: TerritoryReassignmentAccounts.updateAccounts". I also tried initiating test via the following line but this didn't work either: "Database.executeBatch(new TerritoryReassignmentAccounts.updateAccounts(trvList));". Is there anything else you think I could try?
    – mcg123
    Commented Aug 14, 2020 at 12:10
  • updateAccounts is a static method and you are trying to access it as an instance method. I missed to notice this earlier. I've updated my answer. Please check now.
    – arut
    Commented Aug 14, 2020 at 12:28
  • Also, you don't need the Database.executeBatch call in the test class [nor you should be passing this instance 'new TerritoryReassignmentAccounts.updateAccounts(trvList)']. Because the static method itself is executing the Database.executeBatch for each entry in the List collection passed to it.
    – arut
    Commented Aug 14, 2020 at 12:33
  • Thank you arut! That saved and the test ran successfully. It's only getting 57% coverage though. Nothing in the execute method of the batch was covered. I know you mentioned not needing the Database.executeBatch call in the test class, but do you think I do in fact need it since it's not getting any coverage?
    – mcg123
    Commented Aug 14, 2020 at 13:28
  • No, you don't need to. All the test account data you have created have the value 'Testing1234' for the field acc.X2020_New_Territory_Text__c, but the Database.getQueryLocator is trying to compare this field with the value 'AM-Coverage'. Since, no such account records are found, the execute method is not getting any code overage. Set up the test account data with the value 'AM-Coverage' and you should get increased code coverage.
    – arut
    Commented Aug 14, 2020 at 13:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .