You need to sign in to do that
Don't have an account?

Help in test Class?
Can you please help me with this
public with sharing class PicklistDescriber {
static final Pattern OPTION_PATTERN = Pattern.compile('<option.+?>(.+?)</option>');
/**
Desribe a picklist field for an sobject id. RecordType is automatically picked
based on the record's RecordTypeId field value.
example usage :
List<String> options = PicklistDescriber.describe(accountId, 'Industry');
*/
public static List<String> describe(Id sobjectId, String pickListFieldAPIName) {
return parseOptions(
new Map<String, String> {
'id' => sobjectId,
'pickListFieldName'=> pickListFieldAPIName
}
);
}
/**
Describe a picklist field for a SobjectType, its given record type developer name and the picklist field
example usage :
List<String> options = PicklistDescriber.describe('Account', 'Record_Type_1', 'Industry'));
*/
public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName) {
return parseOptions(
new Map<String, String> {
'sobjectType' => sobjectType,
'recordTypeName' => recordTypeName,
'pickListFieldName'=> pickListFieldAPIName
}
);
}
/**
Describe a picklist field for a SobjectType, its given record type ID and the picklist field
example usage :
Id recType1Id = [Select Id from RecordType Where SobjectType = 'Account'
AND DeveloperName like 'Record_Type_2'].Id;
System.assertEquals(REC_TYPE_1_OPTIONS, PicklistDescriber.describe('Account', recType2Id, 'Industry'));
*/
public static List<String> describe(String sobjectType, Id recordTypeId, String pickListFieldAPIName) {
return parseOptions(
new Map<String, String> {
'sobjectType' => sobjectType,
'recordTypeId' => recordTypeId,
'pickListFieldName'=> pickListFieldAPIName
}
);
}
public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
return parseOptions(
new Map<String, String> {
'sobjectType' => sobjectType,
'recordTypeName' => recordTypeName,
'pickListFieldName'=> pickListFieldAPIName,
'contrfieldName' => contrfieldName
}
);
}
public static Map<String,List<String>> describeWithDependency(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
Map<String, List<String>> result = new Map<String,List<String>>();
Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeName, pickListFieldAPIName, contrfieldName)) {
for(String dep : depentOptions.keySet()) {
result.put(dep, new List<String>());
for(String val : depentOptions.get(dep)) {
if(val == pickval) {
result.get(dep).add(val);
}
}
}
}
return result;
}
public static Map<String,List<String>> describeWithDependency(String sobjectType, Id recordTypeId, String pickListFieldAPIName, String contrfieldName) {
Map<String, List<String>> result = new Map<String,List<String>>();
Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeId, pickListFieldAPIName)) {
for(String dep : depentOptions.keySet()) {
result.put(dep, new List<String>());
for(String val : depentOptions.get(dep)) {
System.debug(val);
System.debug(pickval);
if(val == pickval) {
result.get(dep).add(val);
}
}
}
}
return result;
}
/*
Internal method to parse the OPTIONS
*/
static List<String> parseOptions(Map<String, String> params) {
PageReference pr = Page.PicklistDesc;
// to handle development mode, if ON
pr.getParameters().put('core.apexpages.devmode.url', '1');
for (String key : params.keySet()) {
pr.getParameters().put(key, params.get(key));
}
String xmlContent = pr.getContent().toString();
Matcher mchr = OPTION_PATTERN.matcher(xmlContent);
List<String> options = new List<String>();
while(mchr.find()) {
System.debug(mchr.group(1));
options.add(mchr.group(1));
}
// remove the --None-- element
if (!options.isEmpty()) options.remove(0);
return options;
}
}
Thanks
public with sharing class PicklistDescriber {
static final Pattern OPTION_PATTERN = Pattern.compile('<option.+?>(.+?)</option>');
/**
Desribe a picklist field for an sobject id. RecordType is automatically picked
based on the record's RecordTypeId field value.
example usage :
List<String> options = PicklistDescriber.describe(accountId, 'Industry');
*/
public static List<String> describe(Id sobjectId, String pickListFieldAPIName) {
return parseOptions(
new Map<String, String> {
'id' => sobjectId,
'pickListFieldName'=> pickListFieldAPIName
}
);
}
/**
Describe a picklist field for a SobjectType, its given record type developer name and the picklist field
example usage :
List<String> options = PicklistDescriber.describe('Account', 'Record_Type_1', 'Industry'));
*/
public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName) {
return parseOptions(
new Map<String, String> {
'sobjectType' => sobjectType,
'recordTypeName' => recordTypeName,
'pickListFieldName'=> pickListFieldAPIName
}
);
}
/**
Describe a picklist field for a SobjectType, its given record type ID and the picklist field
example usage :
Id recType1Id = [Select Id from RecordType Where SobjectType = 'Account'
AND DeveloperName like 'Record_Type_2'].Id;
System.assertEquals(REC_TYPE_1_OPTIONS, PicklistDescriber.describe('Account', recType2Id, 'Industry'));
*/
public static List<String> describe(String sobjectType, Id recordTypeId, String pickListFieldAPIName) {
return parseOptions(
new Map<String, String> {
'sobjectType' => sobjectType,
'recordTypeId' => recordTypeId,
'pickListFieldName'=> pickListFieldAPIName
}
);
}
public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
return parseOptions(
new Map<String, String> {
'sobjectType' => sobjectType,
'recordTypeName' => recordTypeName,
'pickListFieldName'=> pickListFieldAPIName,
'contrfieldName' => contrfieldName
}
);
}
public static Map<String,List<String>> describeWithDependency(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
Map<String, List<String>> result = new Map<String,List<String>>();
Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeName, pickListFieldAPIName, contrfieldName)) {
for(String dep : depentOptions.keySet()) {
result.put(dep, new List<String>());
for(String val : depentOptions.get(dep)) {
if(val == pickval) {
result.get(dep).add(val);
}
}
}
}
return result;
}
public static Map<String,List<String>> describeWithDependency(String sobjectType, Id recordTypeId, String pickListFieldAPIName, String contrfieldName) {
Map<String, List<String>> result = new Map<String,List<String>>();
Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeId, pickListFieldAPIName)) {
for(String dep : depentOptions.keySet()) {
result.put(dep, new List<String>());
for(String val : depentOptions.get(dep)) {
System.debug(val);
System.debug(pickval);
if(val == pickval) {
result.get(dep).add(val);
}
}
}
}
return result;
}
/*
Internal method to parse the OPTIONS
*/
static List<String> parseOptions(Map<String, String> params) {
PageReference pr = Page.PicklistDesc;
// to handle development mode, if ON
pr.getParameters().put('core.apexpages.devmode.url', '1');
for (String key : params.keySet()) {
pr.getParameters().put(key, params.get(key));
}
String xmlContent = pr.getContent().toString();
Matcher mchr = OPTION_PATTERN.matcher(xmlContent);
List<String> options = new List<String>();
while(mchr.find()) {
System.debug(mchr.group(1));
options.add(mchr.group(1));
}
// remove the --None-- element
if (!options.isEmpty()) options.remove(0);
return options;
}
}
Thanks
Try this
Try this