Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Use Case - There is Javascript execute button on Account page layout named 'Update Account' -

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}

//JSsript code which updates account based on some logic 
...
..
//40 or 50 lines of Code

Problem with above button is it loads all this Javascript on every account pageview and kindof slows down each page view. what if we have 2 or more buttons then again its more time to load. Is there any way we can store Javascript in static resources and then load it only when user clicks button and then execute it ?

Note: I already know that you can Create Apex Webservice method and then call it from JS but wanted to check if there is any alternate way to do this.

share|improve this question

2 Answers 2

Each of your custom buttons could be written as so, letting 'RequireScript' determine whether your static resource script has already been loaded into the page (by another button).

If you can abstract the functions into a static resource file, the resource will only be loaded once and the button action can then call the function defined within the static resource.

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}

{!REQUIRESCRIPT("/resource/YourStaticResourceJSFileName")}

//JSsript code which updates account based on some logic 
callYourFunctionDefinedInTheResource(arguments);

Here is a bit more info about the anti-caching number contained within the static resource URL path. Using static resources with Salesforce custom Javascript buttons

share|improve this answer

You could create a VF page that contains all the logic you need, then pass whatever parameters you need from the Account page to this VF page when the user clicks your button. After the actions have taken place, send the user back to the Account record.

Is there a specific reason you need to execute Javascript? Or can all the updates be contained in the VF page controller?

Let me know if you have any questions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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