How can I update a field with a custom button?

I would like to create a custom button for use on an object detail page that when clicked updates a field on a the record. So far I’ve looked at two methods, but neither one works perfectly.

  1. I tried using a URL paramters to do it like so: /006U00000028KIs/e?retURL=%2F006U00000028KIs&00NU0000002rvxz=1&save=1, but I get an error “The page you submitted was invalid for your session.” It looks like some orgs don’t allow the save parameter.

  2. I tried using a JavaScript button to update the field, but this doesn’t work for users who have the API disabled.

Is there any other way to get this to work without resorting to Apex?

Answer

You need to use the AJAX Toolkit. Set the detail button to execute a bit of javascript on click.

eg

{!requireScript("/soap/ajax/26.0/connection.js")}
var account = new sforce.SObject("Account");
account.id = //set Record Id here, potentially get it from the URL ?id= param

account.field__c = value;
sforce.connection.update([account]); 
window.location.reload(); //to reload the window and show the updated values

Reference : http://www.salesforce.com/us/developer/docs/ajax/index.htm

So, in summary its either the AJAX Toolkit or Apex, nothing else is possible.

Attribution
Source : Link , Question Author : Daniel Hoechst , Answer Author : techtrekker

Leave a Comment