SObjectType Class
A Schema.sObjectType object is returned from the field describe result using the getReferenceTo method, or from the sObject describe result using the getSObjectType method.
The following are methods for SObjectType. All are instance methods.
-
Returns the describe sObject result for this field.
-
Returns the describe sObject result for this field; the parameter value determines whether all child relationships are loaded up-front, or not.
-
Constructs a new sObject of this type.
-
Constructs a new sObject of this type, with the specified ID.
-
newSObject(recordTypeId, loadDefaults)
Constructs a new sObject of this type, and optionally, of the specified record type ID and with default custom field values.
Returns the describe sObject result for this field.
public Schema.DescribeSObjectResult getDescribe()
Type: Schema.DescribeSObjectResult
Returns the describe sObject result for this field; the parameter value determines whether all child relationships are loaded up-front, or not.
public Schema.DescribeSObjectResult getDescribe(Object options)
-
options:
Type:
Object The parameter values determine how the elements of the describe operation are loaded.
- Use
SObjectDescribeOptions.FULLto 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.DEFERREDto 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.DEFAULTto 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.
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 - Use
Type: Schema.DescribeSObjectResult
Constructs a new sObject of this type.
public sObject newSObject()
Type: sObject
For an example, see Dynamic DML.
Constructs a new sObject of this type, with the specified ID.
public sObject newSObject(ID id)
-
id:
Type: ID
Type: sObject
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.
Constructs a new sObject of this type, and optionally, of the specified record type ID and with default custom field values.
Where possible, we changed noninclusive terms to align with our company value of Equality. We maintained certain terms to avoid any effect on customer implementations.
public sObject newSObject(ID recordTypeId, Boolean loadDefaults)
-
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 specifynull, the default record type is used. -
loadDefaults:
Type: Boolean
Specifies whether to populate custom fields with their predefined default values (
true) or not (false).
Type: sObject
- 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 ofnull. - If the loadDefaults argument is
false, this method creates the sObject with field values ofnull. - 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.
You can also use this method to create a platform event with a prepopulated EventUuid field value for Apex publish callbacks. For more information, see Get the Result of Asynchronous Platform Event Publishing with Apex Publish Callbacks in the Platform Events Developer Guide.
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.