{"id":3411,"date":"2013-07-28T13:27:42","date_gmt":"2013-07-28T07:57:42","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=3411"},"modified":"2015-11-03T21:54:45","modified_gmt":"2015-11-03T21:54:45","slug":"apex-interview-question-salesforce-part-16","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","title":{"rendered":"Apex Interview Question \u2013 Salesforce &#8211; Part 16"},"content":{"rendered":"<div class=\"intrinsic-container\"><iframe loading=\"lazy\" src=\"https:\/\/jitendrazaa.com\/blog\/SFDCInterviewList.php?num=16\" width=\"300\" height=\"150\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/div>\n<p><strong>151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ?<\/strong><br \/>\n<strong> Ans :<\/strong> It can be done with help of External Id.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nDate dt = Date.today().addDays(7);\r\nOpportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting', CloseDate = dt);\r\n\r\n\/*\r\nCreate the parent reference. Used only for foreign key reference  and doesn't contain any other fields. If we provide any other value it will give following error\r\n\r\nSystem.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity: Account: &#x5B;]\r\n*\/\r\n\r\nAccount accountReference = new Account(MyExtID__c = 'SHIVA1234567');\r\nnewOpportunity.Account = accountReference;\r\n\r\n\/\/  Create the Account object to insert.  Same as above but has Name field.  Used for the insert.\r\nAccount parentAccount = new Account(Name = 'Shiva', MyExtID__c = 'SHIVA1234567');\r\n\r\nDatabase.SaveResult&#x5B;]\r\n    results = Database.insert(new SObject&#x5B;] {  parentAccount, newOpportunity });\r\n\r\n<\/pre>\n<hr \/>\n<p><strong>152 . Which SOQL statement can be used to get all records even from recycle bin or Achieved Activities?<\/strong><br \/>\n<strong> Ans :<\/strong> We will need &#8220;ALL Rows&#8221; clause of SOQL.<br \/>\nSample :<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT COUNT() FROM Contact WHERE AccountId = a.Id ALL ROWS\r\n<\/pre>\n<hr \/>\n<p><strong>153. How can you lock record using SOQL so that it cannot be modified by other user.<\/strong><br \/>\n<strong> Ans :<\/strong> we will need &#8220;FOR UPDATE&#8221; clause of SOQL.<br \/>\nSample :<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n Account &#x5B;] accts = &#x5B;SELECT Id FROM Account LIMIT 2 FOR UPDATE];\r\n<\/pre>\n<hr \/>\n<p><strong><!--more-->154. If you set more than one savepoint, then roll back to a savepoint that is not the last savepoint you generated, What will happen to later savepoint variables ?<\/strong><br \/>\n<strong> Ans :<\/strong> if you generated savepoint SP1 first, savepoint SP2 after that, and then you rolled back to SP1, the variable SP2 would no longer be valid. You will receive a runtime error if you try to use it.<\/p>\n<hr \/>\n<p><strong>155. What are few limitations (points to remember) of Savepoint or Transaction Control in Apex ?<\/strong><br \/>\n<strong> Ans :<\/strong><\/p>\n<ul>\n<li>Each savepoint you set counts against the governor limit for DML statements.<\/li>\n<li>Static variables are not reverted during a rollback. If you try to run the trigger again, the static variables retain the values from the first run.<\/li>\n<li>Each rollback counts against the governor limit for DML statements. You will receive a Runtime error if you try to rollback the database additional times.<\/li>\n<li>The ID on an sObject inserted after setting a savepoint is not cleared after a rollback.<\/li>\n<\/ul>\n<hr \/>\n<p><strong>156. What are few Considerations about Trigger ?<\/strong><br \/>\n<strong> Ans :<\/strong><\/p>\n<ul>\n<li><strong>upsert<\/strong> triggers fire both before and after insert or before and after update triggers as appropriate.<\/li>\n<li><strong>merge<\/strong> triggers fire both before and after delete triggers for the losing records and before update triggers for the winning record only.<\/li>\n<li>Triggers that execute after a record has been <strong>undeleted<\/strong> only work with specific objects.<\/li>\n<li>Field history is not recorded until the end of a trigger. If you query field history in a trigger, you will not see any history for the current transaction.<\/li>\n<li>You can only use the <strong>webService<\/strong> keyword in a trigger when it is in a method defined as asynchronous; that is, when the method is defined with the <strong>@future<\/strong> keyword.<\/li>\n<li>A trigger invoked by an insert, delete, or update of a <strong>recurring event<\/strong> or <strong>recurring task<\/strong> results in a runtime error when the trigger is called in bulk from the Force.com API.<\/li>\n<li><strong>Merge<\/strong> trigger doesn&#8217;t fire there own trigger instead they fire delete and update of loosing and winning records respectively.<\/li>\n<\/ul>\n<hr \/>\n<p><strong>157. How to execute Apex from Custom button or Javascript\u00a0? Give Example.<\/strong><br \/>\n<strong> Ans :<\/strong><\/p>\n<p>It is possible using <a href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/ajax-toolkit\/\">Ajax toolkiit<\/a>.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nglobal class myClass {\r\n\twebService static Id makeContact (String lastName, Account a) {\r\n\t\tContact c = new Contact(LastName = lastName, AccountId = a.Id);\r\n\t\treturn c.id;\r\n\t}\r\n}\r\n<\/pre>\n<p>we can execute above method from javascript like :<\/p>\n<pre class=\"brush: java; 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\nvar account = sforce.sObject(&quot;Account&quot;);\r\nvar id = sforce.apex.execute(&quot;myClass&quot; , &quot;makeContact&quot;,\r\n{lastName:&quot;Smith&quot;, a:account});\r\n<\/pre>\n<p>To call a webService method with no parameters, use {} as the third parameter for <strong> sforce.apex.execute <\/strong>.<\/p>\n<p>Also, you can use the following line to display a popup window with debugging information:<br \/>\n<strong> sforce.debug.trace=true; <\/strong><\/p>\n<hr \/>\n<p><strong>158. \u00a0What is difference between public and global class in Apex ?<\/strong><br \/>\n<strong> Ans :<\/strong><\/p>\n<ul>\n<li>Public class can be accessed within application or namespace. This is not exactly like public modifier in Java.<\/li>\n<li>Global class visible everywhere , any application or namespace. WebService must be declared as Global and which can be accessed inside Javascript also. It is like public modifier in Java.<\/li>\n<\/ul>\n<hr \/>\n<p><strong>159. Explain Considerations for Static keyword in Apex.<\/strong><\/p>\n<p><strong>Ans :<\/strong><\/p>\n<ul>\n<li>Apex classes cannot be static.<\/li>\n<li>Static allowed only in outer class.<\/li>\n<li>Static variables not transferred as a part of View State.<\/li>\n<li>Static variables and static block runs in order in which they are written in class.<\/li>\n<li>Static variables are static only in scope of request.<\/li>\n<\/ul>\n<hr \/>\n<p><strong>160. Explain few considerations for @Future annotation in Apex.<\/strong><\/p>\n<p><strong>Ans :<\/strong><\/p>\n<ul>\n<li>Method must be static<\/li>\n<li>Cannot return anything ( Only Void )<\/li>\n<li>To test @future methods, you should use startTest and stopTest to make it synchromouse inside Test class.<\/li>\n<li>Parameter to @future method can only be primitive or collection of primitive data type.<\/li>\n<li>Cannot be used inside VF in Constructor, Set or Get methods.<\/li>\n<li>@future method cannot call other @future method.<\/li>\n<\/ul>\n<div class=\"intrinsic-container\"><iframe loading=\"lazy\" src=\"https:\/\/jitendrazaa.com\/blog\/SFDCInterviewList.php?num=16\" width=\"300\" height=\"150\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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. Date dt = Date.today().addDays(7); Opportunity newOpportunity = new Opportunity(Name = &#8216;shivasoft&#8217;, StageName = &#8216;Prospecting&#8217;, CloseDate = dt); \/* Create the [&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_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":[20,9],"tags":[270,124,185],"class_list":["post-3411","post","type-post","status-publish","format-standard","hentry","category-apex","category-salesforce","tag-ajax-toolkit","tag-interview-questions","tag-soql"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":4447,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/all-about-upsert-and-external-id-in-dataloader-and-apex-videos\/","url_meta":{"origin":3411,"position":0},"title":"All about Upsert and External ID in Dataloader and Apex &#8211; Videos","author":"Jitendra","date":"May 11, 2015","format":false,"excerpt":"You may be wondering that there are tons of articles available for upsert operation and what is need of one more blog post. I wanted to note everything important about upsert operations using datalaoder and Apex with identified gotchas at single place, and that's why its here :) . External\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Field Mapping to create Parent Child relationship using ExternalId in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Field-Mapping-to-create-Parent-Child-relationship-using-ExternalId-in-Salesforce-e1431304939365.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Field-Mapping-to-create-Parent-Child-relationship-using-ExternalId-in-Salesforce-e1431304939365.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Field-Mapping-to-create-Parent-Child-relationship-using-ExternalId-in-Salesforce-e1431304939365.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3325,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-visualforce-data-loader-and-soql-interview-question-part-17\/","url_meta":{"origin":3411,"position":1},"title":"Apex, Visualforce, Data Loader and SOQL Interview Question \u2013 Part 17","author":"Jitendra","date":"October 10, 2013","format":false,"excerpt":"161 : Sometimes while deleting record it gives error \"Object cannot be Deleted\". What is the reason for this kind of error ? Ans : This is generic error message prompted by Salesforce many times, which is not well informative. To get informative message, we can try to delete same\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":3025,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-11\/","url_meta":{"origin":3411,"position":2},"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":[]},{"id":2762,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-6\/","url_meta":{"origin":3411,"position":3},"title":"Salesforce Interview Questions &#8211; Part 6","author":"Jitendra","date":"March 10, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce - External Id option while creating field","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4174,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-22\/","url_meta":{"origin":3411,"position":4},"title":"Salesforce Interview Questions &#8211; Part 22","author":"Jitendra","date":"December 1, 2015","format":false,"excerpt":"Consider it Facts or FAQ or interview questions but its small and important notes about Salesforce. More than 200 interview questions for Salesforce developer, Admin and consultants","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"220+ Salesforce Interview Questions","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/11\/220plus-Interview-Questions-1024x303.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/11\/220plus-Interview-Questions-1024x303.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/11\/220plus-Interview-Questions-1024x303.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2681,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-5\/","url_meta":{"origin":3411,"position":5},"title":"Salesforce Interview Questions &#8211; Part 5","author":"Jitendra","date":"January 24, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3411","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=3411"}],"version-history":[{"count":4,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3411\/revisions"}],"predecessor-version":[{"id":5002,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3411\/revisions\/5002"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=3411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=3411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=3411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}