We are letting users select certain buttons that contain a URL in our application. That button now accepts merge fields. Handling '{!Object.field}' and '{!$User.field}' and '{!Today()}', and '{!Now()}' are all simple.

The problem comes when someone adds something like this:

{!CASE(TEXT(Opportunity.Dynamic_Template_Selection__c), "Word","a041H00000hN6Fw,a041H00000hN6Fx","Powerpoint", "a041H00000hN6Fy", "a041H00000hN6Fx")}

We have to handle the parsing of merge fields in our controller. But that cannot be handled without basically re-writing the merge field handler Salesforce already has for its buttons...

I am wondering if we can grab the record page layout given a record Id, find that button, and get its parsed URL from Apex? Or can a button be dynamically added to an Apex page to parse the URL without the button actually doing anything or being visible?

  • I'm interested but a bit confused by what you're looking to do. Seems like you have a user input where they can provide a formula (essentially) and then want to parse the users formula for use in a url? – battery.cord Jul 10 '18 at 18:49

As long as you're using Visualforce, you can do something like this:

Component.Apex.OutputText text = new Component.Apex.OutputText();
text.expressions.value = '{!NOW()}';

This will cause text to be evaluated dynamically. Note that this feature only works in Visualforce, so if you need it elsewhere, you'd still have to find a parser of some sort. Conceptually, you could also make a Visualforce page that accepts the parameter and then call it via PageReference.getContent() to get the result back:

PageReference ref = Page.mergeParser;
ref.getParameters().put('mergeField', mergeStatement);
String result = ref.getContent().toString();
  • Thanks for this. It is definitely giving me something to mess with. However, the second method is giving me Internal Salesforce Error (External Entry Point). The string I want to merge is a url that starts with /apex/page?<parameters that may be merge fields> Not sure how to debug that, the debug log doesn't give me much. The error only comes when I try to system.log the value, or try to put the value back on the visualforce page. – Tyler Dahle Jul 10 '18 at 19:37
  • @TylerDahle If possible, try submitting a case. I could swear that this used to work in a previous release, but I haven't tried it in a while (a few years, at least). – sfdcfox Jul 10 '18 at 19:38
  • I got it working with only record merge fields. Expressions, apparently, are not working. Throwing in {!TEXT(Opportunity.Id)} gives the external entry point error. Otherwise it is fine. – Tyler Dahle Jul 10 '18 at 20:02
  • @TylerDahle I'd still submit a case and see if it's something that can be done (or not). Otherwise, you're going to be stuck writing your own parser, which isn't "difficult" but can definitely be time consuming. – sfdcfox Jul 10 '18 at 20:43
  • Ok, new developments. Got the output text to work with expressions like CASE, TEXT will still blow it up with that error. But then there is the issue that this requires a standard controller, and object will not always be the same, and don't think you can have dynamic standard controllers. Without the ability to set the controller based on the current object to handle the parsing, I think our only option is a custom parser... Which, yeah... not thrilled about writing. – Tyler Dahle Jul 10 '18 at 20:46

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.