How to access ListView in Apex | Using StandardSetController for Pagination

There are scenario in project lifecycle where developer creates SOQL or Dynamic SOQL to return expected result. Also if requirement changes they go back and change existing code to reflect updated SOQL. If you are good developer and avoid to change Apex code at most you will save your SOQL in “Custom Settings” and utilize in Dynamic SOQL.

However, There is very good design Developer can suggest to client. Instead of going to your code and changing code for new requirement, how it sounds if we can utilize existing List View inside Apex or Visualforce only 🙂 ? It will solve lots of problems and if requirement changed, just change or create new Listview and it will reflect in your Visualforce code. We can almost have same functionality like Navigation buttons and Paging without writing any complex code logic, thanks to StandardSetController capabilities.

There is also very good article on Salesforce blog on various approaches suggested for pagination and one of them is using StandardSetController.

To keep article simple, I am posting complete Visualforce and Apex code with Comments inside. Account object is used as an example here. Continue reading “How to access ListView in Apex | Using StandardSetController for Pagination”

Consuming External Web Service in Apex – Salesforce

One of the feature we have in Salesforce is that we can easily consume External Web Services. In this article, we will learn step by step demo of consuming Web Service in Apex. There are many public websites available to consume Web Service and one of them, I am using in this article is , from this location we are using Stock Quote Webservice. To consume this, we need WSDL.

The Web Services Description Language (WSDL) is an XML-based interface description language that is used for describing the functionality offered by a Web Service.

WSDL have to be downloaded from this location. We need this WSDL in later part of this tutorial.

Once, WSDL is downloaded and saved on local drive. We have to go to Salesforce and navigate to “Setup | Develop | Apex Classes”. On right hand side, you will find button named as “Generate from WSDL”. This button will generate equivalent Apex class to support Webservice call. In some Programming languages, these classes are known as Proxy classes or Stubs.

When we click on “Generate from WSDL” button, it will prompt for WSDL File. Select WSDL file downloaded previously and click on “Parse WSDL” button. On next page you will get this error : “Failed to parse wsdl: Found more than one wsdl:portType. WSDL with multiple portType not supported”.

In some cases you may also get error like : “Failed to parse wsdl: Found more than one wsdl:binding. WSDL with multiple binding not supported”.

Continue reading “Consuming External Web Service in Apex – Salesforce”

AutoComplete Component in Visualforce using JQueryUI

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“. This Static resource has all images, CSS and JQuery library needed to implement this component.

In one of my old post, I have already explained that how to generate JSON in Visualforce page. So Considering same article I have create Visualforce page named “Account_JSON” which returns list of Accounts on basis of text entered in input field. Continue reading “AutoComplete Component in Visualforce using JQueryUI”

Using FieldSet with Visualforce and Apex

Author posted by Jitendra on Posted on under category Categories Apex, Visualforce and tagged as Tags , , with 4 Comments on Using FieldSet with Visualforce and Apex

One of the disadvantages comes up with Custom Page or Overriding New or Edit button with Visualforce page is its “Maintenance”, if New Filed is Added or needed to remove field we have to modify our code every time.

However, Thanks to Salesforce that we have “Field Set“.

With the Help of “Dynamic Visualforce Binding” and “Field Set” we can create effective Visualforce pages with ability to change Fields and its Sequence any time without modifying any code.

Let’s start with using Field set in Salesforce.

Step 1: Creating Field Set

First we need to create a field set. For this article, I am using custom object named “Question__c”.

We have to navigate to setting page of that object and then “Field Sets”. Create a new Field Set and add required field in Sequence as displayed in below image.

Create Field Set in Salesforce
Create Field Set in Salesforce

Assume that field set name is “Add_Question”. Continue reading “Using FieldSet with Visualforce and Apex”

How to Debug Manage Package in Salesforce

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce and tagged as Tags , , with 2 Comments on How to Debug Manage Package in Salesforce

Many times as a developer we need to debug the manage package for possible run time error. For example , while uploading contacts using Import wizard we get “Internal Server Error” and when we try to look into debug log there is no clear indications. However in my experience i have seen this kind of error may be because of manage package installed from AppExchange. As per “Intellectual property” rights , Salesforce cannot show the debug logs to everyone, However there is one way to achieve by System admins to some extent.

For purpose of this article, i am using one manage package from AppExchange to show how it works. Continue reading “How to Debug Manage Package in Salesforce”

Salesforce Interview Question – Part 13

121 : Consider we have overall 90% code coverage however there is one class which have 0% code coverage. Can we still able to deploy that class on production?
Ans : Yes. Minimum 1% required for every trigger and there is no such restriction for Apex class.

122 : How to get selected records ID from List View using Javascript / Ajax Toolkit, when custom button is added on List View page?
Ans : Create a new Button on Lead of type List Button. Add the button on Lead List View Layout and write below Javascript code:


