Use the Entity class for create, update and delete

 

Updated: November 29, 2016

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

In Microsoft Dynamics 365 (online & on-premises), you can use the Entity class to create, update, and delete entities and entity attributes.

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);

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright

Community Additions

ADD
Show: