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
TimSTimS 

How to run workflow from custom button

I need some help with javascript code for custom button to update a field on a record so a workflow rule is executed that then sends an email alert. I have a checkbox field to update but as I am not versed very well in code to update it, need some assistance. Does anyone have some code to accomplish this yoy can share? Thanks.
Best Answer chosen by TimS
SonamSonam (Salesforce Developers) 
try the following code:

{!requireScript("/soap/ajax/32.0/connection.js")}
var update_Account = new sforce.SObject("Account");
update_Account.Id = "{!Account.Id}";
update_Account.At_Risk_Request__c = true;
sforce.connection.update([update_Account]);
window.location.reload();
 

All Answers

SonamSonam (Salesforce Developers) 
Please check the following thread:
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008mxjIAA 
Its a similar questions with sample code.
TimSTimS
Thanks...used some code found there but still having an issue.  Following is the button code I have attempted to use but seems no matter what adjustment I make, I always get the error "Unexpected Identifier."  Any ideas or explanation of what I am doing wrong? Button is on Account object...check box field I am wanting to update to true is Ac_Risk_Request__c.

{!REQUIRESCRIPT('/soap/ajax/31.0/connection.js')}

var atRisk ="{!Account.At_Risk_Request__c}";
var account = new sforce.SObject("Account");
account.id = "{!Account.Id}";
account.atRisk = 1;
var result = sforce.connection.update([Account]);

    if(result[0].success == 'true'){
        location.reload();
        alert('Your request for access to this account's At Risk details has been Sent Successfully. You will be notified of the decision to grant or deny access');
    }
    else{
        alert('An Error has Occurred. Error:' + result[0].errors.message);
    }
SonamSonam (Salesforce Developers) 
try the following code:

{!requireScript("/soap/ajax/32.0/connection.js")}
var update_Account = new sforce.SObject("Account");
update_Account.Id = "{!Account.Id}";
update_Account.At_Risk_Request__c = true;
sforce.connection.update([update_Account]);
window.location.reload();
 
This was selected as the best answer
TimSTimS
Great...that did the trick...THANK-YOU!!!!!
Jamie RogersJamie Rogers
What was the trick that made it work?
Bizmarketing (Admin)Bizmarketing (Admin)
Thank you Sonam - I can do amazing things now - thanks to you!