Create a custom entity

 

Updated: November 29, 2016

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

This topic shows how to create a custom user-owned entity called Bank Account and add four different types of attributes to it.

You can also create organization-owned custom entities. More information:  Entity ownership

System_CAPS_noteNote

You won’t be able to see this entity in the application navigation unless the entity properties are edited to set the Areas that display this entity are set.

The following sample uses the CreateEntityRequest to create the entity and the StringAttributeMetadata PrimaryAttribute.

The _customEntityName value is “new_bankaccount”.



Dim createrequest As CreateEntityRequest = New CreateEntityRequest With {
 .Entity = New EntityMetadata With {
  .SchemaName = _customEntityName,
  .DisplayName = New Label("Bank Account", 1033),
  .DisplayCollectionName = New Label("Bank Accounts", 1033),
  .Description = New Label("An entity to store information about customer bank accounts", 1033),
  .OwnershipType = OwnershipTypes.UserOwned,
  .IsActivity = False},
 .PrimaryAttribute = New StringAttributeMetadata With {
  .SchemaName = "new_accountname",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .MaxLength = 100,
  .Format = StringFormat.Text,
  .DisplayName = New Label("Account Name", 1033),
  .Description = New Label("The primary attribute for the Bank Account entity.", 1033)
 }
}
'Define the entity
' Define the primary attribute for the entity
_serviceProxy.Execute(createrequest)
Console.WriteLine("The bank account entity has been created.")

The following sample adds a StringAttributeMetadata attribute to the Bank Account entity.



Dim createBankNameAttributeRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = _customEntityName,
 .Attribute = New StringAttributeMetadata With {
  .SchemaName = "new_bankname",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .MaxLength = 100,
  .Format = StringFormat.Text,
  .DisplayName = New Label("Bank Name", 1033),
  .Description = New Label("The name of the bank.", 1033)
 }
}

_serviceProxy.Execute(createBankNameAttributeRequest)

The following sample adds a MoneyAttributeMetadata attribute to the Bank Account entity.



Dim createBalanceAttributeRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = _customEntityName,
 .Attribute = New MoneyAttributeMetadata With {
  .SchemaName = "new_balance",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .PrecisionSource = 2,
  .DisplayName = New Label("Balance", 1033),
  .Description = New Label("Account Balance at the last known date", 1033)
 }
}

_serviceProxy.Execute(createBalanceAttributeRequest)

The following sample adds a DateTimeAttributeMetadata attribute to the Bank Account entity.



Dim createCheckedDateRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = _customEntityName,
 .Attribute = New DateTimeAttributeMetadata With {
  .SchemaName = "new_checkeddate",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .Format = DateTimeFormat.DateOnly,
  .DisplayName = New Label("Date", 1033),
  .Description = New Label("The date the account balance was last confirmed", 1033)
 }
}

_serviceProxy.Execute(createCheckedDateRequest)
Console.WriteLine("An date attribute has been added to the bank account entity.")

The following sample uses CreateOneToManyRequest to create a one-to-many relationship with the Contact entity so that a LookupAttributeMetadata attribute is added to the Bank Account entity.



Dim req As New CreateOneToManyRequest() With {
 .Lookup = New LookupAttributeMetadata() With {
  .Description = New Label("The owner of the bank account", 1033),
  .DisplayName = New Label("Account Owner", 1033),
  .LogicalName = "new_parent_contactid",
  .SchemaName = "New_Parent_ContactId",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired)},
 .OneToManyRelationship = New OneToManyRelationshipMetadata() With {
  .AssociatedMenuConfiguration = New AssociatedMenuConfiguration() With {
   .Behavior = AssociatedMenuBehavior.UseCollectionName,
   .Group = AssociatedMenuGroup.Details,
   .Label = New Label("Bank Accounts", 1033),
   .Order = 10000},
  .CascadeConfiguration = New CascadeConfiguration() With {
   .Assign = CascadeType.Cascade,
   .Delete = CascadeType.Cascade,
   .Merge = CascadeType.Cascade,
   .Reparent = CascadeType.Cascade,
   .Share = CascadeType.Cascade,
   .Unshare = CascadeType.Cascade},
  .ReferencedEntity = Contact.EntityLogicalName,
  .ReferencedAttribute = "contactid",
  .ReferencingEntity = _customEntityName,
  .SchemaName = "new_contact_new_bankaccount"
 }
}
_serviceProxy.Execute(req)

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright

Community Additions

ADD
Show: