Dynamic Apex
Dynamic apex assists developers to write more flexible and dynamic
applications.
They are handy for situations when you wanted to write a dynamic
query, wanted to access properties of an object on run time, and then
take specific actions, etc.
You can fetch all sobject and their field information in apex by using
describe methods.
Apex gives 2 data types for sobject and field information.
1. Token: A lightweight, serializable reference to an sobject or a field that is
validated at compile time.
2. Describe Result: It contains all the describe information about sobject
and fields. Describe result objects are not serializable, and are validated at
runtime.
It is easy to move from a token to it describe result, and vice versa.
Both sobject and field tokens have the method getDescribe which
returns the describe result for that token.
On the describe result, the getSObjectType and getSObjectField methods
return the tokens for sObject and field, respectively.
Finding All object in Organization
Schema getGlobalDescribe method returns a map that represents the
relationship between all sObject names (keys) to sObject tokens (values).
For example:
Map<String, [Link]> schemaMapOfOrg =
[Link]();
The map has the following characteristics:
It is dynamic, that is, it is generated at runtime on the sobject currently
available for the organization, based on permissions.
The sobject names are case insensitive.
The keys use namespaces as required. The keys reflect whether the
sobject is a custom object.
Methods
[Link](sObjectTypes)
Describes metadata (field list and object properties) for the specified sObject or array of sObjects.
Signature
public static List<[Link]> describeSObjects(List<String> sObjectTypes)
2. describeSObjects(sObjectTypes)
Describes metadata (field list and object properties) for the specified sObject or array of sObjects.
Signature
[Link] static List<[Link]> describeSObjects(List<String> sObjectTypes)
describeDataCategoryGroups(sObjectNames)
Returns a list of the category groups associated with the specified objects.
Signature
public static List<[Link]>
describeDataCategoryGroups(List<String> sObjectNames)
4. describeTabs()
Returns information about the standard and custom apps available to the running user.
Signature
public static List<[Link]> describeTabs()
Example-
public class SobjectDexcribe {
public static void display()
{
Map<String, [Link]> schemaMap =
[Link]();
for(String ss1: [Link]()){
if([Link]('Company__c')){
[Link] objToken=[Link](ss1);
//find details about sobject
[Link]
objDescribe=[Link]();
[Link]('*********sobjectAPINamel:'+[Link]());
[Link]('*********sobjectlabel:'+[Link]());
[Link]('*********sobjectPrefix:'+[Link]());
[Link]('*********IsCustom
sobject:'+[Link]());
//finding all fields of sobjects
Map<String, [Link]> fieldMap =
[Link]();
for(String ss:[Link]()){
[Link]
fd=[Link](ss).getDescribe();
[Link]('*********fieldAPINamel:'+[Link]());
[Link]('*********fieldlabel:'+[Link]());
[Link]('*********fieldtype:'+[Link]());
[Link]('*********isNillable:'+[Link]());
//check if data type is picklist then find out picklist options
if([Link]([Link]()).equalsignorecase('Picklist')){
List<[Link]> Pp = [Link]();
for([Link] p:Pp){
[Link]('****picklist option
label'+[Link]());
[Link]('****picklist option
value'+[Link]());
}
}
}
}
}
}
}
Dynamic DML:
Manipulating data dynamically without hardcoding the object type and
field names.
Querying describe information and building SOQL queries at runtime,
you can also create sObjects dynamically, and insert them into the
database using DML.
To create a new sobject of a given type, use the new SObject method on
an sObject token.
For example:
Contact c=new Contact();
[Link] tA=[Link]();
Contact b=(Contact)[Link]();
[Link]('FirstName','vidit');
[Link]('LastName','Mundada');
[Link] descr=[Link];
[Link]([Link](),'565634343');
[Link](b);
upsert b;
Dynamic SOSL:
Dynamic SOSL refers to the creation of a SOSL string at run time with
Apex code.
Dynamic SOSL enables you to create more flexible applications.
For example, you can create a search based on input from an end user,
or update records with varying field names.\
Use [Link]() to create a dynamic SOSL query at run time.
Ex- public static void SOSLQuery()
String searchquery = 'FIND \'Edge*\'IN ALL FIELDS RETURNING Account(Id,
Name), Contact, Lead';
List<List<SObject>> searchList = [Link](searchquery);
[Link](searchList);
Dynamic SOSL statements evaluate to a list of lists of sObjects, where
each list contains the search results for a particular sObject type.
The result lists are always returned in the same order as they were
specified in the dynamic SOSL query.
You want to retrieve multiple objects and fields, which might not be
related to each other.