Create and update records using Javascript button in Salesforce- Ajax Toolkit

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

Update Record using Javascript

Javascript code :


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]);
   alert('Account updated successfully');
  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.

Update Record using Salesforce AJAX Toolkit
Update Record using Salesforce AJAX Toolkit

Related posts

3 thoughts on “Create and update records using Javascript button in Salesforce- Ajax Toolkit”

  1. Instead of update if there would be a delete operation here on detail view so what would be the syntax for this….I have requirement inwhich i have written a class and calling that class using javascript but when i am trying to delete the record based on the condtion then I am not sure what kind of parameters should i pass in order to delete the record based on the condition……Can u please provide the syntax for custom delete on Detail view…

Leave a Reply