Tuesday 31 March 2015

Record Types In Apex According To The Current User's Profile Availability

Record types allow you to offer different business processes, picklist values, and page layouts to different users.After creating record types for any
object you need to set the availability of that record type into the user profile.

Suppose you creates three record type of Account as 'Borrower','Investor' and 'Dealer'. And set the availability according to the profile:

Profile Name  Available Record Type
 Profile 1         Borrower,Investor,Dealer /** Profile1 can see the all record types.**/
 Profile 2         Borrower,Investor   /** Profile2 can only see the two record types.**/

Now you need to display record type name in VF page.For this we need to find out the name of recordTypes according to the current user's profile availability.
You can achieve it by  getRecordTypeInfos() method of Schema.DescribeSObjectResult given below:

List<String> recordTypeNameList = new List<String>();
/** Need to create list to collect record type name.**/
Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
/** You need to change the object name according to your requirement.**/
List<Schema.RecordTypeInfo> RT = R.getRecordTypeInfos();
for( Schema.RecordTypeInfo recordType : RT )
{
if(recordType.isAvailable())
 { 
 /** Check if the recordtype is visible for the current user or not. **/
if(recordType.Name!='Master') {
recordTypeNameSet.add(recordType.Name);
 /** Collect the record type name in list. **/
    }
  }
}

No comments:

Post a Comment