Share via


Use the Entity class for create, update and delete

 

Applies To: Dynamics CRM 2013

In Microsoft Dynamics CRM 2013 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 metadata 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

Use the Entity class to add or update associations between related records
Use the late bound entity class in code