Skip to content

Instantly share code, notes, and snippets.

@ykurnia
Created July 6, 2022 08:10
Show Gist options
  • Save ykurnia/7c2fe4347af66e6a71c732ff3732be64 to your computer and use it in GitHub Desktop.
Save ykurnia/7c2fe4347af66e6a71c732ff3732be64 to your computer and use it in GitHub Desktop.
Utility - Kumpulan fungsi yang mungkin berguna saat membuat apex class ataupun VF Page
public class Utility {
// public get picklist options in map (string, string)
public static Map<String, String> getMapPicklist(String strObject, String strField) {
Map<String, String> options = new Map<String, String>();
Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> field_map = gd.get(strObject).getDescribe().fields.getMap();
List<Schema.PicklistEntry> picklistValues = field_map.get(strField).getDescribe().getPickListValues();
for (Schema.PicklistEntry pv : picklistValues) {
// options.add(new SelectOption(pv.getValue(),pv.getLabel()));
options.put(pv.getValue(), pv.getLabel());
}
return options;
}
// public get picklist options
public static List<SelectOption> getFieldOptions(String strObject, String strField, Boolean addBlank) {
List<SelectOption> options = new List<SelectOption>();
if (addBlank) {
options.add(new SelectOption('', ''));
}
Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> field_map = gd.get(strObject).getDescribe().fields.getMap();
List<Schema.PicklistEntry> picklistValues = field_map.get(strField).getDescribe().getPickListValues();
for (Schema.PicklistEntry pv : picklistValues) {
options.add(new SelectOption(pv.getValue(),pv.getLabel()));
}
return options;
}
// function to get prefix of an object
public static String getObjectPrefix(String strObjectName)
{
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;
system.debug('==>m is==>'+m);
Schema.SObjectType s = m.get(strObjectName) ;
system.debug('==>Sobject Type is ==>'+s);
Schema.DescribeSObjectResult r = s.getDescribe() ;
String str = r.getKeyPrefix();
return str;
}
// get record type id
// Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client Contact').getRecordTypeId();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment