---
schema_version: "1"
id: 320ae35a934d1057f50b5915e09a4381ac15f7210ed897dbbd63a48a79685bcf
title: SObject Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_methods_system_sobject.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: ab3b0a37684a986e1a8c408c589f677be536bc1ca997a5d331643678f1650927
description: Contains methods for the sObject data type.Marks a trigger record with a custom error message and prevents any DML operation from occurring.Marks a trigger record with a custom error message, specifies if the error message should be escaped, and prevents any DML operation from occurring.Marks a trigger record with a custom error message and prevents any DML operation from occurring.Marks a trigger record with a custom exception error message, specifies whether or not the exception error message should be escaped, and prevents any DML operation from occurring.Dynamically add errors to fields of an SObject associated with the specified field name.Dynamically add errors to an SObject instance associated with the specified field.Dynamically add errors to fields of an SObject associated with the specified field name.Dynamically add errors to an SObject instance associated with the specified field.Clears all field valuesCreates a copy of the SObject record. Creates a copy of the SObject record. Creates a copy of the SObject record. Creates a copy of the SObject record. Returns the value for the field specified by fieldName, such as AccountNumber. Returns the value for the field specified by the field token Schema.sObjectField, such as Schema.Account.AccountNumber.Returns the ID of the entity from which an object was cloned. You can use it for objects cloned through the Salesforce user interface. You can also use it for objects created using the System.SObject.clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) method, provided that the preserveId parameter wasn’t used or was set to false. The getCloneSourceId() method can only be used within the transaction where the entity is cloned, as clone information doesn’t persist in subsequent transactions. Returns a list of Database.Error objects for an SObject instance. If the SObject has no errors, an empty list is returned.Returns the database.DMLOptions object for the SObject. Returns a map of populated field names and their corresponding values. The map contains only the fields that have been populated in memory for the SObject instance.Returns the value for the specified field. This method is primarily used with dynamic DML to access values for external IDs.Returns the value for the field specified by the field token Schema.sObjectField, such as Schema.MyObj.MyExternalId. This method is primarily used with dynamic DML to access values for external IDs.Returns the values for the specified field. This method is primarily used with dynamic DML to access values for associated objects, such as child relationships.Returns the value for the field specified by the field token Schema.fieldName, such as Schema.Account.Contact. This method is primarily used with dynamic DML to access values for associated objects, such as child relationships.Returns the token for this SObject. This method is primarily used with describe information.Retrieves the name of a quick action associated with this SObject. Typically used in triggers.Returns true if an SObject instance has associated errors. The error message can be associated to the SObject instance by using SObject.addError(), validation rules, or by other means.Returns true if an entity is cloned from something, even if the entity hasn’t been saved. The method can only be used within the transaction where the entity is cloned, as clone information doesn’t persist in subsequent transactions.Returns information about the queried sObject field. Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. Returns false if the sObject field isn’t set. If an invalid field is specified, an SObjectException is thrown.Returns information about the queried sObject field. Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. Returns false if the sObject field isn’t set. If an invalid field is specified, an SObjectException is thrown.Sets the value for the specified field and returns the previous value for the field.Sets the value for the field specified by the field token Schema.sObjectField, such as Schema.Account.AccountNumber and returns the previous value for the field.Sets the value for the specified field. This method is primarily used with dynamic DML for setting external IDs. The method returns the previous value of the field. Sets the value for the field specified by the token Schema.SObjectType. This method is primarily used with dynamic DML for setting external IDs. The method returns the previous value of the field. Deprecated as of API version 57.0. Use the recalculateFormulas() method in the System.Formula class instead.Sets the DMLOptions object for the SObject.
---
# SObject Class

Contains methods for the sObject data type.

## Namespace

[System](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_System.htm "The System namespace provides classes and methods for core Apex functionality.")

## 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](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/langCon_apex_SObjects.htm).

## SObject Methods

The following are methods for SObject. All are instance methods.

- **[addError(errorMsg)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_addError)**  
    Marks a trigger record with a custom error message and prevents any DML operation from occurring.
- **[addError(errorMsg, escape)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_addError_2)**  
    Marks a trigger record with a custom error message, specifies if the error message should be escaped, and prevents any DML operation from occurring.
- **[addError(exceptionError)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_addError_3)**  
    Marks a trigger record with a custom error message and prevents any DML operation from occurring.
- **[addError(exceptionError, escape)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_addError_4)**  
    Marks a trigger record with a custom exception error message, specifies whether or not the exception error message should be escaped, and prevents any DML operation from occurring.
