You might be able to accomplish this by implementing a future class in the org that takes a string and runs it through the Tooling API. This answer has more details.
I've included some things I tried that didn't work below:
My first thought was to use Queueable
, which initially seemed to work just fine inside Execute Anonymous:
public class testQueue implements Queueable
{
public void execute(QueueableContext context)
{
System.debug('test');
}
}
System.enqueueJob(new testQueue());
However, although this compiles and runs without errors, and even returns a job ID from `system.enqueueJob, the job is not actually enqueued and if you check the database no job with that ID will exist. I also verified by attempting to update a record that the job does not run.
So, I figured that another option might be to implement a class that runs a constructor inside a future method, and dynamically construct the type:
Class saved to Org:
public virtual class futureHandler
{
@future
public static void runFuture(string toRun)
{
Type runnableType = Type.forName(toRun);
futureRunnable toRunInstance = (futureRunnable)runnableType.NewInstance();
toRunInstance.run();
}
}
public interface futureRunnable
{
void run(){};
}
Execute Anonymous Code:
public class futureDo implements futureRunnable
{
public void run()
{
System.debug('test');
}
}
futureHandler.runFuture('futureDo');
However, the issue with this is that the execute anonymous class doesn't exist by the time the future method is called, so Type.forName
returns null. I'm guessing this issue is what's causing the issue with the Queueable
attempt as well. This leads me to believe that there is no way to accomplish this outside using Execute Anonymous via the Tooling API.