Create,Update records with Java script custom buttons in salesforce.
In this example, I’ll create a new Account record by pressing a button on the Account object. You’ll want to adapt this to a more relevant scenario but this will help you with the basic process.
First, you’ll create the button
- Navigate to Setup | Customize | Account | Buttons, Links and Actions
- Press the New Button or Link button
- Give your button a name
- Choose the appropriate Display Type option but in most cases you’ll likely choose a Detail Page Button
- Choose Execute JavaScript as the Behavior
- In the field box you’ll add the JavaScript which I’ll explain below
The JavaScript for this is pretty simple even if you’re not that familiar with the language. I’m certainly no expert on it either so I’m confident you’ll do fine.
- First, we have to include the libraries so add the following to the field box. These need to be the first items in the script.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
- Next, we need to create an object variable to hold our data. In this example, I’ll create my variable, called “acct”, as an Account record. This could be a contact or any other object within Salesforce.
var acct = new sforce.SObject("Account");
- Now I will populate the fields on the object. I do this by using the variable name I just created. I can add as many of these as needed to correctly populate my new record.
acct.name = 'New Account';
acct.phone = '515-123-4567';
- Now that I have all my fields defined, I’m ready to save the new record.
var result = sforce.connection.create([acct]);
Finally, you’ll want to verify the record was created. In the last step we saved the results of our save in a variable called “result”. Below we’re reading that variable to determine if we were successful. If we are successful, I’m opening the new record in “edit mode” for the user to make any additional changes. If it was not successful, then I display an error message.
if(result[0].getBoolean("success")){
window.location = "/" + result[0].id + "/e";
}else{
alert('Could not create record '+result);
}
With the above, you should take some liberty and process the results in a way that makes sense for your situation. If you remove the ‘ + “/e” ‘ from the end of the window.location line, the new record will show in “read mode”. Maybe, you don’t want to display the new record at all so, in that case you might do another alert() box to simple tell the user the record was successfully created.
update Records with Java Script Button:
In this first example, I query the record and display the results in an alert window:
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
// query the record
var qr = sforce.connection.query("SELECT Id, SyncedQuoteId FROM Opportunity where Id='" + "{!Opportunity.Id}" + "'");
var records = qr.getArray("records");
// display the field from the query
alert("SyncedQuoteId: " + qr.records.SyncedQuoteId);
This next example updates the opportunity stage.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
// identify the record
var o = new sforce.SObject("Opportunity");
o.id = "{!Opportunity.Id}";
// make the field change
o.StageName = "Closed Won";
// save the change
sforce.connection.update([o]);
//refresh the page
window.location.reload();
Hope this helps you update records with ease.
4 comments:
Nice i expecting this kind of explanations which is most helpful for me to improve the programming skills.
Java Training in Chennai
Learned a lot of new things from your post , Thanks for sharing
Java Online Training
thanks for sharing..
Wireless Access Points
I really enjoy the blog.Much thanks again. Really Great. salesforce training
Post a Comment