[SalesForce] Constructor not defined on Global Batch Class

I am receiving the following error.

Constructor not defined: [DynamicBatchApex].(String, String, Map, String) (Line: 9, Column: 30)

If you look at my DynamicBatchApex class, I clearly have the constructor outlined.

Also, what are the positives and downsides of using the global header?

global class NumberOfEmailsSchedulable implements Schedulable
{
      global void execute(SchedulableContext sc) 
      { 
    String query1 = 'SELECT accountId accountId, count( Id) result FROM contact WHERE accountid != null GROUP BY accountId';
    Map<String, Object> field_value_pairs = new Map<String, Object>{'Number_Of_Contacts__c' => 0};
    DynamicBatchApex batch1     =   new DynamicBatchApex('accountId', 'result', field_value_pairs, query1);

     }
}

Batch Apex Class

global class DynamicBatchApex implements Database.Batchable<AggregateResult> {

public String sObjectIdKey;
public String sObjectResultKey;
public Map<String, Object> sObjectFieldsToUpdate;
public String query;


global DynamicBatchApex(String arIdKey, String arResultKey, Map<String, Object> field_value_pairs, String fieldName, String soqlQuery ) 
{


    sObjectIdKey            = arIdKey;
    sObjectResultKey        = arResultKey;

    sObjectFieldsToUpdate   = field_value_pairs;
    query                   = soqlQuery;
}

global Iterable<AggregateResult> start(Database.BatchableContext BC) 
{
    return new BulkIterable(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope) 
{

    DynamicSObjectUpdater sObjectUpdater = new DynamicSObjectUpdater();

    for(sObject sObj: scope)
    {
        AggregateResult ar      = (AggregateResult)sObj;
        ID sObjectId            = (ID)ar.get(sObjectIdKey);
        Decimal arResult        = (Decimal)ar.get(sObjectResultKey);

        sObjectUpdater.getUpdateSObject(sObjectId, sObjectFieldsToUpdate);
    }

    sObjectUpdater.updateSObjects();


}

global void finish(Database.BatchableContext BC) 
{

}
}

Best Answer

You have the constructor as below, which takes 5 arguments.

global DynamicBatchApex(String arIdKey, String arResultKey, 
                        Map<String, Object> field_value_pairs, 
                        String fieldName, String soqlQuery ) 

Whereas you are constructing the class as below passing only 4 arguments:

DynamicBatchApex batch1 = 
    new DynamicBatchApex('accountId', 'result', field_value_pairs, query1);

and that there is no such constructor defined which accepts 4 arguments.

Depending on the context, you have two options to correct this:

  • Introduce a 4 args constructor
  • Pass appropriate or null value for other missing argument
Related Topic