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 sObject Types.
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)
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.
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).
Type: Void
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)
Type:
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)
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.
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 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);
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.
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);