Tuesday, August 25, 2015

RecordType Utility Class


  How to get RecordTypeId dynamically by sending Object and RecordType Name ?
 
 
    Id rt = RecordTypeUtil.recordTypeId('Account','AccountDraft');
    // here you will get  accountDraft Record type id
   
     controller:
   
     public class RecordTypeUtil {
    Public static id recordTypeId(string obj,string recName){
        id recTypeId;
        if(obj!= null && recName != null){
            recTypeId= Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().get(recName).getRecordTypeId();
        } 
        return recTypeId; 
     }
    }

      Test class:
     
      @isTest
    private class RecordTypeUtil_Test {
        static testMethod void RTTestMethod() {
        string obj = 'Opportunity';
        string rtType = 'JanaKisan DE';
        ID rtId = RecordTypeUtil.recordTypeId(obj,rtType);
    }      
    }

Dynamically Get the SOSL Query.

if  you want know the how the sosl work dynamically :


   1.create visualforce page 
   
   with flowing code:


 <apex:page controller="soslexampleclass">
  <apex:form >
  <apex:pageBlock >
    <apex:inputText value="{!str}"/>
    <apex:commandButton value="Show Records" action="{!showRecordsMethod}"/>
    </apex:pageBlock>
  </apex:form>
  <apex:pageBlock title="accounts">
    <apex:pageBlockTable value="{!acclist}" var="acc">
       <apex:column value="{!acc.name}"/>
    </apex:pageBlockTable>
  </apex:pageBlock>
  <apex:pageBlock title="conatcs">
    <apex:pageBlockTable value="{!conlist}" var="acc">
       <apex:column value="{!acc.name}"/>
       <apex:column value="{!acc.email}"/>
       </apex:pageBlockTable>
  </apex:pageBlock>
  <apex:pageBlock title="opportunities">
    <apex:pageBlockTable value="{!opplist}" var="acc">
       <apex:column value="{!acc.name}"/>
       <apex:column value="{!acc.stagename}"/>
       </apex:pageBlockTable>
       <apex:pageBlock title="leads">
    <apex:pageBlockTable value="{!leadlist}" var="acc">
       <apex:column value="{!acc.name}"/>
       <apex:column value="{!acc.company}"/>
       </apex:pageBlockTable>
  </apex:pageBlock>
 
  </apex:pageBlock>
 
  </apex:page>



=============================
controller::::::::::::::


public class soslexampleclass {

    public String str { get; set; }
    public List<lead> leadlist{set;get;}
    public List<Account> acclist{set;get;}
    public List<Contact> conlist{set;get;}
    public List<opportunity> opplist{set;get;}
    public void showRecordsMethod() {
        system.debug('444444444444444444444'+str);
        leadlist= new List<lead>();
        acclist=new list<account>();
        conlist=new list<contact>();
        opplist=new list<opportunity>();
        List<List<SObject>> searchlist=[find :str IN ALL FIELDS RETURNING ACCOUNT(ID,NAME),CONTACT(NAME,EMAIL),OPPORTUNITY(NAME,STAGENAME),LEAD(COMPANY,NAME,STATUS)];
        acclist=((List<account>)searchlist[0]);
        conlist=((List<contact>)searchlist[1]);
        opplist=((List<opportunity>)searchlist[2]);
        leadlist=((List<lead>)searchlist[3]);
    }



Enjoy.....