function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AmbigaRamAmbigaRam 

Creating custom button for updating field value

Hi,

 

I  want to create the custom button for updating the field value by means of some condition. 

 

For an example, If the field name is 'Test', After clicking the custom button, field name should be changed to "Test Updated'

 

Please guide me for doing this..

 

Any help is appreciated .

 

 

Thanks,

Ambiga

Best Answer chosen by Admin (Salesforce Developers) 
Devender MDevender M

<apex:page controller="classAccount">
  <apex:form >
  <apex:messages />     
     <apex:pageBlock >     
      Account Name : <apex:inputField value="{!a.Name}"/>
     </apex:pageBlock>
     
     <apex:commandButton action="{!someMethod}" value="Update"/>
  </apex:form>
</apex:page>

 

==============================================================================

 

public with sharing class classAccount {

    public Account a{get;set;}
    public classAccount() {
     a = new Account();
     a = [select name from Account Where Id=: ApexPages.currentPage().getParameters().get('Id')];
    }
    
    public void someMethod() {
      if(a.Name == 'Test') {
         a.Name = a.Name + ' Updated';
         update a;
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You update successfully');
          Apexpages.addMessage(errorMessage);
      } else {
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You cannot update');
          Apexpages.addMessage(errorMessage);
      }
    }
}

All Answers

RoyGiladRoyGilad
Hi,
there are several stages for doing it:
1. Create an Apex method (in class) with the logic you want, as a custom controller.
2. Create a Visualforce page that executes the method in the Action, with standard controller for object
3. Create a button that goes to this VF page on the object.
Sonam_SFDCSonam_SFDC

Hi Ambiga,

 

As far as my knowledge goes, you cannot edit the field name on the click of a custom button.You can set a value in the field but not the field label itself.

 

 

Devender MDevender M

<apex:page controller="classAccount">
  <apex:form >
  <apex:messages />     
     <apex:pageBlock >     
      Account Name : <apex:inputField value="{!a.Name}"/>
     </apex:pageBlock>
     
     <apex:commandButton action="{!someMethod}" value="Update"/>
  </apex:form>
</apex:page>

 

==============================================================================

 

public with sharing class classAccount {

    public Account a{get;set;}
    public classAccount() {
     a = new Account();
     a = [select name from Account Where Id=: ApexPages.currentPage().getParameters().get('Id')];
    }
    
    public void someMethod() {
      if(a.Name == 'Test') {
         a.Name = a.Name + ' Updated';
         update a;
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You update successfully');
          Apexpages.addMessage(errorMessage);
      } else {
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You cannot update');
          Apexpages.addMessage(errorMessage);
      }
    }
}

This was selected as the best answer
Mayank_JoshiMayank_Joshi
Yu can use AJAX tool kit for updating Fields . Please refer Apex Dev guide for AJAX .

On the custom button ,you can put your AJAX under Exceute Javascript section.
AmbigaRamAmbigaRam

Thank you all for your Guidance and replies.

 

 

AmbigaRamAmbigaRam

Hi Devender,

 

Thanks for your code , I tried this one.

 

But It showed me the following error,

 

System.QueryException: List has no rows for assignment to SObject 

 

Class.classAccount.<init>: line 6, column 1

sandeep@Salesforcesandeep@Salesforce

This is very easy.

 

1.

Please crearte a VF page and bind this with button ( behavior URL) now whenever you click on this button you will be redirected to this new page and in controller of this page please write logic to update name of this record as you want and again get redirect to source URL of detail page of this record.

 

2.

You can make a custom button of Java script behcaior and then you can write a logic in JS to update record's name as you want you need to use connection.js like below

<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
Devender MDevender M
Hi AmbigaRam,

You have to pass accountId in the url
like ?id=001xxxxxxxxxxx
AmbigaRamAmbigaRam

Hi Devender,

 

Thank you so much !

 

It works!

 

 

 

Thanks and Regards.,

Ambiga.R