function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JamsieJamsie 

Access available Record Types from a Profile

Hi Everyone.

 

I've built a page to allow users to select a Case Record Type from a favourites list or all Case Record Types.  It works well except the All Case Types list shows all the Case Record Types in our org rather than those normally accessible to the current user.

 

My question: how can I access the list of Case Record Types available to the user from their Profile in my controller?

 

Thanks in advance,

James.



Best Answer chosen by Admin (Salesforce Developers) 
JamsieJamsie

Thank you code the cloud. :smileyvery-happy:

 

After a bit of mind twisting it was quite simple.  Here's my code...

 

 //Get list of available Case Record Types to be used in the CaseType list
    public List<SelectOption> getCaseTypes() {
        List <RecordType> rl;
        List<SelectOption> sol = new List<SelectOption>();
       
        rl = [select Id, Name from RecordType where sObjectType='Case' and isActive=true];
       
        Schema.DescribeSObjectResult d = Schema.SObjectType.Case;
        Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
       
        sol.add(new SelectOption('','-- Select a Case Type --'));
       
        if(listView=='F'){
            Set<String> favSet = new Set<String>();
            List<Team_Member__c> tm = new List<ATS_Team_Member__c>();
            tm = [select id, User_Reference__c, Favourite_Case_Types__c
                  from Team_Member__c
                  where User_Reference__c = :UserInfo.getUserId()
                  limit 1];
            if(tm.size()>0){
                System.debug('-- customSelector - Favourites: ' + tm[0].Favourite_Case_Types__c);
                List<String> favList = tm[0].Favourite_Case_Types__c.split(';');
                favSet.addAll(favList);
            }
           
            for (RecordType r : rl) {
                if(favSet.contains(r.Name){
                    sol.add(new SelectOption(r.id,r.Name));
                }
            }
        }else if(listView=='A'){
            for (RecordType r : rl) {
                if(rtMapById.get(r.id).isAvailable()){
                    sol.add(new SelectOption(r.id,r.Name));
                }
            }
        }
        return sol;
    }

All Answers

myforcedotcommyforcedotcom

James, 

Take a look at the docs:

http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_calls_describesobjects_describesobjectresult.htm#i1428028

 

if you look at the describeSobjectResult, there is an array of recordTypeInfos[]. Each of the RecordTypeInfo objects has an available attribute that should allow you to dynamically add it to your list of case types for the user.

 

 

 

JamsieJamsie

Thank you code the cloud. :smileyvery-happy:

 

After a bit of mind twisting it was quite simple.  Here's my code...

 

 //Get list of available Case Record Types to be used in the CaseType list
    public List<SelectOption> getCaseTypes() {
        List <RecordType> rl;
        List<SelectOption> sol = new List<SelectOption>();
       
        rl = [select Id, Name from RecordType where sObjectType='Case' and isActive=true];
       
        Schema.DescribeSObjectResult d = Schema.SObjectType.Case;
        Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
       
        sol.add(new SelectOption('','-- Select a Case Type --'));
       
        if(listView=='F'){
            Set<String> favSet = new Set<String>();
            List<Team_Member__c> tm = new List<ATS_Team_Member__c>();
            tm = [select id, User_Reference__c, Favourite_Case_Types__c
                  from Team_Member__c
                  where User_Reference__c = :UserInfo.getUserId()
                  limit 1];
            if(tm.size()>0){
                System.debug('-- customSelector - Favourites: ' + tm[0].Favourite_Case_Types__c);
                List<String> favList = tm[0].Favourite_Case_Types__c.split(';');
                favSet.addAll(favList);
            }
           
            for (RecordType r : rl) {
                if(favSet.contains(r.Name){
                    sol.add(new SelectOption(r.id,r.Name));
                }
            }
        }else if(listView=='A'){
            for (RecordType r : rl) {
                if(rtMapById.get(r.id).isAvailable()){
                    sol.add(new SelectOption(r.id,r.Name));
                }
            }
        }
        return sol;
    }

This was selected as the best answer