Tuesday, July 4, 2017

Salesforce Developers interview questions – Most commonly used code snippets – part 21

Salesforce interview questions – Most frequently used Apex and visualforce code used by Salesforce developers like “How to query and abort scheduled job using Apex”, “Defining VF page as HTML5”, “Visualforce page as JSON” , “Handling colon in element Id for Jquery” , “Chatter using Apex” and many more.

201. Common Apex page attributes.
1<apex:page sidebar="false" standardStylesheets="false" showHeader="false">
202. Declare Visualforce page as HTML5.
1<apex:page docType="html-5.0" />
203. Visualforce page output as JSON.
1<apex:page controller="ControllerName"  contentType="application/x-JavaScript; charset=utf-8"showHeader="false" standardStylesheets="false" sidebar="false">
2{!jsonString}
3</apex:page>
204. How to refer static resources in Visualforce.
CSS File :
1<apex:stylesheet value="{!URLFOR($Resource.style_resources, 'styles.css')}"/>
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.
1table { background-image: img/testimage.gif }
Image tag:
1<apex:image url="{!$Resource.TestImage}" width="50" height="50"/>
or
1<apex:image url="{!URLFOR($Resource.TestZip, 'images/Bluehills.jpg')}" width="50" height="50"/>
Include Javascript in Visualforce from Static resource
1<apex:image url="{!$Resource.TestImage}" width="50" height="50"/>
Refer static resource path from Aprx Controller
1global class MyController {
2    public String getImageName() {
3        return 'Picture.gif';//this is the name of the image
4    }
5}
1<apex:page renderAs="pdf" controller="MyController">
2    <apex:variable var="imageVar" value="{!imageName}"/>
3    <apex:image url="{!URLFOR($Resource.myZipFile, imageVar)}"/>
4</apex:page>
205. How to get element id of Visualforce components to be used in Javascript ?
1{!$Component.Parent1.Parent2.fieldId}
206. Autogenerated Salesforce Id consist of colon, how to handle it in JQuery ?
1var ele = $('[id="abc:xyz"]');
207. How to return Map result from SOQL query in Apex.
1Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);
208. How to query and abort scheduled job using Apex.
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.
1//Limit is 150 because System.abortJob counted against DML
2List<CronTrigger> abort_job = [SELECT Id FROM CronTrigger WHERE State != 'Deleted' limit 150];
3    for (CronTrigger t : abort_job) { //for each record
4     //try catch - to make sure one fail should not impact other jobs which needs to be cancelled
5     try{
6        System.abortJob(t.Id); //abort the job
7     }catch(Exception e){}
8      
9    }
209. How to use standard Salesforce REST API from Visualforce page ?
Befor any Ajax call, make sure to add ‘Bearer’ token in header. If using JQuery use below code snippet. For more information read this post.
1$.ajax({
2          type: reqType,
3          beforeSend: function (xhr)
4                {
5                    xhr.setRequestHeader("Authorization",  'Bearer {!$API.Session_ID}');
6 
7                },
8          headers : {'Content-Type' 'application/json; charset=utf-8'},
9          url: postUrl,
10          data: postData,
11          dataType: 'text'
12        })
13         .done(function( data ) {
14                //Code if success
15          })
16          .fail(function(xhr,textstatus,error){
17             //Code if fail
18          });
210. How to create Chatter post from Apex.
1//Adding a Text post
2FeedItem post = new FeedItem();
3post.ParentId = oId; //eg. Opportunity id, custom object id..
4post.Body = 'Enter post text here';
5insert post;
6 
7//Adding a Link post
8FeedItem post = new FeedItem();
9post.ParentId = oId; //eg. Opportunity id, custom object id..
10post.Body = 'Enter post text here';
11post.LinkUrl = 'http://www.someurl.com';
12insert post;
13 
14//Adding a Content post
15FeedItem post = new FeedItem();
16post.ParentId = oId; //eg. Opportunity id, custom object id..
17post.Body = 'Enter post text here';
18post.ContentData = base64EncodedFileData;
19post.ContentFileName = 'sample.pdf';
20insert post;

No comments:

Post a Comment