[SalesForce] Apex Anonymous With Batch Apex Constructor Issue

I am sure there is a simple explanation but when I run in Anonymous Apex my Batchable class I get error: Constructor not defined: [BatchableEncryptedEmailBackfill].<Constructor>(). It is to my understanding that I need to define the constructor with 'ApexPages.StandardController' when there is an extension class. Could someone break down the basics of a Constructor that I am missing? Why would I need a constructor with this class?

This is what I am calling in Anonymous:

BatchableEncryptedEmailBackfill a = new BatchableEncryptedEmailBackfill();
database.executebatch(a);

This is the batachable class:

global class BatchableEncryptedEmailBackfill implements 
    Database.Batchable<sObject>,Database.AllowsCallouts,Database.Stateful{

    global final String Query;
    global final String Entity;
    global final String Field;
    global final String Value;

    global BatchableEncryptedEmailBackfill(String q, String e, String f, String v){
        Query=q; Entity=e; Field=f;Value=v;
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id FROM Lead WHERE pi__url__c != NULL AND Encrypted_Email_String__c = NULL';
        return Database.getQueryLocator(query);
    }  

    global void execute(Database.BatchableContext BC, List<Lead> listLead){
        List<Lead> changedLeads = new List<Lead>();
        for(Lead l: listLead) {
            if(l.Web_Id_NatFund__c != NULL) {
                String val = l.id + ',' + l.Web_Id_NatFund__c;
                ConsoleApplicationLinkController.getShortenedLinkLT(val);
            }
        }
        /* DO NOT NEED TO UPDATE LEADS- UPDATE IS HAPPENING IN GETSHORTENEDLINK METHOD*/
    }

    global void finish(Database.BatchableContext BC){
        system.debug('got into finish:' + BC);
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
                          TotalJobItems, CreatedBy.Email
                          FROM AsyncApexJob WHERE Id =
                          :BC.getJobId()];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {a.CreatedBy.Email};
            mail.setToAddresses(toAddresses);
        mail.setSubject('Apex Sharing Recalculation ' + a.Status);
        mail.setPlainTextBody
            ('The batch Apex job processed ' + a.TotalJobItems +
             ' batches with '+ a.NumberOfErrors + ' failures.');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Best Answer

You have only one constructor defined:

global BatchableEncryptedEmailBackfill(String q, String e, String f, String v)

Yet you are trying to call one where you pass nothing in:

new BatchableEncryptedEmailBackfill() // this constructor is not defined

It's not clear what values you want to pass for q, e, f, and v, but your actual constructor call needs to pass a value for each:

String query = 'SELECT ... FROM ... WHERE ...';
String entity = 'Contact'; // for example
String field = 'Email'; // for example
String value = 'Some Value';
Database.executeBatch(new MyBatch(query, entity, field, value));
Related Topic