3

I've read through some other similar posts, and what seems to usually throw this error (Ex: constructor takes 3 args and you've only given it 2) I don't think is the case here.

My constructor only takes 1 arg, and I'm passing it 1 arg, but still hitting the error.

The full message is:

Constructor not defined: [batchablePartnerPortalShare].Constructor(List)

Any ideas as to what I'm missing?

Batch Class Code

global class batchablePartnerPortalShare implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {
    
    global Integer recordsProcessed = 0;

    global List<String> loanIdsParam;
    global String query;



    global void batchAccess(List<String> inputLoanIds){

        loanIdsParam = inputLoanIds;

        query = 'SELECT Id ' +
                'FROM LLC_BI__Loan__c ' +
                'WHERE Id IN :loanIdsParam';

    }



    global Database.QueryLocator start(Database.BatchableContext bc) {


        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc, List<LLC_BI__Loan__c> loanListScope){


        for(LLC_BI__Loan__c loan : loanListScope){

            Map<String, Object> flowInterviewInputMap = new Map<String, Object>();
            flowInterviewInputMap.put('inputVariableId', loan.Id);

            PPShareFlowHandler.runFlow('test_case_flow', flowInterviewInputMap);


        }


    }    


    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed.');

        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];

        system.debug('Apex Job -- ' + job);

    }    

}

Example Anonymous Apex To Trigger the Batch Class

List<String> inputList = new List<String>{'idone', 'idtwo', 'idthree'};

batchablePartnerPortalShare newBatch = new batchablePartnerPortalShare(inputList); 


database.executeBatch(newBatch, 5);

2 Answers 2

5

Your batch class doesn't have any constructors defined, so you only get the default, no-arg constructor.

Perhaps you meant to make your global void batchAccess(List<String> inputLoanIds) a constructor, in which case the method signature would be public bathcablePartnerPortalShare(List<String> inputLoanIds) (no return type, method name matches the class name, avoid using the global modifier unless you know you need it).

Otherwise, you could simply use the default constructor and then call your public void batchAccess(List<String> inputLoanIds) method (again, the global access modifier should generally be avoided. Use public unless it's one of the few scenarios that require the global modifier).

List<String> inputList = new List<String>{'idone', 'idtwo', 'idthree'};

// You don't have a constructor that takes a list, so this is invalid syntax
//batchablePartnerPortalShare newBatch = new batchablePartnerPortalShare(inputList);

// Instead, just use the default constructor and call your method afterwords
batchablePartnerPortalShare newBatch = new batchablePartnerPortalShare();

newBatch.batchAccess(inputList);
0
4

Your class currently does not have a constructor that accepts one argument.

A constructor is a method with the exact same name as the class.

From https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm :

If a class does not have a user-defined constructor, a default, no-argument, public constructor is used.

This means that your class has a default, no-argument constructor, so that when you instantiate it in other code, you just use:

batchablePartnerPortalShare newBatch = new batchablePartnerPortalShare();

Then you would could explicitly call your batchAccess method using newBatch (e.g., newBatch.batchAccess(inputList);)

Or you could [slightly modify* and] rename your batchAccess method to be a one-argument constructor:

global batchablePartnerPortalShare (List<String> inputLoanIds) {
    loanIdsParam = inputLoanIds;
    query = 'SELECT Id ' +
            'FROM LLC_BI__Loan__c ' +
            'WHERE Id IN :loanIdsParam';
}

...in which case the anonymous Apex you included should work.

BUT if you do that, from the document linked above:

If you create a constructor that takes arguments, and you still want to use a no-argument constructor, you must create your own no-argument constructor in your code. Once you create a constructor for a class, you no longer have access to the default, no-argument public constructor.

(NOTE: Use public unless you are absolutely certain of your need to use global.)


*UPDATED to remove void return type based on Derek's gentle reminder comment.

2
  • I thought that constructors couldn't have any return type. Testing a bit more, it looks like including any return type still allows the class to be compiled (though the constructor with a return type is not usable unless the class is defined in anonymous apex). Today I learned (but don't tempt fate here).
    – Derek F
    Commented Oct 4, 2021 at 18:06
  • Doh! You are correct! That's what I get for copying/pasting without looking closely. Thanks!
    – Moonpie
    Commented Oct 4, 2021 at 18:12

You must log in to answer this question.

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