1

I currently have a custom button which has the following javascript :

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var ref = "{!Proposal__Proposal__c.Id}"; 
var result = sforce.apex.execute("GenerateQuotePdfCont","saveQuotePdf",{parentId:"{!Proposal__Proposal__c.Id}"});
window.open('/apex/GenerateQuotePdfPage?scontrolCaching=1&id='+ref+'&scontrolCaching=1');

However, I would like to move this logic into a hyperLink function and then use it in a formula field like this :

IF (NOT ISBLANK(Configuration_Saved_Date__c) && Proposal__ReadyToGenerate__c && Grand_total_exclusive__c > 0, HYPERLINK("{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var ref = "{!Proposal__Proposal__c.Id}"; 
var result = sforce.apex.execute("GenerateQuotePdfCont","saveQuotePdf",{parentId:"{!Proposal__Proposal__c.Id}"});
window.open('/apex/GenerateQuotePdfPage?scontrolCaching=1&id='+ref+'&scontrolCaching=1');","_self"), NULL)

Is this something that is possible? Currently, the formula does not compile

  • 1
    I suspect this is actually an x-y problem. Maybe if you asked about what you're trying to accomplish, we could help out better. For example, do you need this to work in a list view? A related list? A report? Just on a detail page? Some things are more possible than others, so I'd recommend asking about the underlying problem directly. – sfdcfox Sep 20 '16 at 16:02
  • Hi @sfdcfox, I would like it work on a detail page. So the idea is to have a button inside a formula field on the detail page. On click, it attaches a pdf to the record and opens the pdf in a new window. – saint Sep 20 '16 at 16:07
  • You could use a Custom Link instead of a Custom Button, or you could even drop the code in to a Visualforce page with a button in it, and drop the page on to the page layout. – sfdcfox Sep 20 '16 at 16:08
  • so if i use a custom link, the logic above would work? I'm not quite sure how different that would be from a button – saint Sep 20 '16 at 16:17
  • 1
    The difference between a link and a button is presentation. Links are in the detail area, while a button is near the top and bottom of the detail area (like Edit/Delete/etc). Likewise, VF pages can be put in an area in a way that it appears to be a field, although, obviously, it can't be shown in list views/reports. – sfdcfox Sep 20 '16 at 16:19
3

Nope, it isn't possible to have Javascript or Apex in a formula field.

When you create a formula field, the formula itself is stored in the database (as opposed to storing the result of the formula when it's evaluated). Every time you query a formula field, that formula is evaluated by the database, and the result made available to you.

This behavior is briefly explained in An Introduction to Formulas in the Formula Limits section

Formula fields are calculated using generated SQL on the backend. This SQL must not exceed 5000 characters. This includes other referenced formula fields. [rest of section omitted]

SQL can't execute or evaluate Javascript (there would be a ton of chaos if that were possible.) At best, your Javascript would be treated as plain text. At worst, your formula field won't compile.

2

At least with the HYPERLINK option it is possible to add javascript to a formula field. Refer to this blog post for adding javascript to HYPERLINK() option in formula fields: http://blog.adityanaag.com/16/Execute+Javascript+from+Formula+field

0

Starting with the Spring ‘17 release in February 2017 this will not be available. They've already stated that it could give rise to a breach and malicious code inclusion. You can find info here and here too.

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.