Find your content:

Search form

You are here

How can I update a field with a custom button?

 
Share

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?


Attribution to: Daniel Hoechst

Possible Suggestion/Solution #1

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 to: techtrekker

Possible Suggestion/Solution #2

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;
    }

}

Attribution to: DaveS
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2231

My Block Status

My Block Content