2

I have text box to input apex function names. For example text box may have DateTime.Today(). I want to read this Text box String value in apex code and use as the standard function. Is there any way we can do that? The issue is the value comes as a String and cannot use it.

5
  • 3
    Can't evaluate a String at runtime into Apex.
    – RCS
    Apr 4, 2018 at 8:54
  • 1
    I don't see why this has 3 close votes of "off-topic".
    – Keith C
    Apr 4, 2018 at 10:45
  • my guess is ppl thought that the post was missing a code sample to better understand what the OP was attempting to do.
    – glls
    Apr 4, 2018 at 12:42
  • 1
    I voted to close, since there's a specific issue thats missing, and that's what the op is trying to do that led them to this point. I think other solutions could be considered other than apex, which due to the scope of the question being very apex focused, aren't being considered. Apr 4, 2018 at 13:13
  • 2
    Fair comments on why close votes might be made; pity not every close vote was accompanied by a comment (or vote on a comment) so the OP could understand what the issue was.
    – Keith C
    Apr 4, 2018 at 15:38

2 Answers 2

6

There is no (simple) mechanism to run arbitrary Apex code and if there was it would represent a serious security problem as code such as delete [select Id from Account]; could be executed.

(JavaScript has an eval function that allows arbitrary JavaScript code to be executed so that has to be blocked to make Lightning Components secure - see Eval() and Function() under LockerService.)

If you have a distinct set of operations you can offer those as a picklist and execute locked down code for each one. Beyond that, it is possible to write an expression parser and create your own (safe) mini-language but that is a lot of work.

1
  • Thanks Keith. Yeah I understand issues behind this. However i tried this in executeAnonymous as sfdxfox pointed out. However it doesn't work on asynchronous context Apr 5, 2018 at 12:39
5

The closest that you can get to today is to call "execute anonymous", which requires administrative privileges to avoid security problems.

You would call it like this:

req.setEndpoint('http://***instance***.salesforce.com/services/data/v42.0/'+
    'tooling/executeAnonymous/?anonymousBody='+
    EncodingUtil.urlEncode(sourceCode,'utf-8'));
req.setMethod('GET');

There is no concept of "reflection" in the world of Apex Code, so it's not trivially possible to execute random code on the fly.

2
  • Thanks sfdcfox. I tried this. However this doesn't work on an Asynchronous context though. I want to run that on both synchronous and asynchronous context. Any lead?? Apr 5, 2018 at 12:36
  • @LMudiyanse Basically, you can't, again, because language reflection doesn't exist. Keith's idea, allowing execution from a predefined set of blocks, might be doable, but you're definitely not going to be able to execute arbitrary code in any context.
    – sfdcfox
    Apr 5, 2018 at 14:20

You must log in to answer this question.

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