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

201. Common Apex page attributes.

<apex:page sidebar="false" standardStylesheets="false" showHeader="false">

202. Declare Visualforce page as HTML5.

<apex:page docType="html-5.0" />

203. Visualforce page output as JSON.

<apex:page controller="ControllerName"  contentType="application/x-JavaScript; charset=utf-8" showHeader="false" standardStylesheets="false" sidebar="false">
{!jsonString}
</apex:page>

204. How to refer static resources in Visualforce.
CSS File :

<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.

table { background-image: img/testimage.gif }

Image tag:

<apex:image url="{!$Resource.TestImage}" width="50" height="50"/>

or

<apex:image url="{!URLFOR($Resource.TestZip, 'images/Bluehills.jpg')}" width="50" height="50"/>

Include Javascript in Visualforce from Static resource

<apex:image url="{!$Resource.TestImage}" width="50" height="50"/>

Refer static resource path from Aprx Controller

global class MyController {
    public String getImageName() {
        return 'Picture.gif';//this is the name of the image
    }
}
<apex:page renderAs="pdf" controller="MyController">
    <apex:variable var="imageVar" value="{!imageName}"/>
    <apex:image url="{!URLFOR($Resource.myZipFile, imageVar)}"/>
</apex:page>

205. How to get element id of Visualforce components to be used in Javascript ?

{!$Component.Parent1.Parent2.fieldId}

read more in this post.

206. Autogenerated Salesforce Id consist of colon, how to handle it in JQuery ?

var ele = $('[id="abc:xyz"]');

read more about problem here.

207. How to return Map result from SOQL query in Apex.

Map<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.

//Limit is 150 because System.abortJob counted against DML 
List<CronTrigger> abort_job = [SELECT Id FROM CronTrigger WHERE State != 'Deleted' limit 150];
    for (CronTrigger t : abort_job) { //for each record
     //try catch - to make sure one fail should not impact other jobs which needs to be cancelled
     try{
        System.abortJob(t.Id); //abort the job
     }catch(Exception e){}
     
    }

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.

$.ajax({
          type: reqType,
          beforeSend: function (xhr)
                {
                    xhr.setRequestHeader("Authorization",  'Bearer {!$API.Session_ID}');

                },
          headers : {'Content-Type' : 'application/json; charset=utf-8'},
          url: postUrl,
          data: postData,
          dataType: 'text'
        })
         .done(function( data ) {
                //Code if success
          })
          .fail(function(xhr,textstatus,error){
             //Code if fail
          });

210. How to create Chatter post from Apex.

//Adding a Text post
FeedItem post = new FeedItem();
post.ParentId = oId; //eg. Opportunity id, custom object id..
post.Body = 'Enter post text here';
insert post;

//Adding a Link post
FeedItem post = new FeedItem();
post.ParentId = oId; //eg. Opportunity id, custom object id..
post.Body = 'Enter post text here';
post.LinkUrl = 'http://www.someurl.com';
insert post;

//Adding a Content post
FeedItem post = new FeedItem();
post.ParentId = oId; //eg. Opportunity id, custom object id..
post.Body = 'Enter post text here';
post.ContentData = base64EncodedFileData;
post.ContentFileName = 'sample.pdf';
insert post;

read all about chatter and Apex here.

Posted

in

by


Related Posts

Comments

One response to “Salesforce Developers interview questions – Most commonly used code snippets – part 21”

  1. Sidharth Negi Avatar
    Sidharth Negi

    To include javascript don’t you need apex:includeScript or something

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading