+ Start a Discussion
gregsgregs 

Can formula fields call apex code or javascript?

I have a formula field that i want to display a hyperlink to a visualforce page if the value if the current userid is a member of a given queue.  What i want to do is something like this:


IF (

$User.Id IN [select Id,Name,Type,(select UserOrGroupId From GroupMembers) from Group where Type='Queue'], "some link if true",

"some link if false"

)

 

Is there any way to do something like this straight from a formula field without just going to a visualforce page that has that logic in the controller?

info4jf-forceinfo4jf-force

No. 

 

I asked Salesforce Support about this a while back and they said confirmed that no, formula field code cannot call apex code or javascript.  About the best that it can do is create a hyperlink that the user could then click on to call a visual force page that contains apex code.  However, that is, of course, not the same as the formula field code calling it; it is subsequent and asynchronous with the formula field code's execution and not guaranteed to happen.

 

It would be really nice if formula field code could call apex code because then we could use apex functions like the one for calculating work days between two dates.

Rajiv Bhatt 7Rajiv Bhatt 7
You can use javascript in formula fields. Here I have explained in detail with code how to do it: 

https://sfdc-gyaan.rhcloud.com/2014/04/referencing-javascript-file-from-static-resources-in-formula-fields/
Matthew FarrarMatthew Farrar
@Rajiv Bhatt 7 This link is dead