Newer Version Available
SObject Class
Namespace
Usage
SObject methods are all instance methods: they are called by and operate on an sObject instance such as an account or contact. The following are the instance methods for sObjects.
For more information on sObjects, see Working with sObjects.
SObject Methods
The following are methods for SObject. All are instance methods.
addError(errorMsg)
Signature
public Void addError(String errorMsg)
Parameters
- errorMsg
- Type: String
-
The error message to mark the record with.
Return Value
Type: Void
Usage
When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers, the error message is displayed in the application interface.
See Triggers and Trigger Exceptions.
When used in Visualforce controllers, the generated message is added to the collection of errors for the page. For more information, see Validation Rules and Standard Controllers in the Visualforce Developer's Guide.
Example
addError(errorMsg, escape)
Signature
public Void addError(String errorMsg, Boolean escape)
Parameters
- errorMsg
- Type: String
-
The error message to mark the record with.
- escape
- Type: Boolean
-
Indicates whether any HTML markup in the custom error message should be escaped (true) or not (false). This parameter is ignored in both Lightning Experience and the Salesforce mobile app, and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Return Value
Type: Void
Usage
The escaped characters are: \n, <, >, &, ", \, \u2028, \u2029, and \u00a9. As a result, HTML markup is not rendered; instead, it is displayed as text in the Salesforce user interface.
Example
addError(exceptionError)
Signature
public Void addError(Exception exceptionError)
Parameters
- exceptionError
- Type: System.Exception
-
An Exception object or a custom exception object that contains the error message to mark the record with.
Return Value
Type: Void
Usage
When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers, the error message is displayed in the application interface.
See Triggers and Trigger Exceptions.
When used in Visualforce controllers, the generated message is added to the collection of errors for the page. For more information, see Validation Rules and Standard Controllers in the Visualforce Developer's Guide.
Example
addError(exceptionError, escape)
Signature
public Void addError(Exception exceptionError, Boolean escape)
Parameters
- exceptionError
- Type: System.Exception
-
An Exception object or a custom exception object that contains the error message to mark the record with.
- escape
- Type: Boolean
-
Indicates whether any HTML markup in the custom error message should be escaped (true) or not (false). This parameter is ignored in both Lightning Experience and the Salesforce mobile app, and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Return Value
Type: Void
Usage
Example
addError(errorMsg)
Signature
public Void addError(String errorMsg)
Parameters
- errorMsg
- Type: String
Return Value
Type: Void
Usage
- When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers, the error appears in the application interface.
- When used in Visualforce controllers, if there is an inputField component bound to field, the message is attached to the component. For more information, see Validation Rules and Standard Controllers in the Visualforce Developer's Guide.
- This method is highly specialized because the field identifier is not actually the invoking object—the sObject record is the invoker. The field is simply used to identify the field that should be used to display the error.
See Triggers and Trigger Exceptions.
Example
addError(errorMsg, escape)
Signature
public Void addError(String errorMsg, Boolean escape)
Parameters
- errorMsg
- Type: String
-
The error message to mark the record with.
- escape
- Type: Boolean
-
Indicates whether any HTML markup in the custom error message should be escaped (true) or not (false). This parameter is ignored in both Lightning Experience and the Salesforce mobile app, and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Return Value
Type:
Usage
Example
addError(fieldName, errorMsg)
Signature
public void addError(String fieldName, String errorMsg)
Parameters
Return Value
Type: void
Usage
If the field name is an empty string or null, the error is associated with the SObject and not with a specific field.
Example
addError(fieldToken, errorMsg)
Signature
public void addError(Schema.SObjectField fieldToken, String errorMsg
Parameters
- fieldToken
- Type: Schema.SObjectField
- The field of the SObject instance.
- errorMsg
- Type: String
- The error message to be added. HTML special characters in the error message string are always escaped.
Return Value
Type: void
Usage
Use this method to add errors to the specified field token of a standard or custom object. If fieldTokenis null, the error is associated with the SObject and not with a specific field.
Example
addError(fieldName, errorMsg, escape)
Signature
public void addError(String fieldName, String errorMsg, Boolean escape)
Parameters
- fieldName
- Type: String
- The field name of the SObject .
- errorMsg
- Type: String
- The error message to be added.
- escape
- Type: Boolean
-
Indicates whether any HTML markup in the custom error message should be escaped (true) or not (false). This parameter is ignored in both Lightning Experience and the Salesforce mobile app, and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Return Value
Type: void
Usage
If the field name is an empty string or null, the error is associated with the SObject and not with a specific field.
The escaped characters are: \n, <, >, &, ", \, \u2028, \u2029, and \u00a9. As a result, HTML markup is not rendered; instead, it is displayed as text in the Salesforce user interface.
Example
addError(fieldToken, errorMsg, escape)
Signature
public void addError(Schema.SObjectField fieldToken, String errorMsg, Boolean escape)
Parameters
- fieldToken
- Type: Schema.SObjectField
- The field of the SObject instance.
- errorMsg
- Type: String
- The error message to be added.
- escape
- Type: Boolean
-
Indicates whether any HTML markup in the custom error message should be escaped (true) or not (false). This parameter is ignored in both Lightning Experience and the Salesforce mobile app, and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Return Value
Type: void
Usage
Use this method to add errors to the specified field token of a standard or custom object. If fieldTokenis null, the error is associated with the SObject and not with a specific field.
The escaped characters are: \n, <, >, &, ", \, \u2028, \u2029, and \u00a9. As a result, HTML markup is not rendered; instead, it is displayed as text in the Salesforce user interface.
Example
clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)
Signature
public SObject clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)
Parameters
- preserveId
- Type: Boolean
- (Optional) Determines whether the ID of the original object is preserved or cleared in the duplicate. If set to true, the ID is copied to the duplicate. The default is false, that is, the ID is cleared.
- isDeepClone
- Type: Boolean
- (Optional) Determines whether the method creates a full copy of the SObject field or
just a reference:
- If set to true, the method creates a full copy of the SObject. All fields on the SObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned SObject, the original SObject is not affected.
- If set to false, the method performs a shallow copy of the SObject fields. All copied relationship fields reference the original SObjects. Consequently, if you make changes to a relationship field on the cloned SObject, the corresponding field on the original SObject is also affected, and vice versa. The default is false.
- preserveReadonlyTimestamps
- Type: Boolean
- (Optional) Determines whether the read-only timestamp fields are preserved or cleared in the duplicate. If set to true, the read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the duplicate. The default is false, that is, the values are cleared.
- preserveAutonumber
- Type: Boolean
- (Optional) Determines whether auto number fields of the original object are preserved or cleared in the duplicate. If set to true, auto number fields are copied to the cloned object. The default is false, that is, auto number fields are cleared.
Return Value
Type: SObject (of same type)
Usage
Example
get(fieldName)
Signature
public Object get(String fieldName)
Parameters
- fieldName
- Type: String
Return Value
Type: Object
Usage
For more information, see Dynamic SOQL.
Example
get(field)
Signature
public Object get(Schema.sObjectField field)
Parameters
- field
- Type: Schema.SObjectField
Return Value
Type: Object
Example
getCloneSourceId()
Signature
public Id getCloneSourceId()
Return Value
Type: Id
Usage
If A is cloned to B, B is cloned to C, and C is cloned to D, then B, C, and D all point back to A as their clone source.
Example
getErrors()
Signature
public List<Database.Error> getErrors()
Return Value
Type: List<Database.Error>
getOptions()
Signature
public Database.DMLOptions getOptions()
Return Value
Type: Database.DMLOptions
Example
getPopulatedFieldsAsMap()
Signature
public Map<String,Object> getPopulatedFieldsAsMap()
Return Value
Type: Map<String,Object>
A map of field names and their corresponding values.
Usage
- The field has been queried by a SOQL statement.
- The field has been explicitly set before the call to the getPopulatedFieldsAsMap() method.
The following example iterates over the map returned by the getPopulatedFieldsAsMap() method after a SOQL query.
This example iterates over the map returned by the getPopulatedFieldsAsMap() method after fields on the SObject are explicitly set.
The following example shows how to use the getPopulatedFieldsAsMap() method with related objects.
Versioned Behavior Changes
In API version 39.0 and later, getPopulatedFieldsAsMap returns all values set on the SObject, even if values were set after the record was queried. This behavior is dependent on the version of the apex class calling this method and not on the version of the class that generated the SObject. If you query an SObject at API version 20.0, and then call this method in a class with API version 40.0, you will get the full set of fields.
getSObject(field)
Signature
public SObject getSObject(Schema.SObjectField field)
Parameters
- field
- Type: Schema.SObjectField
Return Value
Type: SObject
Usage
If the method references polymorphic fields, a Name object is returned. Use the TYPEOF clause in the SOQL SELECT statement to directly get results that depend on the runtime object type referenced by the polymorphic field. See Working with Polymorphic Relationships in SOQL Queries.
Example
getSObjects(fieldName)
Signature
public SObject[] getSObjects(String fieldName)
Parameters
- fieldName
- Type: String
Return Value
Type: SObject[]
Usage
For more information, see Dynamic DML.
Example
getSObjects(fieldName)
Signature
public SObject[] getSObjects(Schema.SObjectType fieldName)
Parameters
- fieldName
- Type: Schema.SObjectType
Return Value
Type: SObject[]
getSObjectType()
Signature
public Schema.SObjectType getSObjectType()
Return Value
Type: Schema.SObjectType
Usage
For more information, see apex_dynamic_describe_objects_understanding.
Example
hasErrors()
Signature
public Boolean hasErrors()
Return Value
Type: Boolean
isSet(fieldName)
Signature
public Void isSet(String fieldName)
Parameters
- fieldName
- Type: String
Return Value
Type: Boolean
Usage
The isSet method doesn’t check if a field is accessible to a specific user via org permissions or other specialized access permissions.
Example
isSet(field)
Signature
public Void isSet(Schema.SObjectField field)
Parameters
- field
- Type:SObjectField Class
Return Value
Type: Boolean
Usage
The isSet method doesn’t check if a field is accessible to a specific user via org permissions or other specialized access permissions.
Example
put(fieldName, value)
Signature
public Object put(String fieldName, Object value)
Parameters
- fieldName
- Type: String
- value
- Type: Object
Return Value
Type: Object
Example
put(field, value)
Signature
public Object put(Schema.SObjectField field, Object value)
Parameters
- field
- Type: Schema.SObjectField
- value
- Type: Object
Return Value
Type: Object
Example
putSObject(fieldName, value)
Signature
public SObject putSObject(String fieldName, SObject value)
Return Value
Type: SObject
Example
putSObject(fieldName, value)
Signature
public SObject putSObject(Schema.SObjectType fieldName, SObject value)
Parameters
- fieldName
- Type: Schema.SObjectType
- value
- Type: SObject
Return Value
Type: SObject
recalculateFormulas()
Signature
public Void recalculateFormulas()
Return Value
Type: Void
Usage
This method doesn’t recalculate cross-object formulas. If you call this method on objects that have both cross-object and non-cross-object formula fields, only the non-cross-object formula fields are recalculated.
Each recalculateFormulas call counts against the SOQL query limits. See Execution Governors and Limits.
setOptions(DMLOptions)
Signature
public Void setOptions(database.DMLOptions DMLOptions)
Parameters
- DMLOptions
- Type: Database.DMLOptions
Return Value
Type: Void