On this blog we have already seen “how to use Ajax Toolkit to mass update records” without writing any Apex code. In this article as well, we will use Ajax Toolkit to add custom button in Account page layout and update record.
There are endless possibilities and use-cases where this approach can be very useful in Salesforce projects and can be accomplished by Salesforce admins. Lets say we want to create child record on click of button, Update current record on basis of some logic, Update hidden fields, execute existing Apex code using Javascript and so on. All of these can be implemented wthout writing single line of Apex code, Trigger or Visualforce pages.
In this post, we will add custom button on Account detail page and execute Javascript to update Account name using Ajax toolkit. This is just proof of concept and any other field can be updated.
Creating Custom button :
Navigate to “Customize | Accounts | Button, Links and Actions”.
Create new button of type “Detail Page Button” , behavior “Execute Javascript” and content source “Onclick JavaScript”.
Update Record using Javascript
Javascript code :
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} var accnt = new sforce.SObject("Account"); accnt.Id = '{!Account.Id}'; accnt.Name = prompt('Enter new Account Name','{!Account.Name}'); var result = sforce.connection.update([accnt]); if(result[0].getBoolean("success")) { alert('Account updated successfully'); window.location.reload(); } else{ alert('Error : '+result); }
Add this newly created button on Account page layout and click on it. It would prompt for new name and will update current record.
Leave a Reply