You need to sign in to do that
Don't have an account?
AmbigaRam
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
<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
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.
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.
<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);
}
}
}
On the custom button ,you can put your AJAX under Exceute Javascript section.
Thank you all for your Guidance and replies.
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
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
You have to pass accountId in the url
like ?id=001xxxxxxxxxxx
Hi Devender,
Thank you so much !
It works!
Thanks and Regards.,
Ambiga.R