Dynamic Apex enables developers to:
1. Access sObject and field describe information* Describe information provides information about sObject and field properties.
* Describe information for an sObject includes:
* Whether 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.
2. Write dynamic SOQL queries, dynamic SOSL queries and dynamic DML.
* Dynamic SOQL and SOSL queries provide the ability to execute SOQL or SOSL as a string at runtime.
* Dynamic DML provides the ability to create a record dynamically and then insert it into the database using DML.
* This can be useful for applications that are installed from Force.com AppExchange.
-----------------------------------------------
Understanding Apex Describe Information
Apex provides two data structures for sObject and field describe information:
• Token—a lightweight, serializable reference to an sObject or a field that is validated at compile time.
• Describe result—an object that contains all the describe properties for the sObject or field. Describe result objects are not serializable, and are validated at runtime.
*It is easy to move from a token to its 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.
*Because tokens are lightweight, using them can make your code faster and more efficient.
*use the token version of an sObject or field when you are determining the type of an sObject or field that your code needs to use.
// Create a new account as the generic type sObject sObject s = new Account(); //s is a record for Account // Verify that the generic sObject is an Account sObject //Checking tokens System.assert(s.getsObjectType() == Account.sObjectType);//verify record belongs to Account or not // Get the sObject describe result for the Account object Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe(); // Get the field describe result for the Name field on the Account object Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields.Name; // Verify that the field token is the token for the Name field on an Account object //Checking tokens System.assert(f.getSObjectField() == Account.Name);
The following algorithm shows how you can work with describe information in Apex:
1. Generate a list or map of tokens for the sObjects in your organization
2. Determine the sObject you need to access
3. Generate the describe result for the sObject
4. If necessary, generate a map of field tokens for the sObject
5. Generate the describe result for the field the code needs to access
Understanding Describe Information Permissions:
* Apex generally runs in system mode. All classes and triggers that are not included in a package, that is, are native to your organization, have no restrictions on the sObjects that they can look up dynamically.
* This means that with native code, you can generate a map of all the sObjects for your organization, regardless of the current user's permission.
* Dynamic Apex, contained in managed packages created by salesforce.com ISV partners that are installed from Force.com
AppExchange, have restricted access to any sObject outside the managed package.
AppExchange, have restricted access to any sObject outside the managed package.
* Partners can set the API Access value within the package to grant access to standard sObjects not included as part of the managed package
Using sObject Tokens:
* SObjects, such as Account and MyCustomObject__c, act as static classes with special static methods and member variables for accessing token and describe result information.
* You must explicitly reference an sObject and field name at compile time to gain access to the describe result.
* 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
2. Call the getSObjectType method on an sObject describe result, an sObject variable, a list, or a map
* Schema.SObjectType is the data type for an sObject token.
* In the following example, the token for the Account sObject is returned:
Schema.sObjectType t = Account.sObjectType;
*Schema.sObjectType t = Account.sObjectType;
Account A = new Account(); Schema.sObjectType T = A.getSObjectType();
* This example can be used to determine whether an sObject or a list of sObjects is of a particular type:
public class sObjectTest { { // Create a generic sObject variable s SObject s = Database.query('select id from account limit 1'); // Verify if that sObject variable is an Account token System.assertEquals(s.getSObjectType(), Account.sObjectType); // Create a list of generic sObjects List<sObject> l = new Account[]{}; // Verify if the list of sObjects contains Account tokens System.assertEquals(l.getSObjectType(), Account.sObjectType); }}
*Some standard sObjects have a field called sObjectType, for example, AssignmentRule, QueueSObject, and RecordType.
*For these types of sObjects, always use the getSObjectType method for retrieving the token.
*If you use the property, for example, RecordType.sObjectType, the field is returned.
No comments:
Post a Comment