8

I want to store scripting in an SObject field so that I can evaluate it using apex code. Is there some API method that would allow me to evaluate a string in a manner similar to

public class myclass{
  public CustomObject__c obj { get; set; }
  public void evaluate() {
    // this would be set on the object via VF
    obj.customFunctionField__c = '{!IF(<replaceMe>,"value1","value2")}'; 
    String toEval = obj.customFunctionField__c.replace('<replaceMe>',obj.fieldToEvaluate__c
    ScriptFunctionAPIResult res = ScriptFunctionAPI.Evaluate(toEval);
    // do something with the result
  }
}

Is there a way to evaluate or execute the same scripting that is used in formulas and visualforce?

3

You can evaluate dynamic expressions while executing in Visualforce context. Unfortunately, this feature isn't available everywhere (e.g. triggers), although if you're not restricted by callout (e.g. in a trigger), you could even create an arbitrary page that can evaluate any expression using that logic (warning: not for the faint of heart).

Apex Code exposes the expression engine through the Component class. A very simple example is:

Component.Apex.OutputText text = new Component.Apex.OutputText();
text.expressions.value = '{!IF(5<10,"Hello","Goodbye")}';
System.debug(text.value);  // Outputs "Hello"

You'll want to take a look at Creating and Displaying Dynamic Components for more information, but in summary, it lets you gain access to expressions you'd be able to evaluate within page context in your code.

The expressions engine includes access to record fields, global variables (e.g. $User), and most anything else you can reference in Visualforce directly.

2
  • How about using System.Assert? – ManishG Feb 27 '18 at 8:39
  • @ManishG The debug statement was only to demonstrate that the value output would be "Hello." It could have been an assert, but wasn't particularly relevant, so I simply wrote it as a debug statement. In real world code, you'd use the output to do something, like setting a variable, etc. – sfdcfox Feb 27 '18 at 12:38
0

As of my knowledge there is nothing like this in apex to do it straight forward in an officially supported way.

It is not the formula-language, but depending on your detailed requirements you could have a look at executeAnonymous() to execute a string of apex. You can invoke it either by the wrapper here https://github.com/afawcett/apex-toolingapi or via the Ajax toolkit. Please keep in mind that both is officially not supported.

Another unsupported workaround could be to use toolingApi to create a visualforce page or a formula-field on the fly - then eventually you could get your formulas.

It's not unlikely to run into serious challenges like this Get Debug Logs with specified log levels for Execute Anonyous via APEX ToolingAPI, SoapAPI or anyhow and it's for sure no beginners task.

I can only provide you this general ideas and no source code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.