{"id":2501,"date":"2011-11-27T01:29:23","date_gmt":"2011-11-26T19:59:23","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2501"},"modified":"2015-02-09T01:28:52","modified_gmt":"2015-02-09T01:28:52","slug":"very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4\/","title":{"rendered":"Latest Salesforce Interview Questions &#8211; Part 4 &#8211; Related to Dynamic Apex"},"content":{"rendered":"<div class=\"intrinsic-container\"><iframe loading=\"lazy\" src=\"https:\/\/jitendrazaa.com\/blog\/SFDCInterviewList.php?num=4\" width=\"300\" height=\"150\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/div>\n<p>This part of the interview question mainly focus on the dynamic Apex feature of the salesforce.com .<\/p>\n<p><strong>30 : What is the dynamic Apex?<br \/>\nAns :<\/strong><br \/>\nDynamic Apex enables developers to create more flexible applications by providing them with the ability to <strong>&#8220;Access sObject and field describe information&#8221;, &#8220;Write Dynamic SOQL Queries&#8221;, &#8220;Write Dynamic SOSL Queries&#8221;<\/strong> and <strong>&#8220;Dynamic DML&#8221;<\/strong>.<\/p>\n<hr \/>\n<p><strong>31 : How to get the list of all available sobject in salesforce database using Apex (Dynamic Apex)?<br \/>\nAns:<\/strong><br \/>\n<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nMap&lt;String, Schema.SObjectType&gt; m =  Schema.getGlobalDescribe();\r\n<\/pre>\n<hr \/>\n<p><strong>32 : How to create instance of sobject dynamically? Normally the sobject is created like &#8220;Account a = new Account();&#8221;. But if you are in situation that you don&#8217;t know which sobject is going to be instantiated ? Means it will be decided at runtime, how you will handle it? Hint : Use Dynamic Apex.<br \/>\nAns:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic SObject getNewSobject(String t)\r\n{\r\n\r\n\t\/\/ Call global describe to get the map of string to token.\r\n\tMap&lt;String, Schema.SObjectType&gt; gd = Schema.getGlobalDescribe();\r\n\r\n\t\/\/ Get the token for the sobject based on the type.\r\n\tSchema.SObjectType st = gd.get(t);\r\n\r\n\t\/\/ Instantiate the sobject from the token.\r\n\tSobject s = st.newSobject();\r\n\r\n\treturn s;\r\n}\r\n<\/pre>\n<hr \/>\n<p><strong>33 : How to get all the fields of sObject using dynamic Apex?<br \/>\nAns:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nMap&lt;String, Schema.SObjectType&gt; m  = Schema.getGlobalDescribe() ;\r\nSchema.SObjectType s = m.get('API_Name_Of_SObject') ;\r\nSchema.DescribeSObjectResult r = s.getDescribe() ;\r\nMap&lt;String,Schema.SObjectField&gt; fields = r.fields.getMap() ;\r\n<\/pre>\n<hr \/>\n<p><strong>34 : How to get all the required fields of sObject dynamically?<br \/>\nAns:<\/strong><br \/>\nThere is no direct property available in Apex dynamic API to represent the required field. However there is another way to know about it.<br \/>\nIf any field have below three properties then it is mandatory field.<\/p>\n<ol>\n<li>If it is Creatable<\/li>\n<li>If it is not nillable and<\/li>\n<li>If it does not have any default value<\/li>\n<\/ol>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nMap&lt;String, Schema.SObjectType&gt; m  = Schema.getGlobalDescribe() ;\r\nSchema.SObjectType s = m.get(so.apiName) ;\r\nSchema.DescribeSObjectResult r = s.getDescribe() ;\r\nMap&lt;String,Schema.SObjectField&gt; fields = r.fields.getMap() ;\r\n\r\nfor(String f : fields.keyset())\r\n{\r\n\tSchema.DescribeFieldResult desribeResult = fields.get(f).getDescribe();\r\n\tif( desribeResult.isCreateable()  &amp;&amp; !desribeResult.isNillable() &amp;&amp; !desribeResult.isDefaultedOnCreate() )\r\n\t{\r\n\/\/This is mandatory \/ required field\r\n\t}\r\n}\r\n<\/pre>\n<hr \/>\n<p><strong>35 : How to display error messages in the visualforce page ?<\/strong><br \/>\n<strong> Ans:<\/strong><br \/>\nIn Apex use below code to create the error message for visualforce.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nApexpages.addMessage( new ApexPages.Message (ApexPages.Severity.ERROR, 'Required fields are missing. '));\r\n<\/pre>\n<p>in Visualforce page add below tag where you want to display the error message.<\/p>\n<p>&lt;apex:pageMessages &gt;&lt;\/apex:pageMessages&gt;<\/p>\n<hr \/>\n<p><strong>36 : What is property in Apex? Explain with advantages.<br \/>\nAns: <\/strong><br \/>\nApex mainly consist of the syntax from the well known programming language Java. As a practice of <strong>encapsulation <\/strong>in java we declare any variable as private and then creates the setters and getters for that variable.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate String name;\r\npublic void setName(String n)\r\n{\r\n  name = n;\r\n}\r\npublic String getName()\r\n{\r\n return name;\r\n}\r\n<\/pre>\n<p>However, the Apex introduced the new concept of property from language C# as shown below:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic String name {get; set;}\r\n<\/pre>\n<p>As we can see how simple the code is and instead of using nearly 8 to 11 lines all done in 1 line only. It will be very useful when lots of member is declared in Apex class. It has another advantage in &#8220;number of lines of code&#8221; limit by salesforce which will drastically reduced.<\/p>\n<hr \/>\n<p><strong>37 : What is the controller extension ?<br \/>\nAns:<\/strong><br \/>\nAny apex class having a public constructor with Custom Controller or Standard Controller object as a single argument is known as controller extension.<\/p>\n<hr \/>\n<p><strong>38 : Explain the need or importance of the controller extension.<br \/>\nAns:<\/strong><br \/>\nController extension is very useful and important concept introduced by the salesforce recently. It gives the power to programmer to extend the functionality of existing custom controller or standard controller.<br \/>\nA Visualforce can have a single Custom controller or standard controller but many controller extensions.<br \/>\nwe can say that the custom extension is the supporter of custom or standard controller.<br \/>\nConsider one example : If there is one controller written and used by the multiple visualforce pages and one of them needs some extra logic. Then instead of writing that logic to controller class (Which is used by many visualforce pages) we can create a controller extension and apply to that page only.<\/p>\n<hr \/>\n<p><strong>39 : How to read the parameter value from the URL in Apex?<br \/>\nAns:<\/strong><br \/>\nConsider that the parameter name is &#8220;RecordType&#8221;.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString recordType = Apexpages.currentPage().getParameters().get('RecordType');\r\n<\/pre>\n<hr \/>\n<div class=\"intrinsic-container\"><iframe loading=\"lazy\" src=\"https:\/\/jitendrazaa.com\/blog\/SFDCInterviewList.php?num=4\" width=\"300\" height=\"150\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC<\/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":[337,331,336],"class_list":["post-2501","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-apex","tag-salesforce","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3042,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-question-part-12\/","url_meta":{"origin":2501,"position":0},"title":"Salesforce Interview Question &#8211; Part 12","author":"Jitendra","date":"September 13, 2012","format":false,"excerpt":"111 : How to get the Recordtype Id using Dynamic Apex? Ans: Normally to get the RecordtypeId for any sObject we use SOQL and it will count against your limit. So below method will bypass the need of SOQL Query. [java] Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ; Schema.SObjectType s =\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":3773,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-faq-part-19\/","url_meta":{"origin":2501,"position":1},"title":"Salesforce interview questions &#8211; Part 19","author":"Jitendra","date":"December 16, 2014","format":false,"excerpt":"Salesforce interview questions for developers and admins around Apex, Visualforce, getting Salesforce object name on basis of Id, Apex API limits","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":1305,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-3\/","url_meta":{"origin":2501,"position":2},"title":"Salesforce Interview Questions \u2013 Part 3","author":"Jitendra","date":"October 12, 2010","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Visual force, SOQL in Salesforce.com SFDC","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":3050,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/my-favorite-top-10-features-of-winter-13-release-salesforce\/","url_meta":{"origin":2501,"position":3},"title":"My Favorite Top 10 Features of Winter 13 release &#8211; Salesforce","author":"Jitendra","date":"August 24, 2012","format":false,"excerpt":"Dear Friends, I am very excited to write this article about the cool Winter 13 features which i have added in My List. There are lots of lots of new features added in this release and i have made the list of my top 10 favorite features. Yesterday Salesforce published\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Winter 13 Release Notes","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Salesforce-Winter-13-Release-Notes-300x212.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":2501,"position":4},"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":5152,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-test-loaddata-to-import-records-with-relationship\/","url_meta":{"origin":2501,"position":5},"title":"Using Test.loadData to import records with relationship","author":"Jitendra","date":"January 6, 2016","format":false,"excerpt":"There are many resources and documents available around how to use Test.loadData to create test records in Apex class. As per best practice of writing Test classes in Apex, Its good idea to store master data (aka Seed, Reference data) in static resource and load\u00a0it in Test classes using \"Test.loadData\"\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Test.loadData and Static resource in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2501","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=2501"}],"version-history":[{"count":3,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2501\/revisions"}],"predecessor-version":[{"id":4218,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2501\/revisions\/4218"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}