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?

  • techtrekker

    techtrekker Correct answer

    9 years ago

    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.

    Having written this just read point 2) in your question and you may have already tried this. Which makes me think its probably apex then.

    +1 for it's either this or apex. There's not a non-API, non-apex way of doing this.

    Agree. Would the only other option be to set the page to edit mode (/e in the url) passing through field values as query params, and leave the page like that. The save would be down to the user clicking Save.

    True, although I would note that 1) that's a totally unsupported API that may break in the future without warning and 2) it doesn't work for managed packages and custom objects as the field IDs you need to pass change between orgs. I've done this in the past because it's the only way sometimes, but I want to make sure the normal warnings are associated with doing so.

    Thanks guys. That was my thought as well, but wanted to check. @techtrekker - If you update your answer that it isn't possible without Apex, I'll accept it.

    Cheers Dan, done.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used