SObjectType Class

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.

Namespace

Schema

Usage

Schema.DescribeFieldResult F = Account.Industry.getDescribe();
List<Schema.sObjectType> P = F.getReferenceTo();

SObjectType Methods

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

getDescribe()

Returns the describe sObject result for this field.

Signature

public Schema.DescribeSObjectResult getDescribe()

Return Value

Type: Schema.DescribeSObjectResult

newSObject()

Constructs a new sObject of this type.

Signature

public sObject newSObject()

Return Value

Type: sObject

Example

For an example, see Dynamic sObject Creation Example.

newSObject(id)

Constructs a new sObject of this type, with the specified ID.

Signature

public sObject newSObject(ID id)

Parameters

id
Type: ID

Return Value

Type: sObject

Usage

For the argument, pass the ID of an existing record in the database.

After you create a new sObject, the sObject returned has all fields set to null. You can set any updateable field to desired values and then update the record in the database. Only the fields you set new values for are updated and all other fields which are not system fields are preserved.

newSObject(recordTypeId, loadDefaults)

Constructs a new sObject of this type, and optionally, of the specified record type ID and with default custom field values.

Signature

public sObject newSObject(ID recordTypeId, Boolean loadDefaults)

Parameters

recordTypeId
Type: ID
Specifies the record type ID of the sObject to create. If no record type exists for this sObject, use null. If the sObject has record types and you specify null, the default record type is used.
loadDefaults
Type: Boolean
Specifies whether to populate custom fields with their predefined default values (true) or not (false).

Return Value

Type: sObject

Usage

  • For required fields that have no default values, make sure to provide a value before inserting the new sObject. Otherwise, the insertion results in an error. An example is the Account Name field or a master-detail relationship field.
  • Since picklists and multi-select picklists can have default values specified per record type, this method populates the default value corresponding to the record type specified.
  • If fields have no predefined default values and the loadDefaults argument is true, this method creates the sObject with field values of null.
  • If the loadDefaults argument is false, this method creates the sObject with field values of null.
  • This method populates read-only custom fields of the new sObject with default values. You can then insert the new sObject with the read-only fields, even though these fields cannot be edited after they’re inserted.
  • If a custom field is marked as unique and also provides a default value, inserting more than one new sObject will cause a run-time exception because of duplicate field values.

To learn more about default field values, see “About Default Field Values” in the Salesforce online help.

Example: Creating New sObject with Default Values

This sample creates an account with any default values populated for its custom fields, if any, using the newSObject method. It also creates a second account for a specific record type. For both accounts, the sample sets the Name field, which is a required field that doesn’t have a default value, before inserting the new accounts.

// Create an account with predefined default values
Account acct = (Account)Account.sObjectType.newSObject(null, true);
// Provide a value for Name
acct.Name = 'Acme';
// Insert new account
insert acct;

// This is for record type RT1 of Account
ID rtId = [SELECT Id FROM RecordType WHERE sObjectType='Account' AND Name='RT1'].Id;
Account acct2 = (Account)Account.sObjectType.newSObject(rtId, true);
// Provide a value for Name
acct2.Name = 'Acme2';
// Insert new account
insert acct2;