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
1Trigger.new[0].addError('bad');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 Lightning Experience and the Salesforce 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. This results in the HTML markup not being rendered; instead it is displayed as text in the Salesforce user interface.
Example
1Trigger.new[0].addError('Fix & resubmit', false);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
1public class MyException extends Exception {}
2Trigger.new[0].addError(new myException('Invalid Id'));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 Lightning Experience and the Salesforce app and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Return Value
Type: Void
Usage
Example
1public class MyException extends Exception {}
2Trigger.new[0].addError(new myException('Invalid Id & other issues', false));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
1Trigger.new[0].myField__c.addError('bad');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 Lightning Experience and the Salesforce app and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Return Value
Type:
Usage
Example
1Trigger.new[0].myField__c.addError('Fix & resubmit', false);clear()
Signature
public Void clear()
Return Value
Type: Void
Example
1Account acc = new account(Name = 'Acme');
2acc.clear();
3Account expected = new Account();
4system.assertEquals(expected, acc);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
1Account acc = new account(Name = 'Acme', Description = 'Acme Account');
2Account clonedAcc = acc.clone(false, false, false, false);
3System.assertEquals(acc, clonedAcc);get(fieldName)
Signature
public Object get(String fieldName)
Parameters
- fieldName
- Type: String
Return Value
Type: Object
Usage
For more information, see Dynamic SOQL.
Example
1Account acc = new account(Name = 'Acme', Description = 'Acme Account');
2String description = (String)acc.get('Description');
3System.assertEquals('Acme Account', description);get(field)
Signature
public Object get(Schema.sObjectField field)
Parameters
- field
- Type: Schema.SObjectField
Return Value
Type: Object
Example
1Account acc = new account(Name = 'Acme', Description = 'Acme Account');
2String description = (String)acc.get(Schema.Account.Description);
3System.assertEquals('Acme Account', description);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
1Account acc0 = new Account(Name = 'Acme');
2insert acc0;
3Account acc1 = acc0.clone();
4Account acc2 = acc1.clone();
5Account acc3 = acc2.clone();
6Account acc4 = acc3.clone();
7System.assert(acc0.Id != null);
8System.assertEquals(acc0.Id, acc1.getCloneSourceId());
9System.assertEquals(acc0.Id, acc2.getCloneSourceId());
10System.assertEquals(acc0.Id, acc3.getCloneSourceId());
11System.assertEquals(acc0.Id, acc4.getCloneSourceId());
12System.assertEquals(null, acc0.getCloneSourceId());getOptions()
Signature
public Database.DMLOptions getOptions()
Return Value
Type: Database.DMLOptions
Example
1Database.DMLOptions dmo = new Database.dmlOptions();
2dmo.assignmentRuleHeader.useDefaultRule = true;
3
4Account acc = new Account(Name = 'Acme');
5acc.setOptions(dmo);
6Database.DMLOptions accDmo = acc.getOptions();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.
1Account a = new Account();
2a.name = 'TestMapAccount1';
3insert a;
4a = [select Id,Name from Account where id=:a.Id];
5Map<String, Object> fieldsToValue = a.getPopulatedFieldsAsMap();
6
7for (String fieldName : fieldsToValue.keySet()){
8 System.debug('field name is ' + fieldName + ', value is ' +
9 fieldsToValue.get(fieldName));
10}
11
12// Example debug statement output:
13// DEBUG|field name is Id, value is 001R0000003EPPkIAO
14// DEBUG|field name is Name, value is TestMapAccount1This example iterates over the map returned by the getPopulatedFieldsAsMap() method after fields on the SObject are explicitly set.
1Account a = new Account();
2a.name = 'TestMapAccount2';
3a.phone = '123-4567';
4insert a;
5Map<String, Object> fieldsToValue = a.getPopulatedFieldsAsMap();
6
7for (String fieldName : fieldsToValue.keySet()) {
8 System.debug('field name is ' + fieldName + ', value is ' +
9 fieldsToValue.get(fieldName));
10}
11
12// Example debug statement output:
13// DEBUG|field name is Name, value is TestMapAccount2
14// DEBUG|field name is Phone, value is 123-4567
15// DEBUG|field name is Id, value is 001R0000003EPPpIAOThe following example shows how to use the getPopulatedFieldsAsMap() method with related objects.
1Account a = new Account();
2a.name='TestMapAccount3';
3insert a;
4Contact c = new Contact();
5c.firstname='TestContactFirstName';
6c.lastName ='TestContactLastName';
7c.accountid = a.id;
8insert c;
9
10c = [SELECT id, Contact.Firstname, Contact.Account.Name FROM Contact
11 where id=:c.id limit 1];
12Map<String, Object> fieldsToValue = c.getPopulatedFieldsAsMap();
13
14// To get the fields on Account, get the Account object
15// and call getMapPopulatedFieldsAsMap() on that object.
16
17a = (Account)fieldsToValue.get('Account');
18fieldsToValue = a.getPopulatedFieldsAsMap();
19
20for (String fieldName : fieldsToValue.keySet()) {
21 System.debug('field name is ' + fieldName + ', value is ' +
22 fieldsToValue.get(fieldName));
23}
24
25// Example debug statement output:
26// DEBUG|field name is Id, value is 001R0000003EPPuIAO
27// DEBUG|field name is Name, value is TestMapAccount3getSObject(fieldName)
Signature
public SObject getSObject(String fieldName)
Parameters
- fieldName
- Type: String
Return Value
Type: SObject
Example
1Account acc = new account(Name = 'Acme', Description = 'Acme Account');
2insert acc;
3Contact con = new Contact(Lastname = 'AcmeCon', AccountId = acc.id);
4insert con;
5
6SObject contactDB =
7 [SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id LIMIT 1];
8Account a = (Account)contactDB.getSObject('Account');
9System.assertEquals('Acme', a.name);getSObject(fieldName)
Signature
public SObject getSObject(Schema.SObjectField fieldName)
Parameters
- fieldName
- Type: Schema.SObjectField
Return Value
Type: SObject
Example
1Account acc = new account(name = 'Acme', description = 'Acme Account');
2insert acc;
3Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id);
4insert con;
5
6Schema.DescribeFieldResult fieldResult = Contact.AccountId.getDescribe();
7Schema.SObjectField field = fieldResult.getSObjectField();
8
9SObject contactDB =
10 [SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id LIMIT 1];
11Account a = (Account)contactDB.getSObject(field);
12System.assertEquals('Acme', a.name);getSObjects(fieldName)
Signature
public SObject[] getSObjects(String fieldName)
Parameters
- fieldName
- Type: String
Return Value
Type: SObject[]
Usage
For more information, see Dynamic DML.
Example
1Account acc = new account(name = 'Acme', description = 'Acme Account');
2insert acc;
3Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id);
4insert con;
5
6SObject[] a = [SELECT id, (SELECT Name FROM Contacts LIMIT 1) FROM Account WHERE id = :acc.id];
7SObject[] contactsDB = a.get(0).getSObjects('Contacts');
8String fieldValue = (String)contactsDB.get(0).get('Name');
9System.assertEquals('AcmeCon', fieldValue);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 Understanding Apex Describe Information.
Example
1Account acc = new Account(name = 'Acme', description = 'Acme Account');
2Schema.SObjectType expected = Schema.Account.getSObjectType();
3System.assertEquals(expected, acc.getSObjectType());getQuickActionName()
Signature
public String getQuickActionName()
Return Value
Type: String
Example
1trigger accTrig2 on Contact (before insert) {
2 for (Contact c : Trigger.new) {
3 if (c.getQuickActionName() == QuickAction.CreateContact) {
4 c.WhereFrom__c = 'GlobaActionl';
5 } else if (c.getQuickActionName() == Schema.Account.QuickAction.CreateContact) {
6 c.WhereFrom__c = 'AccountAction';
7 } else if (c.getQuickActionName() == null) {
8 c.WhereFrom__c = 'NoAction';
9 } else {
10 System.assert(false);
11 }
12 }
13}isClone()
Signature
public Boolean isClone()
Return Value
Type: Boolean
Example
1Account acc = new Account(Name = 'Acme');
2insert acc;
3Account acc2 = acc.clone();
4// Test before saving
5System.assertEquals(true, acc2.isClone());
6insert acc2;
7// Test after saving
8System.assertEquals(true, acc2.isClone());put(fieldName, value)
Signature
public Object put(String fieldName, Object value)
Parameters
- fieldName
- Type: String
- value
- Type: Object
Return Value
Type: Object
Example
1Account acc = new Account(name = 'test', description = 'old desc');
2String oldDesc = (String)acc.put('description', 'new desc');
3System.assertEquals('old desc', oldDesc);
4System.assertEquals('new desc', acc.description);put(fieldName, value)
Signature
public Object put(Schema.SObjectField fieldName, Object value)
Parameters
- fieldName
- Type: Schema.SObjectField
- value
- Type: Object
Return Value
Type: Object
Example
1Account acc = new Account(name = 'test', description = 'old desc');
2String oldDesc = (String)acc.put(Schema.Account.Description, 'new desc');
3System.assertEquals('old desc', oldDesc);
4System.assertEquals('new desc', acc.description);putSObject(fieldName, value)
Signature
public SObject putSObject(String fieldName, SObject value)
Return Value
Type: SObject
Example
1Account acc = new Account(name = 'Acme', description = 'Acme Account');
2insert acc;
3Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id);
4insert con;
5Account acc2 = new account(name = 'Not Acme');
6
7Contact contactDB =
8 (Contact)[SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id LIMIT 1];
9Account a = (Account)contactDB.putSObject('Account', acc2);
10System.assertEquals('Acme', a.name);
11System.assertEquals('Not Acme', contactDB.Account.name);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
Example
1Database.DMLOptions dmo = new Database.dmlOptions();
2dmo.assignmentRuleHeader.useDefaultRule = true;
3
4Account acc = new Account(Name = 'Acme');
5acc.setOptions(dmo);