{"id":4618,"date":"2015-07-07T16:31:20","date_gmt":"2015-07-07T16:31:20","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=4618"},"modified":"2015-11-02T19:21:21","modified_gmt":"2015-11-02T19:21:21","slug":"most-frequently-used-code-snippets-for-salesforce-developers-faq-part-21","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/most-frequently-used-code-snippets-for-salesforce-developers-faq-part-21\/","title":{"rendered":"Salesforce Developers interview questions &#8211; Most commonly used code snippets &#8211; part 21"},"content":{"rendered":"<div class=\"intrinsic-container\"><iframe loading=\"lazy\" src=\"https:\/\/jitendrazaa.com\/blog\/SFDCInterviewList.php?num=21\" width=\"300\" height=\"150\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/div>\n<p><strong>201. Common <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.pages.meta\/pages\/pages_compref_page.htm\">Apex page<\/a> attributes.<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:page sidebar=&quot;false&quot; standardStylesheets=&quot;false&quot; showHeader=&quot;false&quot;&gt;\r\n<\/pre>\n<p><strong>202. Declare Visualforce page as HTML5.<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:page docType=&quot;html-5.0&quot; \/&gt;\r\n<\/pre>\n<p><strong>203. Visualforce page <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/json-output-in-visualforce\/\">output as JSON<\/a>.<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:page controller=&quot;ControllerName&quot;  contentType=&quot;application\/x-JavaScript; charset=utf-8&quot; showHeader=&quot;false&quot; standardStylesheets=&quot;false&quot; sidebar=&quot;false&quot;&gt;\r\n{!jsonString}\r\n&lt;\/apex:page&gt;\r\n<\/pre>\n<p><!--more--><\/p>\n<p><strong>204. How to refer <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.pages.meta\/pages\/pages_resources_reference.htm\">static resources in Visualforce<\/a>.<\/strong><br \/>\nCSS File :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:stylesheet value=&quot;{!URLFOR($Resource.style_resources, 'styles.css')}&quot;\/&gt;\r\n<\/pre>\n<p>Relative path in CSS from static resource, You can use relative paths in files in static resource archives to refer to other content within the archive.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ntable { background-image: img\/testimage.gif }\r\n<\/pre>\n<p>Image tag:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:image url=&quot;{!$Resource.TestImage}&quot; width=&quot;50&quot; height=&quot;50&quot;\/&gt;\r\n<\/pre>\n<p>or<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:image url=&quot;{!URLFOR($Resource.TestZip, 'images\/Bluehills.jpg')}&quot; width=&quot;50&quot; height=&quot;50&quot;\/&gt;\r\n<\/pre>\n<p>Include Javascript in Visualforce from Static resource<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:image url=&quot;{!$Resource.TestImage}&quot; width=&quot;50&quot; height=&quot;50&quot;\/&gt;\r\n<\/pre>\n<p>Refer static resource path from Aprx Controller<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nglobal class MyController {\r\n    public String getImageName() {\r\n        return 'Picture.gif';\/\/this is the name of the image\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:page renderAs=&quot;pdf&quot; controller=&quot;MyController&quot;&gt;\r\n    &lt;apex:variable var=&quot;imageVar&quot; value=&quot;{!imageName}&quot;\/&gt;\r\n    &lt;apex:image url=&quot;{!URLFOR($Resource.myZipFile, imageVar)}&quot;\/&gt;\r\n&lt;\/apex:page&gt;\r\n<\/pre>\n<p><strong>205. How to get element id of Visualforce components to be used in Javascript ?<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n{!$Component.Parent1.Parent2.fieldId}\r\n<\/pre>\n<p><a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/get-dom-elementid-of-the-visualforce-components\/\">read more in this post<\/a>.<\/p>\n<p><strong>206. Autogenerated Salesforce Id consist of colon, how to handle it in JQuery ?<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvar ele = $('&#x5B;id=&quot;abc:xyz&quot;]');\r\n<\/pre>\n<p><a href=\"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/handling-colon-in-element-id-in-jquery-visualforce-problem\/\">read more about problem here<\/a>.<\/p>\n<p><strong>207. How to return Map result from SOQL query in Apex.<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nMap&lt;ID, Contact&gt; m = new Map&lt;ID, Contact&gt;(&#x5B;SELECT Id, LastName FROM Contact]);\r\n<\/pre>\n<p><strong>208. How to query and abort scheduled job using Apex.<\/strong><\/p>\n<p>While updating class on Sandboxes, chances are high that it is being used in any scheduler, so below code will abort all scheduled job. If there are 150+ scheduled job, then you might want to use below code again and again in developer console until all jobs are removed from system.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/Limit is 150 because System.abortJob counted against DML \r\nList&lt;CronTrigger&gt; abort_job = &#x5B;SELECT Id FROM CronTrigger WHERE State != 'Deleted' limit 150];\r\n    for (CronTrigger t : abort_job) { \/\/for each record\r\n     \/\/try catch - to make sure one fail should not impact other jobs which needs to be cancelled\r\n     try{\r\n        System.abortJob(t.Id); \/\/abort the job\r\n     }catch(Exception e){}\r\n     \r\n    }\r\n<\/pre>\n<p><strong>209. How to use standard Salesforce REST API from Visualforce page ?<\/strong><br \/>\nBefor any Ajax call, make sure to add &#8216;Bearer&#8217; token in header. If using JQuery use below code snippet. For more information <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-rest-api-playground\/\">read this post<\/a>.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n$.ajax({\r\n          type: reqType,\r\n          beforeSend: function (xhr)\r\n                {\r\n                    xhr.setRequestHeader(&quot;Authorization&quot;,  'Bearer {!$API.Session_ID}');\r\n\r\n                },\r\n          headers : {'Content-Type' : 'application\/json; charset=utf-8'},\r\n          url: postUrl,\r\n          data: postData,\r\n          dataType: 'text'\r\n        })\r\n         .done(function( data ) {\r\n                \/\/Code if success\r\n          })\r\n          .fail(function(xhr,textstatus,error){\r\n             \/\/Code if fail\r\n          });\r\n<\/pre>\n<p><strong>210. How to create Chatter post from Apex.<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/Adding a Text post\r\nFeedItem post = new FeedItem();\r\npost.ParentId = oId; \/\/eg. Opportunity id, custom object id..\r\npost.Body = 'Enter post text here';\r\ninsert post;\r\n\r\n\/\/Adding a Link post\r\nFeedItem post = new FeedItem();\r\npost.ParentId = oId; \/\/eg. Opportunity id, custom object id..\r\npost.Body = 'Enter post text here';\r\npost.LinkUrl = 'http:\/\/www.someurl.com';\r\ninsert post;\r\n\r\n\/\/Adding a Content post\r\nFeedItem post = new FeedItem();\r\npost.ParentId = oId; \/\/eg. Opportunity id, custom object id..\r\npost.Body = 'Enter post text here';\r\npost.ContentData = base64EncodedFileData;\r\npost.ContentFileName = 'sample.pdf';\r\ninsert post;\r\n<\/pre>\n<p><a href=\"https:\/\/developer.salesforce.com\/page\/Chatter_Code_Recipes\">read all about chatter and Apex here<\/a>.<\/p>\n<div class=\"intrinsic-container\"><iframe loading=\"lazy\" src=\"https:\/\/jitendrazaa.com\/blog\/SFDCInterviewList.php?num=21\" width=\"300\" height=\"150\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Salesforce interview questions &#8211; Most frequently used Apex and visualforce code used by Salesforce developers like &#8220;How to query and abort scheduled job using Apex&#8221;, &#8220;Defining VF page as HTML5&#8221;, &#8220;Visualforce page as JSON&#8221; , &#8220;Handling colon in element Id for Jquery&#8221; , &#8220;Chatter using Apex&#8221; and many more.<\/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_post_was_ever_published":false,"_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_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[55,124,331,325,336],"class_list":["post-4618","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-chatter","tag-interview-questions","tag-salesforce","tag-static-resource","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2808,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/json-output-in-visualforce\/","url_meta":{"origin":4618,"position":0},"title":"JSON output in Visualforce","author":"Jitendra","date":"April 18, 2012","format":false,"excerpt":"The Example of generating the JSON Output in Visualforce page using JSON.serialize method provided by Salesforce","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Visualforce page output as JSON","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/04\/Visualforce-page-output-as-JSON.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/04\/Visualforce-page-output-as-JSON.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/04\/Visualforce-page-output-as-JSON.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3347,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/ajax-based-autocomplete-component-in-salesforce-using-jquery-ui-and-json\/","url_meta":{"origin":4618,"position":1},"title":"AutoComplete Component in Visualforce using JQueryUI","author":"Jitendra","date":"June 28, 2013","format":false,"excerpt":"In this tutorial, I am going to explain very Simple AJAX and JSON based Auto Complete component with the help of JQuery UI. First I am assuming that you already have Static Resource of named \"AutoCompleteWithModal\"\u009d. This Static resource has all images, CSS and JQuery library needed to implement this\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"JQuery UI and JSON based AJAX AutoComplete Component in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-UI-and-JSON-based-AJAX-AutoComplete-Component-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-UI-and-JSON-based-AJAX-AutoComplete-Component-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-UI-and-JSON-based-AJAX-AutoComplete-Component-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2987,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-10\/","url_meta":{"origin":4618,"position":2},"title":"Salesforce Interview Questions \u2013 Part 10","author":"Jitendra","date":"August 2, 2012","format":false,"excerpt":"This Part of Salesforce interview question series depict on browser compatibility issue (Internet Explorer 9) and Visualforce normally for AJAX, Group By and Having Clause. 91. How to add the Document Header in Visualforce page? Ans : Directly there is no way to add the document type in visualforce. However\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":4486,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/continuation-object-in-apex-asynchronous-callouts-for-long-running-request-live-demo\/","url_meta":{"origin":4618,"position":3},"title":"Continuation object in Apex &#8211; Asynchronous callouts for long running request &#8211; Live Demo","author":"Jitendra","date":"May 22, 2015","format":false,"excerpt":"Check the below video first if you are planning to use Continuation https:\/\/youtu.be\/ya2N9EX9dmg We may run into\u00a0scenario in Salesforce project, where we need call external web service but no need to\u00a0wait for response. Response from external web service can be processed asynchronously and once processed it can be presented in\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Execution Flow of an Asynchronous Callout - Image from Salesforce documentation","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3917,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/ajax-based-autocomplete-typeahead-directive-in-angularjs\/","url_meta":{"origin":4618,"position":4},"title":"Ajax based AutoComplete \/ TypeAhead Directive in AngularJS","author":"Jitendra","date":"July 15, 2014","format":false,"excerpt":"Previously we already discussed below Auto Complete components: Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net AutoComplete Component in Visualforce using JQueryUI In this article, we will be creating TypeAhead Directive (Auto Complete) again in Salesforce However this time we will use AngularJs. Why we are using AngularJS ? We\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"TypeAhead Demo using AngularJs","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/07\/TypeAhead-Demo-using-AngularJs.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/07\/TypeAhead-Demo-using-AngularJs.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/07\/TypeAhead-Demo-using-AngularJs.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/07\/TypeAhead-Demo-using-AngularJs.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3025,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-11\/","url_meta":{"origin":4618,"position":5},"title":"Salesforce Interview Questions \u2013 Part 11","author":"Jitendra","date":"August 19, 2012","format":false,"excerpt":"101. How to force lead assignment rule via Apex while updating or adding the Lead? Ans : To enforce Assignment Rules in Apex you will need to perform following steps: Instantiate the \"Database.DMLOptions\"\u009d class. Set the \"useDefaultRule\"\u009d property of \"assignmentRuleHeader\"\u009d to True. Finally call a native method on your Lead\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Pagination in SOQL using keyword Offset","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.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\/4618","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=4618"}],"version-history":[{"count":11,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4618\/revisions"}],"predecessor-version":[{"id":4992,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4618\/revisions\/4992"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=4618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=4618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=4618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}