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
Jon FoyJon Foy 

Need Help with Javascript Button

We have a custom object "Task_Custom__c" that I would like to add a button to.  When the button is clicked, we need two fields on this object to be updated:

Completion_Stats__c = "Completed"
Completion_Percentage__c = "100"

Can that be done with on click javascript?
Best Answer chosen by Jon Foy
claperclaper

I Created the Custom object, fields & Custom buttom, on my org and tested it, it worked. See this screencast: 

http://screencast-o-matic.com/watch/c2XZqQeYvw

Code for Detail Buttom:
 

{!requirescript("/soap/ajax/26.0/connection.js")}
var taskCustom= new sforce.SObject("Task_FTA__c");
taskCustom.id = '{!Task_FTA__c.Id}';

taskCustom.Completion_Status__c= "Completed";
taskCustom.Completion_Percentage__c="100";
sforce.connection.update([taskCustom]);
window.location.reload(); //reload the window to show the updated values

All Answers

claperclaper
Yes,  you can do so with the following code:
{!requireScript("/soap/ajax/26.0/connection.js")}
var taskCustom= new sforce.SObject("Task_Custom__c");
taskCustom.id = '{!Task_Custom__c.Id}';

taskCustom.Completion_Stats__c= "Completed";
taskCustom.Completion_Percentage__c="100"
sforce.connection.update([taskCustom]); 
window.location.reload(); //reload the window to show the updated values

 
Jon FoyJon Foy
I had a couple of the values wrong, I've updated everythign with correct field/values:

{!requirescript("/soap/ajax/26.0/connection.js")}
var taskCustom= new sforce.SObject("Task_FTA__c");
taskCustom.id = '{!Task_FTA__c.Id}';

taskCustom.Completion_Status__c= "Completed";
taskCustom.Completion_Percentage__c="100";
sforce.connection.update([taskCustom]);
window.location.reload(); //reload the window to show the updated values

However, the button, when clicked, refreshes the screen, but the values are not updated.
claperclaper

I Created the Custom object, fields & Custom buttom, on my org and tested it, it worked. See this screencast: 

http://screencast-o-matic.com/watch/c2XZqQeYvw

Code for Detail Buttom:
 

{!requirescript("/soap/ajax/26.0/connection.js")}
var taskCustom= new sforce.SObject("Task_FTA__c");
taskCustom.id = '{!Task_FTA__c.Id}';

taskCustom.Completion_Status__c= "Completed";
taskCustom.Completion_Percentage__c="100";
sforce.connection.update([taskCustom]);
window.location.reload(); //reload the window to show the updated values
This was selected as the best answer