- **[addError(fieldName, errorMsg)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#unique_777855719)**  
    Dynamically add errors to fields of an SObject associated with the specified field name.
- **[addError(fieldToken, errorMsg)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#unique_1827830969)**  
    Dynamically add errors to an SObject instance associated with the specified field.
- **[addError(fieldName, errorMsg, escape)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#unique_377021838)**  
    Dynamically add errors to fields of an SObject associated with the specified field name.
- **[addError(fieldToken, errorMsg, escape)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#unique_697980217)**  
    Dynamically add errors to an SObject instance associated with the specified field.
- **[clear()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_clear)**  
    Clears all field values
- **[clone(preserveId)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_clone)**  
    Creates a copy of the SObject record.
- **[clone(preserveId, isDeepClone)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#unique_667981484)**  
    Creates a copy of the SObject record.
- **[clone(preserveId, isDeepClone, preserveReadonlyTimestamps)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#unique_717545589)**  
    Creates a copy of the SObject record.
- **[clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#unique_704732210)**  
    Creates a copy of the SObject record.
- **[get(fieldName)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_get)**  
    Returns the value for the field specified by fieldName, such as AccountNumber.
- **[get(field)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_get_2)**  
    Returns the value for the field specified by the field token Schema.sObjectField, such as Schema.Account.AccountNumber.
- **[getCloneSourceId()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getCloneSourceId)**  
    Returns the ID of the entity from which an object was cloned. You can use it for objects cloned through the Salesforce user interface. You can also use it for objects created using the System.SObject.clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) method, provided that the preserveId parameter wasn’t used or was set to false. The getCloneSourceId() method can only be used within the transaction where the entity is cloned, as clone information doesn’t persist in subsequent transactions.
- **[getErrors()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getErrors)**  
    Returns a list of Database.Error objects for an SObject instance. If the SObject has no errors, an empty list is returned.
- **[getOptions()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getOptions)**  
    Returns the database.DMLOptions object for the SObject.
- **[getPopulatedFieldsAsMap()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getPopulatedFieldsAsMap)**  
    Returns a map of populated field names and their corresponding values. The map contains only the fields that have been populated in memory for the SObject instance.
- **[getSObject(fieldName)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getSObject)**  
    Returns the value for the specified field. This method is primarily used with dynamic DML to access values for external IDs.
- **[getSObject(field)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getSObject_2)**  
    Returns the value for the field specified by the field token Schema.sObjectField, such as Schema.MyObj.MyExternalId. This method is primarily used with dynamic DML to access values for external IDs.
- **[getSObjects(fieldName)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getSObjects)**  
    Returns the values for the specified field. This method is primarily used with dynamic DML to access values for associated objects, such as child relationships.
- **[getSObjects(fieldName)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getSObjects_2)**  
    Returns the value for the field specified by the field token Schema.fieldName, such as Schema.Account.Contact. This method is primarily used with dynamic DML to access values for associated objects, such as child relationships.
- **[getSObjectType()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getSObjectType)**  
    Returns the token for this SObject. This method is primarily used with describe information.
- **[getQuickActionName()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getQuickActionName)**  
    Retrieves the name of a quick action associated with this SObject. Typically used in triggers.
- **[hasErrors()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_hasErrors)**  
    Returns true if an SObject instance has associated errors. The error message can be associated to the SObject instance by using SObject.addError(), validation rules, or by other means.
- **[isClone()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_isClone)**  
    Returns true if an entity is cloned from something, even if the entity hasn’t been saved. The method can only be used within the transaction where the entity is cloned, as clone information doesn’t persist in subsequent transactions.
- **[isSet(fieldName)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_isSet)**  
    Returns information about the queried sObject field. Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. Returns false if the sObject field isn’t set. If an invalid field is specified, an SObjectException is thrown.
- **[isSet(field)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_isSet_2)**  
    Returns information about the queried sObject field. Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. Returns false if the sObject field isn’t set. If an invalid field is specified, an SObjectException is thrown.
- **[put(fieldName, value)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_put)**  
    Sets the value for the specified field and returns the previous value for the field.
- **[put(field, value)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_put_2)**  
    Sets the value for the field specified by the field token Schema.sObjectField, such as Schema.Account.AccountNumber and returns the previous value for the field.
- **[putSObject(fieldName, value)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_putSObject)**  
    Sets the value for the specified field. This method is primarily used with dynamic DML for setting external IDs. The method returns the previous value of the field.
- **[putSObject(fieldName, value)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_putSObject_2)**  
    Sets the value for the field specified by the token Schema.SObjectType. This method is primarily used with dynamic DML for setting external IDs. The method returns the previous value of the field.
- **[recalculateFormulas()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_recalculateFormulas)**  
    **Deprecated as of API version 57.0. Use the recalculateFormulas() method in the System.Formula class instead.**
- **[setOptions(DMLOptions)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_setOptions)**  
    Sets the DMLOptions object for the SObject.

### addError(errorMsg)

Marks a trigger record with a custom error message and prevents any DML operation from occurring.

#### Signature

public Void addError(String errorMsg)

#### Parameters

- **errorMsg**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    The error message to mark the record with.

#### Return Value

Type: Void

#### Usage

When used on Trigger.new in insert and update triggers, and on Trigger.old in delete triggers, the error message is displayed in the application interface.

See [Triggers](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_triggers.htm) and [Trigger Exceptions](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_triggers_exceptions.htm).

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

This method escapes any HTML markup in the specified error message. 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.

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](https://developer.salesforce.com/docs/atlas.en-us.264.0.pages.meta/pages/pages_controller_std.htm "HTML (New Window)") in the Visualforce Developer's Guide.

#### Example

Trigger.new\[0\].addError('bad');

### addError(errorMsg, escape)

Marks a trigger record with a custom error message, specifies if the error message should be escaped, and prevents any DML operation from occurring.

#### Signature

public Void addError(String errorMsg, Boolean escape)

#### Parameters

- **errorMsg**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    The error message to mark the record with.
- **escape**:
    
    Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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.

![Warning](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note_warning.png)

#### Warning

Be cautious if you specify false for the escape argument. Unescaped strings displayed in the Salesforce user interface can represent a vulnerability in the system because these strings might contain harmful code. If you want to include HTML markup in the error message, call this method with a false escape argument. Make sure that you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call [addError(String errorMsg)](#apex_System_SObject_addError "Marks a trigger record with a custom error message and prevents any DML operation from occurring.") instead.

#### Example

Trigger.new\[0\].addError('Fix & resubmit', false);

### addError(exceptionError)

Marks a trigger record with a custom error message and prevents any DML operation from occurring.

#### Signature

public Void addError(Exception exceptionError)

#### Parameters

- **exceptionError**:
    
    Type: [System.Exception](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_exception_methods.htm "An exception denotes an error that disrupts the normal flow of code execution. You can use Apex built-in exceptions or create custom exceptions. All exceptions have common methods.")
    
    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 insert and update triggers, and on Trigger.old in delete triggers, the error message is displayed in the application interface.

See [Triggers](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_triggers.htm) and [Trigger Exceptions](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_triggers_exceptions.htm).

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

This method escapes any HTML markup in the specified error message. 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.

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](https://developer.salesforce.com/docs/atlas.en-us.264.0.pages.meta/pages/pages_controller_std.htm "HTML (New Window)") in the Visualforce Developer's Guide.

#### Example

public with sharing class MyException extends Exception {}

This trigger marks the first record in Trigger.new with the exception.

trigger AddErrorExample on Account (before insert) {
    Trigger.new\[0\].addError(new MyException('Invalid Id'));
}

### addError(exceptionError, escape)

Marks a trigger record with a custom exception error message, specifies whether or not the exception error message should be escaped, and prevents any DML operation from occurring.

#### Signature

public Void addError(Exception exceptionError, Boolean escape)

#### Parameters

- **exceptionError**:
    
    Type: [System.Exception](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_exception_methods.htm "An exception denotes an error that disrupts the normal flow of code execution. You can use Apex built-in exceptions or create custom exceptions. All exceptions have common methods.")
    
    An Exception object or a custom exception object that contains the error message to mark the record with.
- **escape**:
    
    Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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.

![Warning](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note_warning.png)

#### Warning

Be cautious if you specify false for the escape argument. Unescaped strings displayed in the Salesforce user interface can represent a vulnerability in the system because these strings might contain harmful code. If you want to include HTML markup in the error message, call this method with a false escape argument. Make sure that you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call [addError(Exception e)](#apex_System_SObject_addError_3 "Marks a trigger record with a custom error message and prevents any DML operation from occurring.") instead.

#### Example

public with sharing class MyException extends Exception {}

This trigger marks the first record in Trigger.new with an unescaped exception message.

trigger AddErrorEscapeExample on Account (before insert) {
    Trigger.new\[0\].addError(new MyException('Invalid Id & other issues'), false);
}

### addError(fieldName, errorMsg)

Dynamically add errors to fields of an SObject associated with the specified field name.

#### Signature

public void addError(String fieldName, String errorMsg)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    The field name of the SObject .
- **errorMsg**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    The error message to be added. HTML special characters in the error message string are always escaped.

#### 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

// Add an error to an SObject field using the addError() method.
Account acct = new Account(name = 'TestAccount');
acct.addError('name', 'error in name field');
// Use the hasErrors() method to verify that the error is added, and then the getErrors() method to validate the error.
Assert.isTrue(acct.hasErrors());
List<Database.Error> errors = acct.getErrors();
Assert.areEqual(1, errors.size());

### addError(fieldToken, errorMsg)

Dynamically add errors to an SObject instance associated with the specified field.

#### Signature

public void addError(Schema.SObjectField fieldToken, String errorMsg

#### Parameters

- **fieldToken**:
    
    Type: [Schema.SObjectField](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectField.htm#apex_class_Schema_SObjectField "A Schema.sObjectField object is returned from the field describe result using the getController and getSObjectField methods.")
    
    The field of the SObject instance.
- **errorMsg**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    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

// Add an error to a field of an SObject instance using the addError() method.
Account acct = new Account(name = 'TestAccount');
Schema.DescribeFieldResult nameDesc = Account.name.getDescribe();
Schema.sObjectField nameField = nameDesc.getSObjectField();
acct.addError(nameField, 'error is name field');
// Use the hasErrors() method to verify that the error is added, and then the getErrors() method to validate the error.
Assert.isTrue(acct.hasErrors());
List<Database.Error> errors = acct.getErrors();
Assert.areEqual(1, errors.size());

### addError(fieldName, errorMsg, escape)

Dynamically add errors to fields of an SObject associated with the specified field name.

#### Signature

public void addError(String fieldName, String errorMsg, Boolean escape)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    The field name of the SObject .
- **errorMsg**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    The error message to be added.
- **escape**:
    
    Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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.

![Warning](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note_warning.png)

#### Warning

- The escape parameter cannot be disabled in Lightning Experience and in the Salesforce mobile app, and will be ignored.
- Be cautious if you specify false for the escape argument. Unescaped strings displayed in the Salesforce user interface can represent a vulnerability in the system because these strings might contain harmful code. If you want to include HTML markup in the error message, call this method with a false escape argument. Make sure that you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call [addError(String fieldName, String errorMsg)](#unique_777855719 "Dynamically add errors to fields of an SObject associated with the specified field name.") instead.

#### Example

// Add an error to an SObject field using the addError() method.
Account acct = new Account(name = 'TestAccount');
acct.addError('name', 'error in name field', false);
// Use the hasErrors() method to verify that the error is added, and then the getErrors() method to validate the error.
Assert.isTrue(acct.hasErrors());
List<Database.Error> errors = acct.getErrors();
Assert.areEqual(1, errors.size());

### addError(fieldToken, errorMsg, escape)

Dynamically add errors to an SObject instance associated with the specified field.

#### Signature

public void addError(Schema.SObjectField fieldToken, String errorMsg, Boolean escape)

#### Parameters

- **fieldToken**:
    
    Type: [Schema.SObjectField](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectField.htm#apex_class_Schema_SObjectField "A Schema.sObjectField object is returned from the field describe result using the getController and getSObjectField methods.")
    
    The field of the SObject instance.
- **errorMsg**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    The error message to be added.
- **escape**:
    
    Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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.

![Warning](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note_warning.png)

#### Warning

- The escape parameter cannot be disabled in Lightning Experience and in the Salesforce mobile app, and will be ignored.
- Be cautious if you specify false for the escape argument. Unescaped strings displayed in the Salesforce user interface can represent a vulnerability in the system because these strings might contain harmful code. If you want to include HTML markup in the error message, call this method with a false escape argument. Make sure that you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call [addError(Schema.SObjectField fieldToken, String errorMsg)](#unique_1827830969 "Dynamically add errors to an SObject instance associated with the specified field.") instead.

#### Example

// Add an error to a field of an SObject instance using the addError() method. 
Account acct = new Account(name = 'TestAccount');
Schema.DescribeFieldResult nameDesc = Account.name.getDescribe();
Schema.sObjectField nameField = nameDesc.getSObjectField();
acct.addError(nameField, 'error is name field', false);
// Use the hasErrors() method to verify that the error is added, and then the getErrors() method to validate the error.
Assert.isTrue(acct.hasErrors());
List<Database.Error> errors = acct.getErrors();
Assert.areEqual(1, errors.size());

### clear()

Clears all field values

#### Signature

public Void clear()

#### Return Value

Type: Void

#### Example

Account acc = new account(Name = 'Acme');
acc.clear();
Account expected = new Account();
Assert.areEqual(expected, acc);

### clone(preserveId)

Creates a copy of the SObject record.

#### Signature

public SObject clone(Boolean preserveId)

#### Parameters

- **preserveId**: Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.") 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.

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.") (of the same type)

#### Usage

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

For Apex saved using Salesforce API version 22.0 or earlier, the default value for the preserveId argument is true, that is, the ID is preserved.

### clone(preserveId, isDeepClone)

Creates a copy of the SObject record.

#### Signature

public SObject clone(Boolean preserveId, Boolean isDeepClone)

#### Parameters

- **preserveId**:
    
    Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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 change a field on the cloned SObject, the original SObject isn’t 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 change a relationship field on the cloned SObject, the corresponding field on the original SObject is also affected, and vice versa. The default is false.

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.") (of the same type)

#### Usage

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

For Apex saved using Salesforce API version 22.0 or earlier, the default value for the preserveId argument is true, that is, the ID is preserved.

### clone(preserveId, isDeepClone, preserveReadonlyTimestamps)

Creates a copy of the SObject record.

#### Signature

public SObject clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps)

#### Parameters

- **preserveId**:
    
    Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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 change a field on the cloned SObject, the original SObject isn’t 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 change 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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.
    
    ![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)
    
    #### Note
    
    Audit field values won’t be persisted to the database via DML on the cloned SObject instance.

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.") (of the same type)

#### Usage

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

For Apex saved using Salesforce API version 22.0 or earlier, the default value for the preserveId argument is true, that is, the ID is preserved.

### clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

Creates a copy of the SObject record.

#### Signature

public SObject clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)

#### Parameters

- **preserveId**:
    
    Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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 change a field on the cloned SObject, the original SObject isn’t 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 change 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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.
    
    ![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)
    
    #### Note
    
    Audit field values won’t be persisted to the database via DML on the cloned SObject instance.
- **preserveAutonumber**: Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")
    
    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](#apex_methods_system_sobject "Contains methods for the sObject data type.") (of the same type)

#### Usage

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

For Apex saved using Salesforce API version 22.0 or earlier, the default value for the preserveId argument is true, that is, the ID is preserved.

#### Example

Account acc = new account(Name = 'Acme', Description = 'Acme Account');
Account clonedAcc = acc.clone(false, false, false, false);
Assert.areEqual(acc, clonedAcc);

### get(fieldName)

Returns the value for the field specified by fieldName, such as AccountNumber.

#### Signature

public Object get(String fieldName)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: Object

#### Usage

For more information, see [Dynamic SOQL](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_dynamic_soql.htm).

#### Example

Account acc = new account(Name = 'Acme', Description = 'Acme Account');
String description = (String)acc.get('Description');
Assert.areEqual('Acme Account', description);

#### Versioned Behavior Changes

In API version 34.0 and later, you must include the namespace name to retrieve a field from a field Map using this method. For example, to get the account\_\_c field in the MyNamespace namespace from a fields field Map, use: fields.get(‘MyNamespace\_\_account\_\_c’).

### get(field)

Returns the value for the field specified by the field token Schema.sObjectField, such as Schema.Account.AccountNumber.

#### Signature

public Object get(Schema.sObjectField field)

#### Parameters

- **field**:
    
    Type: [Schema.SObjectField](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectField.htm#apex_class_Schema_SObjectField "A Schema.sObjectField object is returned from the field describe result using the getController and getSObjectField methods.")

#### Return Value

Type: Object

#### Usage

For more information, see [Dynamic SOQL](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_dynamic_soql.htm).

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

Field tokens aren't available for person accounts. If you access Schema.Account.fieldname, where _fieldname_ is a placeholder value, you get an exception error. Instead, specify the field name as a string.

#### Example

Account acc = new account(Name = 'Acme', Description = 'Acme Account');
String description = (String)acc.get(Schema.Account.Description);
Assert.areEqual('Acme Account', description);

### getCloneSourceId()

Returns the ID of the entity from which an object was cloned. You can use it for objects cloned through the Salesforce user interface. You can also use it for objects created using the System.SObject.clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) method, provided that the preserveId parameter wasn’t used or was set to false. The getCloneSourceId() method can only be used within the transaction where the entity is cloned, as clone information doesn’t persist in subsequent transactions.

#### Signature

public Id getCloneSourceId()

#### Return Value

Type: [Id](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_id.htm#apex_methods_system_id "Contains methods for the ID primitive data type.")

#### 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

Account acc0 = new Account(Name = 'Acme');
insert as user acc0;
Account acc1 = acc0.clone();
Account acc2 = acc1.clone();
Account acc3 = acc2.clone();
Account acc4 = acc3.clone();
Assert.isNotNull(acc0.Id);
Assert.areEqual(acc0.Id, acc1.getCloneSourceId());
Assert.areEqual(acc0.Id, acc2.getCloneSourceId());
Assert.areEqual(acc0.Id, acc3.getCloneSourceId());
Assert.areEqual(acc0.Id, acc4.getCloneSourceId());
Assert.isNull(acc0.getCloneSourceId());

### getErrors()

Returns a list of Database.Error objects for an SObject instance. If the SObject has no errors, an empty list is returned.

#### Signature

public List<Database.Error> getErrors()

#### Return Value

Type: [List](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm#apex_methods_system_list "Contains methods for the List collection type.")<[Database.Error](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_error.htm#apex_methods_system_database_error "Represents information about an error that occurred during a DML operation when using a Database method.")\>

### getOptions()

Returns the database.DMLOptions object for the SObject.

#### Signature

public Database.DMLOptions getOptions()

#### Return Value

Type: [Database.DMLOptions](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_dmloptions.htm#apex_methods_system_database_dmloptions "Enables you to set options related to DML operations.")

#### Example

Database.DMLOptions dmo = new Database.dmlOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;

Account acc = new Account(Name = 'Acme');
acc.setOptions(dmo);
Database.DMLOptions accDmo = acc.getOptions();

### getPopulatedFieldsAsMap()

Returns a map of populated field names and their corresponding values. The map contains only the fields that have been populated in memory for the SObject instance.

#### Signature

public Map<String,Object\> getPopulatedFieldsAsMap()

#### Return Value

Type: Map<String,Object>

A map of field names and their corresponding values.

#### Usage

The returned map contains only the fields that have been populated in memory for the SObject instance, which makes it easy to iterate over those fields. A field is populated in memory in these cases.

- The field has been queried by a SOQL statement, and the field is not null.
- The field has been explicitly set before the call to the getPopulatedFieldsAsMap() method.

Fields on related objects that are queried or set are also returned in the map.

The following example iterates over the map returned by the getPopulatedFieldsAsMap() method after a SOQL query.

Account a = new Account();
a.name = 'TestMapAccount1';
insert as user a;
a = \[SELECT Id,Name FROM Account WHERE id=:a.Id WITH USER\_MODE\];
Map<String, Object\> fieldsToValue = a.getPopulatedFieldsAsMap();

for (String fieldName : fieldsToValue.keySet()){
    System.debug('field name is ' + fieldName + ', value is ' + 
        fieldsToValue.get(fieldName));
}

// Example debug statement output:
// DEBUG|field name is Id, value is 001R0000003EPPkIAO
// DEBUG|field name is Name, value is TestMapAccount1

This example iterates over the map returned by the getPopulatedFieldsAsMap() method after fields on the SObject are explicitly set.

Account a = new Account();
a.name = 'TestMapAccount2';
a.phone = '123-4567';
insert as user a;
Map<String, Object\> fieldsToValue = a.getPopulatedFieldsAsMap();

for (String fieldName : fieldsToValue.keySet()) {
    System.debug('field name is ' + fieldName + ', value is ' + 
        fieldsToValue.get(fieldName));
}

// Example debug statement output:
// DEBUG|field name is Name, value is TestMapAccount2
// DEBUG|field name is Phone, value is 123-4567
// DEBUG|field name is Id, value is 001R0000003EPPpIAO

The following example shows how to use the getPopulatedFieldsAsMap() method with related objects.

Account a = new Account();
a.name='TestMapAccount3';
insert as user a;
Contact c = new Contact();
c.firstname='TestContactFirstName';
c.lastName ='TestContactLastName';
c.accountid = a.id;
insert as user c;

c = \[SELECT Id, Contact.Firstname, Contact.Account.Name FROM Contact 
        WHERE id=:c.id WITH USER\_MODE LIMIT 1\];
Map<String, Object\> fieldsToValue = c.getPopulatedFieldsAsMap();

// To get the fields on Account, get the Account object 
// and call getMapPopulatedFieldsAsMap() on that object.

a = (Account)fieldsToValue.get('Account');
fieldsToValue = a.getPopulatedFieldsAsMap();

for (String fieldName : fieldsToValue.keySet()) {
    System.debug('field name is ' + fieldName + ', value is ' + 
        fieldsToValue.get(fieldName));
}

// Example debug statement output:
// DEBUG|field name is Id, value is 001R0000003EPPuIAO
// DEBUG|field name is Name, value is TestMapAccount3

#### 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(fieldName)

Returns the value for the specified field. This method is primarily used with dynamic DML to access values for external IDs.

#### Signature

public SObject getSObject(String fieldName)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")

#### Example

Account acc = new account(Name = 'Acme', Description = 'Acme Account');
insert as user acc;
Contact con = new Contact(Lastname = 'AcmeCon', AccountId = acc.id);
insert as user con;

SObject contactDB = 
    \[SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id WITH USER\_MODE LIMIT 1\];
Account a = (Account)contactDB.getSObject('Account');
Assert.areEqual('Acme', a.name);

### getSObject(field)

Returns the value for the field specified by the field token Schema.sObjectField, such as Schema.MyObj.MyExternalId. This method is primarily used with dynamic DML to access values for external IDs.

#### Signature

public SObject getSObject(Schema.SObjectField field)

#### Parameters

- **field**:
    
    Type: [Schema.SObjectField](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectField.htm#apex_class_Schema_SObjectField "A Schema.sObjectField object is returned from the field describe result using the getController and getSObjectField methods.")

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")

#### Usage

If the method references polymorphic fields, a [Name object](https://developer.salesforce.com/docs/atlas.en-us.264.0.object_reference.meta/object_reference/sforce_api_objects_name.htm) 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](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/langCon_apex_SOQL_polymorphic_relationships.htm).

#### Example

Account acc = new account(name = 'Acme', description = 'Acme Account');
insert as user acc;
Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id);
insert as user con;

Schema.DescribeFieldResult fieldResult = Contact.AccountId.getDescribe();
Schema.SObjectField field = fieldResult.getSObjectField();

SObject contactDB = 
    \[SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id WITH USER\_MODE LIMIT 1\];
Account a = (Account)contactDB.getSObject(field);
Assert.areEqual('Acme', a.name);

### getSObjects(fieldName)

Returns the values for the specified field. This method is primarily used with dynamic DML to access values for associated objects, such as child relationships.

#### Signature

public SObject\[\] getSObjects(String fieldName)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")\[\]

#### Usage

For more information, see [Dynamic DML](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_dynamic_dml.htm).

#### Example

Account acc = new account(name = 'Acme', description = 'Acme Account');
insert as user acc;
Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id);
insert as user con;

SObject\[\] a = \[SELECT id, (SELECT Name FROM Contacts LIMIT 1) FROM Account WHERE id = :acc.id\];
SObject\[\] contactsDB = a.get(0).getSObjects('Contacts');
String fieldValue = (String)contactsDB.get(0).get('Name');
Assert.areEqual('AcmeCon', fieldValue);

### getSObjects(fieldName)

Returns the value for the field specified by the field token Schema.fieldName, such as Schema.Account.Contact. This method is primarily used with dynamic DML to access values for associated objects, such as child relationships.

#### Signature

public SObject\[\] getSObjects(Schema.SObjectType fieldName)

#### Parameters

- **fieldName**:
    
    Type: [Schema.SObjectType](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectType.htm#apex_class_Schema_SObjectType "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.")

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")\[\]

### getSObjectType()

Returns the token for this SObject. This method is primarily used with describe information.

#### Signature

public Schema.SObjectType getSObjectType()

#### Return Value

Type: [Schema.SObjectType](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectType.htm#apex_class_Schema_SObjectType "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.")

#### Usage

For more information, see apex\_dynamic\_describe\_objects\_understanding.

#### Example

Account acc = new Account(name = 'Acme', description = 'Acme Account');
Schema.SObjectType expected = Schema.Account.getSObjectType();
Assert.areEqual(expected, acc.getSObjectType());

### getQuickActionName()

Retrieves the name of a quick action associated with this SObject. Typically used in triggers.

#### Signature

public String getQuickActionName()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Example

trigger accTrig2 on Contact (before insert) {
    for (Contact c : Trigger.new) {
        if (c.getQuickActionName() == QuickAction.CreateContact) {
            c.WhereFrom\_\_c = 'GlobalAction';
        } else if (c.getQuickActionName() == Schema.Account.QuickAction.CreateContact) {
            c.WhereFrom\_\_c = 'AccountAction';
        } else if (c.getQuickActionName() == null) {
            c.WhereFrom\_\_c = 'NoAction';
        } else {
            Assert.fail('Unexpected quick action: ' + c.getQuickActionName());
        }
    }
}

### hasErrors()

Returns true if an SObject instance has associated errors. The error message can be associated to the SObject instance by using SObject.addError(), validation rules, or by other means.

#### Signature

public Boolean hasErrors()

#### Return Value

Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")

### isClone()

Returns true if an entity is cloned from something, even if the entity hasn’t been saved. The method can only be used within the transaction where the entity is cloned, as clone information doesn’t persist in subsequent transactions.

#### Signature

public Boolean isClone()

#### Return Value

Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")

#### Example

Account acc = new Account(Name = 'Acme');
insert as user acc;
Account acc2 = acc.clone();
// Test before saving
Assert.isTrue(acc2.isClone());
insert as user acc2;
// Test after saving
Assert.isTrue(acc2.isClone());

### isSet(fieldName)

Returns information about the queried sObject field. Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. Returns false if the sObject field isn’t set. If an invalid field is specified, an SObjectException is thrown.

#### Signature

public Boolean isSet(String fieldName)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")

#### 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

Contact  c = new Contact(LastName = 'Joyce');
Assert.isTrue(c.isSet('LastName'));
Assert.isFalse(c.isSet('FirstName')); // FirstName field is not written to
c.firstName = null;
Assert.isTrue(c.isSet('FirstName')); //FirstName field is written to

### isSet(field)

Returns information about the queried sObject field. Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. Returns false if the sObject field isn’t set. If an invalid field is specified, an SObjectException is thrown.

#### Signature

public Boolean isSet(Schema.SObjectField field)

#### Parameters

- **field**:
    
    Type:[SObjectField Class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectField.htm#apex_class_Schema_SObjectField "A Schema.sObjectField object is returned from the field describe result using the getController and getSObjectField methods.")

#### Return Value

Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")

#### 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

Contact  newContact = new Contact(LastName = 'Joyce');
insert(newContact); //Insert a new contact with last name Joyce
Contact c = \[SELECT FirstName FROM Contact WHERE Id = :newContact.Id WITH USER\_MODE\];
Assert.isTrue(c.isSet(Contact.FirstName)); //FirstName field in query
Assert.isFalse(c.isSet(Contact.LastName)); //LastName field not in query

### put(fieldName, value)

Sets the value for the specified field and returns the previous value for the field.

#### Signature

public Object put(String fieldName, Object value)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **value**:
    
    Type: Object

#### Return Value

Type: Object

#### Example

Account acc = new Account(name = 'test', description = 'old desc');
String oldDesc = (String)acc.put('description', 'new desc');
Assert.areEqual('old desc', oldDesc);
Assert.areEqual('new desc', acc.description);

### put(field, value)

Sets the value for the field specified by the field token Schema.sObjectField, such as Schema.Account.AccountNumber and returns the previous value for the field.

#### Signature

public Object put(Schema.SObjectField field, Object value)

#### Parameters

- **field**:
    
    Type: [Schema.SObjectField](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectField.htm#apex_class_Schema_SObjectField "A Schema.sObjectField object is returned from the field describe result using the getController and getSObjectField methods.")
- **value**:
    
    Type: Object

#### Return Value

Type: Object

#### Example

Account acc = new Account(name = 'test', description = 'old desc');
String oldDesc = (String)acc.put(Schema.Account.Description, 'new desc');
Assert.areEqual('old desc', oldDesc);
Assert.areEqual('new desc', acc.description);

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

Field tokens aren't available for person accounts. If you access Schema.Account.fieldname, where _fieldname_ is a placeholder value, you get an exception error. Instead, specify the field name as a string.

### putSObject(fieldName, value)

Sets the value for the specified field. This method is primarily used with dynamic DML for setting external IDs. The method returns the previous value of the field.

#### Signature

public SObject putSObject(String fieldName, SObject value)

#### Parameters

- **fieldName**:
    
    Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **value**:
    
    Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")

#### Example

Account acc = new Account(name = 'Acme', description = 'Acme Account');
insert as user acc;
Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id);
insert as user con;
Account acc2 = new account(name = 'Not Acme');

Contact contactDB = 
    (Contact)\[SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id WITH USER\_MODE LIMIT 1\];
Account a = (Account)contactDB.putSObject('Account', acc2);
Assert.areEqual('Acme', a.name);
Assert.areEqual('Not Acme', contactDB.Account.name);

### putSObject(fieldName, value)

Sets the value for the field specified by the token Schema.SObjectType. This method is primarily used with dynamic DML for setting external IDs. The method returns the previous value of the field.

#### Signature

public SObject putSObject(Schema.SObjectType fieldName, SObject value)

#### Parameters

- **fieldName**:
    
    Type: [Schema.SObjectType](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_SObjectType.htm#apex_class_Schema_SObjectType "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.")
- **value**:
    
    Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")

#### Return Value

Type: [SObject](#apex_methods_system_sobject "Contains methods for the sObject data type.")

### recalculateFormulas()

**Deprecated as of API version 57.0. Use the recalculateFormulas() method in the System.Formula class instead.**

#### 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](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_gov_limits.htm).

#### See Also

- [recalculateFormulas(sobjects)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Formula.htm#apex_System_Formula_recalculateFormulas "Updates (recalculates) all formula fields on the input SObjects.")
- [What Is a Cross-Object Formula?](https://help.salesforce.com/HTViewHelpDoc?id=customize_cross_object.htm&language=en_US "What Is a Cross-Object Formula? - HTML (New Window)")

### setOptions(DMLOptions)

Sets the DMLOptions object for the SObject.

#### Signature

public Void setOptions(database.DMLOptions DMLOptions)

#### Parameters

- **DMLOptions**:
    
    Type: [Database.DMLOptions](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_dmloptions.htm#apex_methods_system_database_dmloptions "Enables you to set options related to DML operations.")

#### Return Value

Type: Void

#### Example

Database.DMLOptions dmo = new Database.dmlOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;

Account acc = new Account(Name = 'Acme');
acc.setOptions(dmo);