Created
January 10, 2020 14:43
-
-
Save Tamarer/f38ae0f23e2b4c9a980ec93bff3a5891 to your computer and use it in GitHub Desktop.
modification of GetRecordTypeInfobyObject to ignore master record type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global with sharing class GetRecordTypeInfobyObject { | |
//given a target object name, return a collection of strings representing the available record types | |
//great for extracting the Names and IDs of record types available for selection | |
// source: https://github.com/alexed1/LightningFlowComponents/blob/master/flow_screen_components/quickChoiceFSCproject/force-app/main/default/classes/GetRecordTypeInfobyObject.cls | |
// adapted by Tamar Erlich, added a toggle to ignore the master record type. | |
@InvocableMethod | |
global static List <Results> get(List<Requests> requestList) { | |
Results response = new Results(); | |
String objectName = requestList[0].objectName; | |
Boolean onlyReturnActiveRecordTypes = requestList[0].onlyReturnActiveRecordTypes; | |
Boolean onlyReturnRecordTypesAvailableToCurrentUser = requestList[0].onlyReturnRecordTypesAvailableToCurrentUser; | |
Boolean ignoreMasterRecordType = requestList[0].ignoreMasterRecordType; | |
List<String> recordTypeNames = new List<String>(); | |
List<String> recordTypeIds = new List<String>(); | |
String errors; | |
// Send the email you have created. | |
try { | |
Map<String, Schema.RecordTypeInfo> recTypeInfoMap = Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosByName(); | |
List<Schema.RecordTypeInfo> recTypeInfoList = recTypeInfoMap.values(); | |
//extract the names and recordIds and return them as List<String> | |
for(Schema.RecordTypeInfo curRecord : recTypeInfoList) { | |
System.debug('processing record: ' + curRecord); | |
Boolean filteredOut = false; | |
//if only active | |
if (onlyReturnActiveRecordTypes = true && ! curRecord.isActive()) { | |
filteredOut = true; | |
} | |
if (onlyReturnRecordTypesAvailableToCurrentUser = true && ! curRecord.isAvailable()) { | |
filteredOut = true; | |
} | |
if (ignoreMasterRecordType = true && curRecord.isMaster()) { | |
filteredOut = true; | |
} | |
if ( filteredOut != true) { | |
recordTypeNames.add(curRecord.getName()); | |
recordTypeIds.add(curRecord.getRecordTypeId()); | |
} | |
} | |
} catch (InvocableActionException e){ | |
System.debug ('exception occured: ' + e.getMessage()); | |
errors = e.getMessage(); | |
} | |
response.recordTypeNames = recordTypeNames; | |
response.recordTypeIds = recordTypeIds; | |
response.errors = errors; | |
List<Results> responseWrapper = new List<Results>(); | |
responseWrapper.add(response); | |
return responseWrapper; | |
} | |
global class Requests { | |
@InvocableVariable(label='Object Name' required=true) | |
global String objectName; | |
@InvocableVariable | |
global Boolean onlyReturnActiveRecordTypes; | |
@InvocableVariable | |
global Boolean onlyReturnRecordTypesAvailableToCurrentUser; | |
@InvocableVariable | |
global Boolean ignoreMasterRecordType; | |
} | |
global class Results { | |
public Results() { | |
} | |
@InvocableVariable | |
global String errors; | |
@InvocableVariable | |
global List<String> recordTypeNames; | |
@InvocableVariable | |
global List<String> recordTypeIds; | |
} | |
global class InvocableActionException extends Exception {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment