Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();
Schema.SObjectType s = m.get('Contact');
Schema.DescribeSObjectResult r = s.getDescribe();
Map<String,Schema.SObjectField> fields = r.fields.getMap();
for(String field : fields.keyset()) {
Schema.DescribeFieldResult describeResult = fields.get(field).getDescribe();
if (describeResult.isCreateable() && !describeResult.isNillable() && !describeResult.isDefaultedOnCreate()) {
System.debug(field);
}
}
Showing posts with label Dynamic Apex. Show all posts
Showing posts with label Dynamic Apex. Show all posts
Sunday, December 3, 2017
Sunday, August 13, 2017
Using Dynamic Apex to retrieve Picklist Values
USECASE: Fetch the picklist values from Industry field of Account sObject.
OR
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Account.Industry.getDescribe();
List<Schema.PicklistEntry> picklistEntries = fieldResult.getPicklistValues();
for (Schema.PicklistEntry picklistEntry : picklistEntries) {
options.add(new SelectOption(picklistEntry.getLabel(), picklistEntry.getValue()));
}
return options;
OR
List<SelectOption> options = new List<SelectOption>();
Map<String, Schema.SObjectField> fieldMap = Account.getSObjectType().getDescribe().fields.getMap();
List<Schema.PicklistEntry> picklistEntries = fieldMap.get('Industry').getDescribe().getPickListValues();
for (Schema.PicklistEntry picklistEntry : picklistEntries) {
options.add(new SelectOption(picklistEntry.getLabel(), picklistEntry.getValue()));
}
return options;
DescribeSObjectResult: Check whether the object is Custom or Standard
List<String> allCustomObjsList = new List<string>();
List<String> allStandardObjsList = new List<string>();
for(Schema.SobjectType obj : Schema.getGlobalDescribe().values()) {
Schema.DescribeSobjectResult objResult = obj.getDescribe();
if(objResult.isCustom()) {
String strObjName =objResult.getname();
allCustomObjsList.add(strObjName);
}
else {
String strObjName =objResult.getname();
allStandardObjsList.add(strObjName);
}
}
System.debug('Custom Objects List: ' +allCustomObjsList);
System.debug('Standard Objects List: ' +allStandardObjsList);
If you need to check for a specific object, execute the below code:
Schema.DescribeSObjectResult describeAccount = Schema.sObjectType.Account;
System.debug(describeAccount.isCustom());
Similarly you can use other methods for describing sObjects, the list of which can be found below:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject_describe.htm
Fetch recordType Name and recordTypeId without SOQL
To avoid SOQL Limit we can use dynamic apex to get RecordTypeId. We can get a particular RecordTypeId of a particular sObject or get all recordTypeid of one sobejct in a map.
I created a record type "NewLabel" on Account object. Enter the below Apex code in Execute Anonymous window and check the output:
Note that values of
Check the link below to check a utility class that performs the query against the RecordType object
https://interactiveties.com/blog/2016/recordtype-utility-class.php
global class RecordTypeUtils {
//Method to get the recordTypeId
public static Id recordTypeId(string obj,string recName){
Id recTypeId;
if(obj!= null && recName != null){
recTypeId= Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().get(recName).getRecordTypeId();
}
return recTypeId;
}
//Method to get the map of recordType Name as key and recordTypeId as value
public static Map<String,Id> recordTypeMap(string obj){
Map<String,Id> recTypeNameWithIdMap=new Map<String,Id>();
if(obj!= null){
for(Schema.RecordTypeInfo recInfo : Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().values()){
recTypeNameWithIdMap.put(recInfo.getName(),recInfo.getRecordTypeId());
}
}
return recTypeNameWithIdMap;
}
}
I created a record type "NewLabel" on Account object. Enter the below Apex code in Execute Anonymous window and check the output:
Map<String,Id> recTypeIdMap=RecordTypeUtils.recordTypeMap('Account');
System.debug(recTypeIdMap);
Id recId=RecordTypeUtils.recordTypeId('Account','NewLabel');
System.debug(recId);
Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('NewLabel').getRecordTypeId();
System.debug(recordTypeId);
Note that values of
recId
and recordTypeId
in your output is same.Check the link below to check a utility class that performs the query against the RecordType object
https://interactiveties.com/blog/2016/recordtype-utility-class.php
Saturday, August 12, 2017
Apex: Get sObject details using ID
Id id = 'a0M7F000000qXgg';
Schema.sObjectType objType = id.getSObjectType();
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
System.debug(objDescribe.getName());
Similarly, you can get other sObject details using the methods described in the link below:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject_describe.htm
Dynamic SOQL and SOSL
To create a dynamic SOQL query at run time, use the database query method. Check the reference link for more details.
To create a dynamic SOSL query at run time, use the search query method. For example:
To prevent SOSL injection, use the
References:
http://metillium.com/2016/03/variable-binding-in-dynamic-soql/
List<sObject> sobjList = Database.query(string);
To create a dynamic SOSL query at run time, use the search query method. For example:
String searchquery = 'FIND \'Edge*\' IN ALL FIELDS RETURNING Account(Id, Name), Contact, Lead';
List<List<SObject>>searchList = search.query(searchquery);
To prevent SOSL injection, use the
escapeSingleQuotes
method. This method adds the escape character (\) to all single quotation marks in a string that is passed in from a user. The method ensures that all single quotation marks are treated as enclosing strings, instead of database commands.fieldList = (fieldList!=null) ? String.escapeSingleQuotes(fieldList) : fieldList;
References:
http://metillium.com/2016/03/variable-binding-in-dynamic-soql/
Thursday, August 13, 2015
Dynamic Apex
Describe information provides metadata information about sObject and field properties. For example, the describe information for an sObject includes whether that type of sObject supports operations like create or undelete, the sObject's name and label, the sObject's fields and child objects, and so on. The describe information for a field includes whether the field has a default value, whether it is a calculated field, the type of the field, and so on.
You can describe sObjects either by using tokens or the describeSObjects Schema method.
Apex Describe Information
• Token
• sObject
• sObject Token
• Field
• Field Token
• Describe Result
• sObject
• sObject Describe Result
• Field
• Field Describe Result
1. Access the sObjectType member variable on an sObject type, such as Account.
2. Call the getSObjectType method on an sObject describe result, an sObject variable, a list, or a map.
Details on getSObjectType(): https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_getSObjectType
1. Access the static member variable name of an sObject static type, for example, Account.Description.
2. Call the getSObjectField method on a field describe result.
1. Call the getDescribe method on an sObject token.
2. Use the Schema sObjectType static variable with the name of the sObject.
1. Call the getDescribe method on a field token.
2. Access the fields member variable of an sObject token with a field member variable
The following algorithm shows how you can work with describe information in Apex:
References:
You can describe sObjects either by using tokens or the describeSObjects Schema method.
Apex Describe Information
• Token
• sObject
• sObject Token
• Field
• Field Token
• Describe Result
• sObject
• sObject Describe Result
• Field
• Field Describe Result
Using sObject Tokens
Schema.SObjectType is the data type for an sObject token. To access the token for an sObject, use one of the following methods:1. Access the sObjectType member variable on an sObject type, such as Account.
Schema.sObjectType t = Account.sObjectType;
2. Call the getSObjectType method on an sObject describe result, an sObject variable, a list, or a map.
Account a = new Account();
Schema.sObjectType t = a.getSObjectType();
Details on getSObjectType(): https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_getSObjectType
Using field Tokens
Schema.SObjectField is the data type for a field token. To access the token for a field, use one of the following methods:1. Access the static member variable name of an sObject static type, for example, Account.Description.
Schema.SObjectField fieldToken = Account.Description;
2. Call the getSObjectField method on a field describe result.
// Get the describe result for the Name field on the Account object
Schema.DescribeFieldResult dfr = Schema.sObjectType.Account.fields.Name;
// Verify that the field token is the token for the Name field on an Account object
System.assert(dfr.getSObjectField() == Account.Name);
// Get the describe result from the token
dfr = dfr.getSObjectField().getDescribe();
Using sObject Describe Results
Schema.DescribeSObjectResult is the data type for an sObject describe result. To access the describe result for an sObject, use one of the following methods:1. Call the getDescribe method on an sObject token.
Schema.DescribeSObjectResult dsr = Account.sObjectType.getDescribe();
2. Use the Schema sObjectType static variable with the name of the sObject.
Schema.DescribeSObjectResult dsr = Schema.SObjectType.Account;
Using Field Describe Results
Schema.DescribeFieldResult is the data type for a field describe result. To access the describe result for a field, use one of the following methods:1. Call the getDescribe method on a field token.
Schema.DescribeFieldResult dfr = Account.Description.getDescribe();
2. Access the fields member variable of an sObject token with a field member variable
Schema.DescribeFieldResult dfr = Schema.SObjectType.Account.fields.Name;
Accessing All Field Describe Results for an sObject
Map representing the relationship between all the field names (keys) and the field tokens (values) for an sObject.Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Account.fields.getMap();
Accessing All sObjects
Map representing the relationship between all sObject names (keys) to sObject tokens (values).Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
The following algorithm shows how you can work with describe information in Apex:
- Generate a list or map of tokens for the sObjects in your organization.
- Determine the sObject you need to access.
- Generate the describe result for the sObject.
- If necessary, generate a map of field tokens for the sObject.
- Generate the describe result for the field the code needs to access.
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType t = gd.get('Broker__c');
Schema.DescribeSObjectResult dsr = t.getDescribe();
Map<String, Schema.SObjectField> fieldMap = dsr.fields.getMap();
Schema.DescribeFieldResult dfr = fieldMap.get('Name').getDescribe();
References:
Subscribe to:
Posts (Atom)