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 :

{!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.

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

Posted

in

by


Related Posts

Comments

6 responses to “Create and update records using Javascript button in Salesforce- Ajax Toolkit”

  1. Sam sun Avatar

    Nice.

  2. Dhananjay Patil Avatar
    Dhananjay Patil

    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…

    1. Jitendra Zaa Avatar

      delete is keyword in Javascript so we need to use deleteIds. check page 9 on this documentation – https://resources.docs.salesforce.com/sfdc/pdf/apex_ajax.pdf

  3. GaBert Avatar
    GaBert

    Hello Jitendra,
    Do you know a document that describe how this kind of code are articulated.
    I am brand new in SF industry, i don’t programation much than that only have my admin certif.
    but this tool seems to be really powerful and really useful for my project to come where i will for instance need to create a button to change a case owner (put it in a particular queue).
    I transposed your code in the case object by changing every reference to account to the case object but i don’t have any result.
    Anyway, if you know a good course, i’ll be really interested (and lot of beginners too!)
    My goal is not to become a JS Star, but just to know how to create button with simple actions using Ajax toolkit
    Many thanks

  4. shams shaik Avatar
    shams shaik

    Hi ,

    How to count and display no. of contacts associated to account on buttonclick using javascript

  5. 1111 Avatar

    Nice, but i want know one thing why its throwing error if i want try from a different user

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading