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
Matt FolgerMatt Folger 

Custom button that sets a numeric field value

Hi, I have an app that needs to have a button next to Edit and Delete and Save that changes a field value of the record viewed to '11'. 

So far I have this: 
 
<apex:commandbutton value="----- Mark Complete-----" action="{!RPP_Tracker__c.Present_Dept_Number__c = 11}"  />
I know this won't work because it's not actual referencing any specific record, simply the custom object field, but how do I change this to get the custom button to grab the existing record that is displayed on the page on which is presently being viewed?

Seems like it should be easy.
 
Best Answer chosen by Matt Folger
Vivek DeshmaneVivek Deshmane
Hi,
Try below javascript code in your custom button and let me know if it works.
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 
{!REQUIRESCRIPT("/support/console/28.0/integration.js")} 


var record = sforce.connection.query("SELECT Id, Present_Dept_Number__c from RPP_Tracker__c where Id = '{!RPP_Tracker__c.Id}'"); 
var recordArr = record.getArray("records");
recordArr[0].Present_Dept_Number__c = '11'; 
var recordArr2 = []; 
RecordArr2.push(recordArr[0]); 
sforce.connection.update(RecordArr2);


Best Regards,
-Vivek

All Answers

Terence_ChiuTerence_Chiu
Matt, you will need to implement an Apex controller class for your VF page so you can create a custom action method to faciliate the update. Below link gives an overview and an example of how that would work.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_methods.htm
Vivek DeshmaneVivek Deshmane
Hi,
Try below javascript code in your custom button and let me know if it works.
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 
{!REQUIRESCRIPT("/support/console/28.0/integration.js")} 


var record = sforce.connection.query("SELECT Id, Present_Dept_Number__c from RPP_Tracker__c where Id = '{!RPP_Tracker__c.Id}'"); 
var recordArr = record.getArray("records");
recordArr[0].Present_Dept_Number__c = '11'; 
var recordArr2 = []; 
RecordArr2.push(recordArr[0]); 
sforce.connection.update(RecordArr2);


Best Regards,
-Vivek
This was selected as the best answer