I am trying to save an batch apex class dynamically via the tooling api.
//I want the batch class to be of the sObject type that user wants.
//Regex replaces %s with className provided by the user.
//Then I post the class via Tooling API.
public class TestBatchExecute {
public String batchClass='public class TestBatch implements Database.Batchable<%s>{\npublic System.Iterable<%s> start(Database.BatchableContext bc){\nreturn Database.getQueryLocator(\'select id,name from \'+ %s);\n}\npublic void execute(Database.BatchableContext bc,List<%s> scope){for(%s obj:scope){\nsystem.debug(obj);\n}\n}public void finish(Database.BatchableContext bc){system.debug(\'done job\');}}';
public TestBatchExecute(String className){
if(Schema.getGlobalDescribe().containsKey(className)){
try{
batchClass = batchClass.replaceAll('%s',className);
System.debug(batchClass);
if(NetworkUtils.generatePostResponse('ApexClass/',JSON.serialize(new PostObject('TestBatch',batchClass)))){
system.debug('goodmate');
}
}catch(StringException se){
system.debug(se);
}
}
}
}
Then in the anonymous window : -
new TestBatchExecute('Account');
I get the following error : -
{"message":"Variable does not exist: Account","errorCode":"INVALID_FIELD_FOR_INSERT_UPDATE","fields":[]}```