Use the Entity Class for Create, Update and Delete

[Applies to: Microsoft Dynamics CRM 2011]

In Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online, you can use the Entity class to create, update, and delete entities and entity attributes.

Create, Update, and Delete using the Entity class

When you work with the Entity class and use late binding, you work with the entity and logical attribute name. This contrasts with early binding where you work with the entity and attribute schema name. The logical attribute name is all lowercase whereas the schema attribute name uses Pascal casing.

To create a new entity, you first create a new instance of the Entity class and pass it an entity name. The following code example shows how to create a new account record.

// Instantiate an account object.
Entity account = new Entity("account");

// Set the required attributes. For account, only the name is required. 
// See the Entity Metadata topic in the SDK documentation to determine 
// which attributes must be set for each entity.
account["name"] = "Fourth Coffee";

// Create an account record named Fourth Coffee.
_accountId = _orgService.Create(account);

In the example, a new entity object is created of type “account,” attributes are set, and then the IOrganizationService.Create method is called to create the new record.

To update an entity, you set a value for the attribute to be updated, and then call the IOrganizationService.Update method. The following code example shows how to update an attribute in an account.

Entity account = new Entity("account");

// Create a column set to define which attributes should be retrieved.
ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid" }); 

// Retrieve the account and its name and ownerid attributes.
account = _orgService.Retrieve(account.LogicalName, _accountId, attributes);

// Update the postal code attribute.
account["address1_postalcode"] = "98052";

// Update the account.
_orgService.Update(account);

To delete an entity, you can pass the key attribute information to the IOrganizationService.Delete method. The following code example shows how to use the Delete method.

_orgService.Delete("account", _accountId);

See Also

Concepts

Use the Entity Class to Add or Update Associations Between Related Records
Use the Late Bound Entity Class in Code

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.