{"id":4394,"date":"2015-04-21T18:39:01","date_gmt":"2015-04-21T18:39:01","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=4394"},"modified":"2015-04-21T18:41:29","modified_gmt":"2015-04-21T18:41:29","slug":"create-and-update-records-using-javascript-button-in-salesforce-ajax-toolkit","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-and-update-records-using-javascript-button-in-salesforce-ajax-toolkit\/","title":{"rendered":"Create and update records using Javascript button in Salesforce- Ajax Toolkit"},"content":{"rendered":"<p style=\"text-align: justify;\">On this blog we have already seen &#8220;<a title=\"Create Simple Mass Delete Button for ListView using Salesforce Ajax Toolkit\" href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-simple-mass-delete-button-for-listview-using-javascript\/\">how to use Ajax Toolkit to mass update records<\/a>&#8221; without writing any Apex code. In this article as well, we will use <a title=\"Salesforce - The AJAX Toolkit\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/ajax\/\">Ajax Toolkit<\/a> to add custom button in Account page layout and update record.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p><strong>Creating Custom button :<\/strong><\/p>\n<p>Navigate to &#8220;Customize | Accounts | Button, Links and Actions&#8221;.<\/p>\n<p style=\"text-align: justify;\">Create new button of type &#8220;Detail Page Button&#8221;\u00a0,\u00a0behavior &#8220;Execute Javascript&#8221; and content source &#8220;Onclick JavaScript&#8221;.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Javascript.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"wp-image-4395\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Javascript.png?resize=550%2C424&#038;ssl=1\" alt=\"Update Record using Javascript\" width=\"550\" height=\"424\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Javascript.png?w=941&amp;ssl=1 941w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Javascript.png?resize=300%2C231&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Javascript.png?resize=624%2C481&amp;ssl=1 624w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p>Update Record using Javascript<\/p>\n<p><!--more--><\/p>\n<p><strong>Javascript code :<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{!REQUIRESCRIPT(&quot;\/soap\/ajax\/33.0\/connection.js&quot;)}\r\n{!REQUIRESCRIPT(&quot;\/soap\/ajax\/33.0\/apex.js&quot;)} \r\n\r\nvar accnt = new sforce.SObject(&quot;Account&quot;);\r\naccnt.Id = '{!Account.Id}';\r\naccnt.Name = prompt('Enter new Account Name','{!Account.Name}');\r\n\r\nvar result = sforce.connection.update(&#x5B;accnt]);\r\nif(result&#x5B;0].getBoolean(&quot;success&quot;))\r\n{\r\n   alert('Account updated successfully');\r\n   window.location.reload();\r\n}\r\nelse{\r\n  alert('Error : '+result);\r\n}\r\n<\/pre>\n<p style=\"text-align: justify;\">Add this newly created button on Account page layout and click on it. It would prompt for new name and will update current record.<\/p>\n<figure id=\"attachment_4396\" aria-describedby=\"caption-attachment-4396\" style=\"width: 625px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Salesforce-AJAX-Toolkit.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-4396\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Salesforce-AJAX-Toolkit.png?resize=625%2C197&#038;ssl=1\" alt=\"Update Record using Salesforce AJAX Toolkit\" width=\"625\" height=\"197\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Salesforce-AJAX-Toolkit.png?resize=1024%2C322&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Salesforce-AJAX-Toolkit.png?resize=300%2C94&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Salesforce-AJAX-Toolkit.png?resize=624%2C196&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/04\/Update-Record-using-Salesforce-AJAX-Toolkit.png?w=1116&amp;ssl=1 1116w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><figcaption id=\"caption-attachment-4396\" class=\"wp-caption-text\">Update Record using Salesforce AJAX Toolkit<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>On this blog we have already seen &#8220;how to use Ajax Toolkit to mass update records&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[9],"tags":[270,126,331,269],"class_list":["post-4394","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-ajax-toolkit","tag-javascript","tag-salesforce","tag-salesforce-ajax-toolkit"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3299,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-simple-mass-delete-button-for-listview-using-javascript\/","url_meta":{"origin":4394,"position":0},"title":"Create Simple Mass Delete Button for ListView using Salesforce Ajax Toolkit","author":"Jitendra","date":"May 6, 2013","format":false,"excerpt":"In Many situation, I needed a Mass Delete Kind of button. Where, i can select records in ListView and simply click \"Delete\" button. In this example, we will create a simple List View button and add on \"Search Layout\" of that object. So, create a new Custom Button with following\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Mass Delete Button - List View","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Salesforce-Mass-Delete-Button-List-View.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Salesforce-Mass-Delete-Button-List-View.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Salesforce-Mass-Delete-Button-List-View.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3112,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-question-part-13\/","url_meta":{"origin":4394,"position":1},"title":"Salesforce Interview Question \u2013 Part 13","author":"Jitendra","date":"September 25, 2012","format":false,"excerpt":"121 : Consider we have overall 90% code coverage however there is one class which have 0% code coverage. Can we still able to deploy that class on production? Ans : Yes. Minimum 1% required for every trigger and there is no such restriction for Apex class. 122 : How\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":4394,"position":2},"title":"Apex Interview Question \u2013 Salesforce &#8211; Part 16","author":"Jitendra","date":"July 28, 2013","format":false,"excerpt":"151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ? Ans : It can be done with help of External Id. [java] Date dt = Date.today().addDays(7); Opportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting',\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3185,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-question-part-14\/","url_meta":{"origin":4394,"position":3},"title":"Salesforce Interview Question \u2013 Part 14","author":"Jitendra","date":"February 28, 2013","format":false,"excerpt":"131. What will happen if you try to update record in After Trigger Context? Ans : You will get an error saying \"record is Read only\". 132. Let's say we have to update the same record in After Trigger context. Is there any way or workaround? Ans : If we\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2470,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/salesforce-tutorial-create-simple-ajax-based-visualforce-page\/","url_meta":{"origin":4394,"position":4},"title":"Salesforce Tutorial &#8211; Create Simple Ajax based Visualforce page","author":"Jitendra","date":"October 17, 2011","format":false,"excerpt":"Salesforce Tutorial - Step by step tutorial to create AJAX based application in visualforce page with Apex class","rel":"","context":"In &quot;Web Technology&quot;","block_context":{"text":"Web Technology","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/"},"img":{"alt_text":"Simple AJAX demo in salesforce using visualforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/10\/Simple-AJAX-demo-in-salesforce-using-visualforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2903,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-toolkit-for-php\/","url_meta":{"origin":4394,"position":5},"title":"Salesforce Toolkit for PHP","author":"Jitendra","date":"June 7, 2012","format":false,"excerpt":"Example of using PHP toolkit in Salesforce with Sample code","rel":"","context":"In &quot;Force.com&quot;","block_context":{"text":"Force.com","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/force-com\/"},"img":{"alt_text":"force.com toolkit for PHP toolkit","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/force.com-toolkit-for-PHP-toolkit.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/force.com-toolkit-for-PHP-toolkit.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/force.com-toolkit-for-PHP-toolkit.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=4394"}],"version-history":[{"count":4,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4394\/revisions"}],"predecessor-version":[{"id":4400,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4394\/revisions\/4400"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=4394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=4394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=4394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}