본문 바로가기

Develop/Salesforce

Get List of Profiles that are visible to a particular tab

반응형

How can I get a list of profiles visible on a particular tab?

//get all Tab names to do a query of prmissionSetTabSetting
List<TabDefinition> tabList = [SELECT Label, Name, Url From TabDefinition ORDER BY Label];

Map<String, TabDefinition> tabMap = new Map<String, TabDefinition>();
for(TabDefinition tab : tabList){
	tabMap.put(tab.Name, tab);
}

//Querying using a nameset
List<PermissionSetTabSetting> targetList = [SELECT Parent.Profile.Name, Parent.Iscustom, Name, Visibility FROM PermissionSetTabSetting where Name IN :tabMap.keySet()];
Map<String, List<String>> tempMap = new Map<String, List<String>>();

for(PermissionSetTabSetting target : targetList){

	//I grouped a list of profile names by tab label
	TabDefinition tab = tabMap.get(target.Name);
	String key = tab?.Label;

	//I don't know why, but there was a label with a null value
	if(!String.isNotEmpty(key)){
		continue;
	}

	List<String> tabLabelList = new List<String>();
	if(tempMap.containsKey(key)){
		tabLabelList = tempMap.get(key);
	}
	// Only custom profiles were extracted
	if(target.Parent.Iscustom){
		tabLabelList.add(target.Parent.Profile.Name);
	}
	tempMap.put(key, tabLabelList);

}

for(String key : tempMap.keySet()){
	System.debug('tab Label: ' + key);
	String s = '\n';
	for(String profileName : tempMap.get(key)){
		s += profileName + '\n';
	}
	System.debug(s);
}

 

반응형