Thursday, 1 November 2018

How to know object name from a record Id in dynamically ?

Hi All,

There are may ways to identify object name from the record id, Below is the simplest one.

String recId = '001hjhtthijhkfj';
String sObjName = recID.getSobjectType().getDescribe().getName();
System.debug('Printing-sObjName -'+sObjName ); --- Account

Here is my earlier post which talks about to get keyprefix of an object

Use Cases 

Lets assume we need to modify few fields of two different object records (Say Account and Contact), user has to modify from the detail page,

What will be our first step ?  We will create two custom detail page button (one for Account and another for Contact) .
Buttons will be linked to visual force page having standard controller Account/Contact.

The page will have a button "Save" , Save button function will be written in an extension class. At that time we should not create two extension class one for Account another for contact. We should be creating one extension class having one method which updates Account and Contact dynamically based on record id.

Public Account acc{get;set;}
Public Contact con {get;set;}
public updateRecords(Apexpages.StandardController con){
    System.debug('Printing con...'+con.getId());
        String sRecordId = con.getId();
        if(sRecordId.subString(0,3) == Account.sObjectType.getDescribe().getKeyPrefix()){
        acc= new Account(Id = sRecordId);
        else if(sRecordId.subString(0,3) == Contact.sObjectType.getDescribe().getKeyPrefix()){
        con = new Queue_View__c(Id = sRecordId);

If(acc !=null){
update acc;
if(con !=null){
update con;

Friday, 17 August 2018

How to get Record Type developer name in Apex ?

Hi All,

Before summer 18, we used to write SOQL query to get developer name of a particular record type, now SOQL query is not required. 

This is my earlier post where recordtypeinfo method has been explained. What can we do using those methods.

1. How can we get Record Type Id of an object ?

    a.  Lets assume we know the record type name. say its "Inquiry".

Id objectRecordTypeId = Schema.SObjectType.CASE.getRecordTypeInfosByName().get('Inquiry').getRecordTypeId();
System.debug('objectRecordTypeId ---'+objectRecordTypeId ); 

    b.  Lets assume we know the record type developer name, say its "DevInquiry".

Id objectRecordTypeId = Schema.SObjectType.CASE.getRecordTypeInfosByDeveloperName().get('DevInquiry').getRecordTypeId();
System.debug('objectRecordTypeId ---'+objectRecordTypeId );

2.  How can we get record type developer name ? 

     a. Lets assume we know the record type name. say its "Inquiry".

String developerName = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inquiry').getDeveloperName();

Same code can be broken into following.

String recordTypename = 'Inquiry';

Schema.DescribeSObjectResult caseDesribe = Schema.SObjectType.Case;
Map<String,Schema.RecordTypeInfo> rtMapByName = caseDesribe.getRecordTypeInfosByName();
Schema.RecordTypeInfo rtByName =  rtMapByName.get(recordTypename);
String RecordTypeDeveloperName = rtByName.getDeveloperName();
System.debug('RecordTypeDeveloperName --'+RecordTypeDeveloperName ); 

  b.  Lets Assume we know recordType id.

String developerName = Schema.SObjectType.Case.getRecordTypeInfosById().get('012w0000000kc2XAAQ').getDeveloperName();

Please let me know if you have any confusion, I would be happy to help.

Maintain Your Platform Developer I Certification for Summer ’18

How to use ISBLANK function in visuaforce page ?

Hi All,

Just a brief what isblank method does.


Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.
How to render a particular component based on the string value defined in the controller. if that string is null or blank dont rerder component.
In the class say the variable name is
Public String sStringBlankField{get;set;}  
In the visualforce page use it like below
 <apex:outputpanel  rendered = "{!len(sStringBlankField)>0 || sStringBlankField!=null}">

Thursday, 9 August 2018

How to use custom Lebel in javascript file ?

We all know how custom labels are useful, we can use in apex/visual force/workflow formula/validation rule etc. This post is all about hot to use in java script file which is been uploaded in static resource.

In the page just add these two lines of code, which will make js file understand the custom label.

      window.$Label = window.$Label || {};


In the java script file use like below

Tuesday, 7 August 2018

How to use includes operator in query filter ?

Hi All,

It has been a long time, there is no post in salesforce4u, I am apologizing for being so late, now I am assuring you, there will be at least one post in each month.

We will start with the simple one, Last week I faced one query exception "expecting left parentheses, found ':'",  Let me share how did I resolve that issue.

The requirement was just filtering records using multi-select picklist values. What exactly will come to a developer mind ??  Use the following operator to accomplish.

1. LIKE - It will be used for normal string/text field filter and it's a partial match.
2. CONTAINS - It is not being used at all in SOQL
3. INCLUDES - This is the best one to use for this requirement.

How to use INCLUDES operator in the SOQL query.

Below is the code snippet to filter Account based on value  ( 'Agriculture','HR' ).

Static Querry
List<Account> accList = [Select id,name,LOB__c from Account where LOB__c INCLUDES ('Agriculture','HR') ];
System.debug('accList ---'+accList[0]);

Dynamic Query

set<String> lobSet = new Set<String>{'Agriculture','HR'};
System.debug('lobSet ---'+lobSet );

String queryFilter = '';
For(String str :lobSet ){
   queryFilter += queryFilter == '' ? '(\''+str+'\'' : ','+ '\''+str+'\'';
queryFilter += ')';
System.debug('queryFilter ---'+queryFilter );

String sQuery = 'Select id,name,LOB__c from Account where LOB__c INCLUDES  '+queryFilter;
System.debug('sQuery ---'+sQuery );
List<Account> accListDynamicQueryResults = Database.query(sQuery);
System.debug('accListDynamicQueryResults ---'+accListDynamicQueryResults );

For more info related to Includes operator

Please let me know if it is useful or not by adding comments below.