var recordsSelected = {!GetRecordIds($ObjectType.Lead)}
for(var i=0; i < recordsSelected .length ; i++) {
     alert('Selected ID '+recordsSelected[i]);

123 : In Ajax toolkit for custom Javascript button, you have to explicitly login to API because global Session variable is not available. In that case it is security vulnerable because anybody logged in can see the javascript code and your username and password. So is there any way to avoid this?
Ans: We can create a visualforce page with output type as JavaScript. Global session variable is available in VF page. Initialize the global javascript variable in that VF page. include VF page as a javascript file and we are done!

124 : In Custom Component How we can return value to Custom Controller or Controller Extension?
Ans: In Apex, Objects are passed by reference (read this article to understand Pass by Value and Pass by reference in Salesforce and also read this Salesforce blog article). So supply an argument of wrapper class (object) type to custom component. If its value is changed in Custom component we will get updated value in controller also. Continue reading “Salesforce Interview Question – Part 13”

Salesforce Interview Question – Part 12

Author posted by Jitendra on Posted on under category Categories Salesforce and tagged as Tags , , with 1 Comment on Salesforce Interview Question – Part 12

111 : How to get the Recordtype Id using Dynamic Apex?
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.

Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get('API_Name_Of_SObject') ;
Schema.DescribeSObjectResult cfrSchema = s.getDescribe() ;
Map<String,Schema.RecordTypeInfo> RecordTypeInfo = cfrSchema.getRecordTypeInfosByName();
Id rtId = RecordTypeInfo.get('Record Type Name').getRecordTypeId();


Schema.SObjectType.Object_API_Name__c.getRecordTypeInfosByName().get('recordtype name').getRecordTypeId()

112 : Write Apex code which will take the RecordID as input and on the basis of that it will print the Object name and field names of sObject.

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
Map<String,String> objectMap = new Map<String,String>();
for(Schema.SObjectType f : gd)
     objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());

String sampleId ='00390000003LIVw';
String prefix =  sampleId.substring(0,3);
String objectName = objectMap.get(prefix);
System.debug('** SObject Name ** '+objectName);

Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();
List<String> fieldList = new List<String>();
for(integer i =0;i<fieldList.size();i++)
    System.debug('** Field Name ** '+fieldList[i]);

113. Consider a scenario where you have created a Visualforce page and Controller. You want to restrict the controller action for users which are logged in using “Grant Login Access”. How to acheive this?


When System admin logged in on the behalf of any other user. On upper right corner message is displayed that user is logged-in on behalf of some other user. In Visualforce page we can search for the element with class name present or not? If the element with that Class name exist means logged-in user is not a actual user.

114. How to get “https” link instead of “http” for Visualforce page using URLFOR() in Email Template ?
Ans: When you create the Link using URLFOR() in Email Template, it creates link in “http” format instead of “https” and thus causes end user to logged into salesforce again.

So instead of

<a href='{!URLFOR('/apex/SomePage', null, [id=Some_Object__c.Id,retURL="/apex/SomeOtherPage"])}'>Go to SomePage here!</a>

We can use something like :

<a href='{!SUBSTITUTE(URLFOR('/apex/SomePage', null, [id=Some_Object__c.Id,retURL="/apex/SomeOtherPage"]),'http:','https:')}'>Go to SomePage here!</a>

Continue reading “Salesforce Interview Question – Part 12”

How to Sort Wrapper class Collection in Apex

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce and tagged as Tags , with 14 Comments on How to Sort Wrapper class Collection in Apex

There is inbuilt functionality in Apex to sort the primitive datatypes supported by I am sure maximum of developers must have come across the situations where they need to sort custom datatype built by them i.e. custom class or I would say wrapper class in terms of

But think, How will come to know that how to sort class? In case of integer, date or double they already know. But how they are going to know that what is going to be content in your class? Before Summer’12 there was no direct way. However thanks to Salesforce to introduce the interface Comparable in Summer’12 release. And… there is smile on the face of Java developers, because they already know what it is.

So let’s start with “What is the use of interface Comparable?“ I would say, this is the way to tell that how we are going to compare the custom Objects (Wrapper class).

Comparable Interface:

To implement the Comparable Interface, you must declare global class with the implements keyword as follow: Continue reading “How to Sort Wrapper class Collection in Apex”

Salesforce Interview Questions – Part 10

Author posted by Jitendra on Posted on under category Categories Salesforce and tagged as Tags , , , , with 6 Comments on Salesforce Interview Questions – Part 10

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 in most of the cases IE9 does not work with Visualforce pleasantly. And then we need to add the Document type in header. So following workaround will work.

value="{!'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">'}"/>

Read more in detail in thread –

92. Onchange event does not work with <apex:actionsupport> in IE9. How to resolve this error?
Ans: If we hide the Header on Visualforce page then it creates lots of problem in IE9. I think there are few java-script library loaded by Header of Salesforce which makes IE9 compatible. So the best solution is to enable the Headre by using “showHeader=true” in Apex page.
Read more in detail in below thread URL :

93. If IE9 is not working with your custom visualforce page then how to tell your visualforce code to run in IE8 compatibility mode?
Add following metatag to pages:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />

94. It may happen that above tips will not work as lots of time the page header already sent. then how to achieve same result using Apex?
Add below line of code in Apex (Constructor)

Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');

Read more tips and tricks to solve IE9 issue in Salesforce here. Continue reading “Salesforce Interview Questions – Part 10”