Applies to: Classic |
After writing the post about creating records with a button, it made sense to extend that thought with manipulating record with a JavaScript button click.
Again, I’m not a JavaScript expert but here are some of the tricks I’ve learned to help automate tasks within Salesforce. Let’s take a look at some of the possibilities:
With all of these examples you’ll need to create your JavaScript button so start by doing that. My examples below are intended to be simple examples for you to adapt to your specific needs.
In this first example, I query the record and display the results in an alert window:
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}</p> <p>// query the record<br />var qr = sforce.connection.query("SELECT Id, SyncedQuoteId FROM Opportunity where Id='" + "{!Opportunity.Id}" + "'");<br />var records = qr.getArray("records");</p> <p>// display the field from the query<br />alert("SyncedQuoteId: " + qr.records.SyncedQuoteId);<br />
This next example updates the opportunity stage.
<br />{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}</p> <p>// identify the record<br />var o = new sforce.SObject("Opportunity");<br />o.id = "{!Opportunity.Id}";</p> <p>// make the field change<br />o.StageName = "Closed Won";</p> <p>// save the change<br />sforce.connection.update([o]);</p> <p>//refresh the page<br />window.location.reload();<br />
Hope this helps you update records with ease.
7 Comments
Hi,
I am trying to create a button called ‘ Load Defaults’ in opportunity create page which when clicked populates fields with default values defined for every user in another object. I’m not sure how to load values in a form on click of button. Can you please help with it?
Thanks in advance,
D
Divya, if you’re on the “Create” page your only button options are Save/Cancel and we’re not in control at that point. You’d have to create and add the button to the “Detail” page. If the field values vary from person to person, then you’ll first need to query the object that holds the defaults and set those to variables. Then reference the variables when building out the new URL. See my post at http://hometeamconsulting.com/url-hacking-for-salesforce-automation/ to see how to build out the URL string. Hope that helps.
Hi Terry:
This is really helpful. Any hints on how I could return the user to their home page after the data has been updated?
Thanks, Michele
Instead of using:
window.location.reload();
try:
window.location = “/home/home.jsp”;
Do validation rules run when doing this? I have a JS button to update the stage, but it does nothing and returns no errors on records where I’d like to have a validation pop up.
First, sorry for the delay in my response. Validation rules should run. I’m speculating here but it would make sense to me that if a validation rule is triggered, the update will fail. However, my code sample isn’t trapping for that so no notification is showing. I’ve not tried this but try this and see if it gets you closer to what you’re wanting:
try {
sforce.connection.update([o]);
}
catch(err) {
alert(err.message);
}
I’m not sure if that will work or not but worth a try.
Good web site! I really love how it is easy on my eyes and the data are well written. I’m wondering how I might be notified when a new post has been made.
I have subscribed to your feed which must do the trick!
Have a nice day!