Monday, 3 February 2014


Most of time user made query on record type like 'select id,name from Record type where isActive = true' but this query return all the record type which are active.something recordtype are active.but these are varies profilewise. lets say there is three record type which are active :
1. A
 2. B
 3. C
 Record type 1 and 2 are avaible for user with profile 'System administrator'. and Record type 2 and 3 is available for another profile let say XYZ. if any a login user made query on record type it always record 3 record type whether user belong to system administrator or other profile.To show record type profile wise i write a small vf page and controller as follow:
 Following code also avoid writting query in the trigger and test class to retrieve recordtype id.
  VF Page
<apex:page controller="RecordTypeProfileWiseController">
    <apex:form >
    <apex:pageBlock title="Select Record Type">
        <apex:selectList size="1" value="{!recordtypeID}">
             <apex:action support method is here>
            <apex:selectOptions value="{!SelectOpp}"></apex:selectOptions>
        </apex:selectList>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class
public class RecordTypeProfileWiseController {
      public String recordtypeID {get;set;}
      public RecordTypeProfileWiseController (){}
      public List < SelectOption > getSelectOpp(){
                list< SelectOption > lst = new list< SelectOption > ();
               / /it will return all record type              
             for( RecordTypeInfo s:Schema.SObjectType.account.getRecordTypeInfosByName().values()){
                    //it will check whether record type is available to lofin user or not 
                    if(s.isAvailable()){
                              lst.add(new selectOption(s.getRecordTypeId(),s.getName()));
                   
                   }
           }
            return lst;
      }
}

1 comment: