0

Using this to get below working:

    public class t12345 implements Database.Batchable<sObject> {

        String query;
        Boolean xyz;

        public t12345(String query){
            this.query= query;
        }

public Database.QueryLocator start(Database.BatchableContext bC){      
    return Database.getQueryLocator(query);
}
    }

and when I run below from developer console,

    Type api = Type.forName('t12345');
    Object apiCall = JSON.deserialize('{"query":"hello world"}',api);
Database.executeBatch((Database.Batchable<sObject>) apiCall);

above execution results in error Argument 1 cannot be null which is coming from start method of batch where query value is still null as constructor with query is not invoked

1
  • 1
    You should include your error message verbatim if you have one...
    – Adrian Larson
    Commented Jan 16, 2018 at 19:20

3 Answers 3

2

If you try:

MyClass instance = new MyClass(); // or any other approach to construction
Database.executeBatch((Database.Batchable<Account>)instance);

You should get this error:

System.TypeException: Invalid conversion from runtime type MyClass to Database.Batchable

If you want to cast a class to Database.Batchable<SObject>, it has to implement that interface.

Instead of:

public class MyClass { ... }

Use:

public class MyClass implements Database.Batchable<SObject> { ... }

This adjustment will mean you actually have to define the methods enforced by that interface.

1
  • Thansk Adrian. I am preparing object in t12345 which is calling batch class that uses batchable interface. I corrected my question accordingly. Goal is to call parameterized constructor in batch class. I tried to generalize my error. sorry for any confusuion
    – sf_user
    Commented Jan 16, 2018 at 19:27
4

You cannot generically call a non-no-argument constructor. While other object-oriented languages have methods for doing this, Apex Code does not have any support for this.

3

Your JSON.deserialize will (probably) set the fields but not via the constructor you have the System.debug in. (Constructors can have an arbitrary signature but the field names are effectively a fixed signature.)

1
  • I am trying to call batch using that object return type. Updated question above.
    – sf_user
    Commented Jan 16, 2018 at 19:18

You must log in to answer this question.

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