1

Can anyone suggest me how to update a field by using custom button--for example I have a status field in case object and a custom button named Test.If I click on "test"(Custom Button) Status field to be changed to Queued or something else

3 Answers 3

3

Here is the custom button, 'onclick javascript', that you would create and add to your page layout(s):

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

var caseObj = new sforce.SObject("Case");
caseObj.id = '{!Case.Id}'; /* Need Id field to update Case */
caseObj.Status = 'Queued'; /* Change status */

/* update method takes an array of Cases; init to 1 element - 'caseObj' */
var result = sforce.connection.update([caseObj]);

if (result[0].success == 'false') {
alert(result[0].errors.message);
}
else {
location.reload(true); /* redisplay the detail page */
}
0

If you're using the default salesforce layout for your object, you may want to explore using "Execute Onclick Javascript" as an option for your button.

http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_more_samples.htm

This page has a lot of great examples of things you can do using the javascript api. To allow your code to run, your first line for the button will need to be:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
1
  • I am new to javascript, can you provide the javascript code for this scenario
    – Venkatesh
    Sep 24, 2014 at 17:53
0

Using {!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} would increase API calls every time this button is used. Instead, just write a dummy VF page code which has only tags like the following :

< apex:page controller = "XYZ" action="{!ABC} >< /apex:page >

Create a class "XYZ" . Have a method "ABC" on the class and in the method, get the Id of the record using ApexPages.currentPage().getParameters().get('id'). Do all manipulations in the controllers and update the record there. Have ABC as a page reference method so that once updated, it can be redirected back to original case page. This dummy VF page will not appear anywhere. It is just for connecting button and controller. Here, no API calls will be sent and any manipulation can be done in the controller.

4
  • Is this usage of a visual force page to essentially allow a button to run Apex code a standard accepted procedure? It seems strange to have a visual force page that is never actually rendered. Also, would this not result in two page loads rather than just a single refresh of the page we're currently on? Sep 24, 2014 at 18:18
  • Considering API limits , this is a better solution than the other solutions which involves Javascript . Moreover it is basically a simple Apex function and it ideally uses action method in Apex: page which is normally not used many times . Salesforce has provided action attribute in apex:page for good reasons and this is one such!! Sep 24, 2014 at 18:20
  • this gets into tradeoffs between dev cycle/deployment/regression tests versus instant user gratification via a change that can be deployed in PROD in 5 minutes -- a lot depends on the nature of the org you are sysad'ing. EE has 5000 api calls per user max of 1,000,000 per day. Not all EE orgs come anywhere close to this limit
    – cropredy
    Sep 24, 2014 at 19:07
  • Why do u wanna waste your API calls for this small thing - what if you wanna use some Real Time Web Services like Address Validations in your org and you can save API calls so as to not reach API limits . All depends on what you wanna do with your org at the end . Can't just decide on a solution just because it works faster!!! Sep 25, 2014 at 4:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .