Newer Version Available
SObjectType Class
Namespace
Usage
1Schema.DescribeFieldResult F = Account.Industry.getDescribe();
2List<Schema.sObjectType> P = F.getReferenceTo();SObjectType Methods
The following are methods for SObjectType. All are instance methods.
getDescribe()
Signature
public Schema.DescribeSObjectResult getDescribe()
Return Value
getDescribe(options)
Signature
public Schema.DescribeSObjectResult getDescribe(Object options)
Parameters
- options
- Type: Object
- The parameter values determine how the elements of the describe operation are loaded.
- Use SObjectDescribeOptions.FULL to eager-load all elements of the describe, including child relationships, up-front at the time of method invocation. This describe guarantees fully coherent results, even if the describe object is passed to another namespace, API version, or other Apex context that may have different results when generating describe attributes.
- Use SObjectDescribeOptions.DEFERRED to enable lazy initialization of describe attributes on first use. This means that all child relationships will not be loaded at the time of first invocation of the method.
- Use SObjectDescribeOptions.DEFAULT to default to either eager-load or lazy-load depending on the API version.
- The type of describe operation, as determined by the parameter value is depicted in
this table.
Table 1. Type of Load for SObjectType.getDescribe() Parameter Value API Version 43.0 and Earlier API Version 44.0 and Later Full Eager Eager Deferred Lazy Lazy Default Lazy Lazy
Return Value
newSObject()
Signature
public sObject newSObject()
Return Value
Type: sObject
Example
For an example, see Dynamic DML.
newSObject(id)
Signature
public sObject newSObject(ID id)
Parameters
- id
- Type: ID
Return Value
Type: sObject
Usage
For the argument, pass the ID of an existing record in the database.
After you create a new sObject, the sObject returned has all fields set to null. You can set any updateable field to desired values and then update the record in the database. Only the fields you set new values for are updated and all other fields which are not system fields are preserved.
newSObject(recordTypeId, loadDefaults)
Signature
public sObject newSObject(ID recordTypeId, Boolean loadDefaults)
Parameters
- recordTypeId
- Type: ID
- Specifies the record type ID of the sObject to create. If no record type exists for this sObject, use null. If the sObject has record types and you specify null, the default record type is used.
- loadDefaults
- Type: Boolean
- Specifies whether to populate custom fields with their predefined default values (true) or not (false).
Return Value
Type: sObject
Usage
- For required fields that have no default values, make sure to provide a value before inserting the new sObject. Otherwise, the insertion results in an error. An example is the Account Name field or a master-detail relationship field.
- Since picklists and multi-select picklists can have default values specified per record type, this method populates the default value corresponding to the record type specified.
- If fields have no predefined default values and the loadDefaults argument is true, this method creates the sObject with field values of null.
- If the loadDefaults argument is false, this method creates the sObject with field values of null.
- This method populates read-only custom fields of the new sObject with default values. You can then insert the new sObject with the read-only fields, even though these fields cannot be edited after they’re inserted.
- If a custom field is marked as unique and also provides a default value, inserting more than one new sObject will cause a run-time exception because of duplicate field values.
To learn more about default field values, see “Default Field Values” in the Salesforce online help.
Example: Creating New sObject with Default Values
This sample creates an account with any default values populated for its custom fields, if any, using the newSObject method. It also creates a second account for a specific record type. For both accounts, the sample sets the Name field, which is a required field that doesn’t have a default value, before inserting the new accounts.
1// Create an account with predefined default values
2Account acct = (Account)Account.sObjectType.newSObject(null, true);
3// Provide a value for Name
4acct.Name = 'Acme';
5// Insert new account
6insert acct;
7
8// This is for record type RT1 of Account
9ID rtId = [SELECT Id FROM RecordType WHERE sObjectType='Account' AND Name='RT1'].Id;
10Account acct2 = (Account)Account.sObjectType.newSObject(rtId, true);
11// Provide a value for Name
12acct2.Name = 'Acme2';
13// Insert new account
14insert acct2;