sObject methods are all instance methods, that is, they are called by and operate on a particular instance of an sObject, such as an account or contact. The following are the instance methods for sObjects.
For more information on sObjects, see Working with sObjects.
The following are methods for SObject. All are instance methods.
public Void addError(String errorMsg)
The error message to mark the record with.
Type: Void
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.
Trigger.new[0].addError('bad');
public Void addError(String errorMsg, Boolean escape)
The error message to mark the record with.
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.
Type: Void
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.
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 and make sure you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call addError(String errorMsg) instead.
Trigger.new[0].addError('Fix & resubmit', false);
public Void addError(Exception exceptionError)
An Exception object or a custom exception object that contains the error message to mark the record with.
Type: Void
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.
public class MyException extends Exception {} Trigger.new[0].addError(new myException('Invalid Id'));
public Void addError(Exception exceptionError, Boolean escape)
An Exception object or a custom exception object that contains the error message to mark the record with.
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.
Type: Void
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 and make sure you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call addError(Exception e) instead.
public class MyException extends Exception {} Trigger.new[0].addError(new myException('Invalid Id & other issues', false));
public Void addError(String errorMsg)
Type: Void
See Triggers and Trigger Exceptions.
Trigger.new[0].myField__c.addError('bad');
public Void addError(String errorMsg, Boolean escape)
The error message to mark the record with.
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.
Type:
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 and make sure you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call field.addError(String errorMsg) instead.
Trigger.new[0].myField__c.addError('Fix & resubmit', false);
public Void clear()
Type: Void
Account acc = new account(Name = 'Acme'); acc.clear(); Account expected = new Account(); system.assertEquals(expected, acc);
public sObject clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)
Type: sObject (of same type)
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.
Account acc = new account(Name = 'Acme', Description = 'Acme Account'); Account clonedAcc = acc.clone(false, false, false, false); System.assertEquals(acc, clonedAcc);
public Object get(String fieldName)
Type: Object
For more information, see Dynamic SOQL.
Account acc = new account(Name = 'Acme', Description = 'Acme Account'); String description = (String)acc.get('Description'); System.assertEquals('Acme Account', description);
public Object get(Schema.sObjectField field)
Type: Object
For more information, see Dynamic SOQL.
Field tokens aren't available for person accounts. If you access Schema.Account.fieldname, you'll get an exception error. Instead, specify the field name as a string.
Account acc = new account(Name = 'Acme', Description = 'Acme Account'); String description = (String)acc.get(Schema.Account.Description); System.assertEquals('Acme Account', description);
public Id getCloneSourceId()
Type: Id
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.
Account acc0 = new Account(Name = 'Acme'); insert acc0; Account acc1 = acc0.clone(); Account acc2 = acc1.clone(); Account acc3 = acc2.clone(); Account acc4 = acc3.clone(); System.assert(acc0.Id != null); System.assertEquals(acc0.Id, acc1.getCloneSourceId()); System.assertEquals(acc0.Id, acc2.getCloneSourceId()); System.assertEquals(acc0.Id, acc3.getCloneSourceId()); System.assertEquals(acc0.Id, acc4.getCloneSourceId()); System.assertEquals(null, acc0.getCloneSourceId());
public Database.DMLOptions getOptions()
Type: Database.DMLOptions
Database.DMLOptions dmo = new Database.dmlOptions(); dmo.assignmentRuleHeader.useDefaultRule = true; Account acc = new Account(Name = 'Acme'); acc.setOptions(dmo); Database.DMLOptions accDmo = acc.getOptions();
public Map<String,Object> getPopulatedFieldsAsMap()
Type: Map<String,Object>
A map of field names and their corresponding values.
The following example iterates over the map returned by the getPopulatedFieldsAsMap() method after a SOQL query.
Account a = new Account(); a.name = 'TestMapAccount1'; insert a; a = [select Id,Name from Account where id=:a.Id]; 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 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 a; Contact c = new Contact(); c.firstname='TestContactFirstName'; c.lastName ='TestContactLastName'; c.accountid = a.id; insert c; c = [SELECT id, Contact.Firstname, Contact.Account.Name FROM Contact where id=:c.id 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
public sObject getSObject(String fieldName)
Type: sObject
Account acc = new account(Name = 'Acme', Description = 'Acme Account'); insert acc; Contact con = new Contact(Lastname = 'AcmeCon', AccountId = acc.id); insert con; SObject contactDB = [SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id LIMIT 1]; Account a = (Account)contactDB.getSObject('Account'); System.assertEquals('Acme', a.name);
public sObject getSObject(Schema.SObjectField fieldName)
Type: sObject
Account acc = new account(name = 'Acme', description = 'Acme Account'); insert acc; Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id); insert 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 LIMIT 1]; Account a = (Account)contactDB.getSObject(field); System.assertEquals('Acme', a.name);
public sObject[] getSObjects(String fieldName)
Type: sObject[]
For more information, see Dynamic DML.
Account acc = new account(name = 'Acme', description = 'Acme Account'); insert acc; Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id); insert 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'); System.assertEquals('AcmeCon', fieldValue);
public sObject[] getSObjects(Schema.SObjectType fieldName)
Type: sObject[]
public Schema.SObjectType getSObjectType()
Type: Schema.SObjectType
For more information, see Understanding Apex Describe Information.
Account acc = new Account(name = 'Acme', description = 'Acme Account'); Schema.sObjectType expected = Schema.Account.getSObjectType(); System.assertEquals(expected, acc.getSobjectType());
public String getQuickActionName()
Type: String
trigger accTrig2 on Contact (before insert) { for (Contact c : Trigger.new) { if (c.getQuickActionName() == QuickAction.CreateContact) { c.WhereFrom__c = 'GlobaActionl'; } else if (c.getQuickActionName() == Schema.Account.QuickAction.CreateContact) { c.WhereFrom__c = 'AccountAction'; } else if (c.getQuickActionName() == null) { c.WhereFrom__c = 'NoAction'; } else { System.assert(false); } } }
public Boolean isClone()
Type: Boolean
Account acc = new Account(Name = 'Acme'); insert acc; Account acc2 = acc.clone(); // Test before saving System.assertEquals(true, acc2.isClone()); insert acc2; // Test after saving System.assertEquals(true, acc2.isClone());
public Object put(String fieldName, Object value)
Type: Object
Account acc = new Account(name = 'test', description = 'old desc'); String oldDesc = (String)acc.put('description', 'new desc'); System.assertEquals('old desc', oldDesc); System.assertEquals('new desc', acc.description);
public Object put(Schema.SObjectField fieldName, Object value)
Type: Object
Account acc = new Account(name = 'test', description = 'old desc'); String oldDesc = (String)acc.put(Schema.Account.Description, 'new desc'); System.assertEquals('old desc', oldDesc); System.assertEquals('new desc', acc.description);
Field tokens aren't available for person accounts. If you access Schema.Account.fieldname, you'll get an exception error. Instead, specify the field name as a string.
public sObject putSObject(String fieldName, sObject value)
Type: sObject
Account acc = new Account(name = 'Acme', description = 'Acme Account'); insert acc; Contact con = new contact(lastname = 'AcmeCon', accountid = acc.id); insert con; Account acc2 = new account(name = 'Not Acme'); Contact contactDB = (Contact)[SELECT Id, AccountId, Account.Name FROM Contact WHERE id = :con.id LIMIT 1]; Account a = (Account)contactDB.putSObject('Account', acc2); System.assertEquals('Acme', a.name); System.assertEquals('Not Acme', contactDB.Account.name);
public sObject putSObject(Schema.sObjectType fieldName, sObject value)
Type: sObject
public Void recalculateFormulas()
Type: Void
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.
public Void setOptions(database.DMLOptions DMLOptions)
Type: Void
Database.DMLOptions dmo = new Database.dmlOptions(); dmo.assignmentRuleHeader.useDefaultRule = true; Account acc = new Account(Name = 'Acme'); acc.setOptions(dmo);