11

Note: This question was written long ago for the Classic UI. If you are looking at this question now, the answers are no longer the best option. Take a look at a quick action or screen flow as alternatives. Leaving the question as is for historical sake.


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?

2 Answers 2

17

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.

6
  • 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. Oct 11, 2012 at 19:44
  • 1
    +1 for it's either this or apex. There's not a non-API, non-apex way of doing this. Oct 11, 2012 at 19:45
  • 1
    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. Oct 11, 2012 at 19:48
  • 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. Oct 11, 2012 at 19:51
  • 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. Oct 11, 2012 at 20:47
1

Actually there is another possiblity. We had a standard page that showed a customer's email address and wanted the agents to click a button that simply "verifies" it. The email address would appear in different annoying colors like highlighted yellow if it had been a long time since the email was verified.

We implemented this by making a little-tiny visual force page with a custom extension. The standard controller was the object on the page (Account, in our case), of course. I put a form with a single field and custom button. Clicking the button called the controller which of course updated the last-verified date in the Account object.

Then, we simply went into the page layout and added that VF page right onto the form in a little tiny single field size slice of a standard section. (It looks like a regular field on the form, albeit with a button next to it)

Now we have the most effectively annoying yellow field that forces the agent to click the button if they want to get rid of the nasty yellow. It's quite difficult to be on the page and not click the button, you know, just to get rid of the yellow. But, hey, now our email addresses get validated every 3 months.

I'm happy to share the code if you like. Here's the VF Page:

<apex:page standardController="Account" extensions="CustomerConfirmedEmailExtension">
<apex:form >

    <font style="background: {!highlight}">
    <apex:outputText rendered="{! NOT(ISBLANK(Account.Other_Email_Confirm_Date__c))}" value="{0,date,MM'/'dd'/'yyyy}">
        <apex:param value="{!Account.Other_Email_Confirm_Date__c}" />
    </apex:outputText>
    <apex:outputText rendered="{! ISBLANK(Account.Other_Email_Confirm_Date__c)}" value="Never"/>

    </font>&nbsp;&nbsp;
    <apex:commandButton action="{!reconfirmEmail}" value="Validate" title="Click this after verifying the customer email address" />

</apex:form>
</apex:page>

Here's the Controller:

public class CustomerConfirmedEmailExtension {
    private final Account acct;
    public String highlight {
        get {
                if (acct.Other_Email_Confirm_Date__c != null && acct.Other_Email_Confirm_Date__c > System.today().addMonths(-6)) {
                    return 'none';
                } else {
                    return 'yellow';
                }
            }
        set {}
        }
    public CustomerConfirmedEmailExtension() {

    }
    public CustomerConfirmedEmailExtension(ApexPages.StandardController stdController) {
    this.acct = (Account)stdController.getRecord();
    }
    public PageReference reconfirmEmail () {
        this.acct.Other_Email_Confirm_Date__c = System.today();
        update acct;
        CustomerInteractionExtension.logInteraction (acct.id, 'Confirm Email', 'SFDC', null );
        return null;
    }

}
5
  • Thanks for the idea, but I was trying to avoid using any Apex code. Jan 9, 2013 at 16:15
  • 1
    DaveS - I am looking to do something pretty much the same - does your offer to share code still stand?
    – AngiB
    Jan 24, 2013 at 20:06
  • @AngiB I'm happy to share it with you. I didn't see this comment until just now, otherwise I'd have gotten it to you sooner. Do you still need it?
    – DaveS
    Feb 5, 2013 at 20:19
  • @DaveS Yes please (sorry for the delay in responding... life continues to be too busy)
    – AngiB
    Feb 10, 2013 at 23:06
  • @AngiB Glad I could help--the code is in my answer
    – DaveS
    Feb 11, 2013 at 2:31

You must log in to answer this question.

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