Search code examples
c#entitydynamics-crmmetadata

Set Value Entity Regardingobjectid Field With Object Type Code Using C#


I have an entity. This entity's type is activity. I want to set this entity's regardingobjectid field value and then i want to create record. Each record's regardingobjectid field can show different entity. Namely i set this field "a" entity, and then i can set this field with "b" entity on next record. I only have "a" and "b" entities's logicalname. I take object type code with using logicalname. And i take entity metadata with this object type code. How can i take entity id from entity metadata or object typecode with using C#. If i take entity id (a or b) and then i will set it to regardingobjectid which is in activity type entity.


Solution

  • Your wording is pretty confusing. Here is an example which I think covers what you are trying to achieve.

    Create a contact, then create a task with the regarding field populated with the contact.

    Entity contact = new Entity("contact");
    contact["lastname"] = "Test Contact";
    Guid contactId = service.Create(contact);
    
    Entity task = new Entity("task");
    task["subject"] = "Test Task";
    task["regardingobjectid"] = new EntityReference("contact", contactId);
    service.Create(task);
    

    You could also update an existing task like so:

    Entity task2 = new Entity("task");
    task2.Id = new Guid(...);
    task2["regardingobjectid"] = new EntityReference("contact", contactId);
    service.Create(task);
    

    You can get the record id (for use here new Guid(...) or here contactId) in a variety of ways.

    The record id refers to a specific record in CRM, e.g. a row in a table.

    If you have created the record using a service call the id is returned to you immediately.

    Entity contact = new Entity("contact");
    ...
    Guid recordId = service.Create(contact);
    

    You can query the record id from CRM. Further reading: IOrganizationService.RetrieveMultiple.

    Guid recordId = Service.RetrieveMultiple(new QueryExpression("contact")).Entities.First().Id;
    

    If you have an Entity object returned from CRM you always get its record id from the Id property.

    Entity contact = new Entity("contact");
    Guid recordId = contact.Id;
    

    Side Notes

    Object Type Code (also know as Entity Type Code)

    This isn't usually used for anything, it's a piece of data that has a small number of uses - I rarely find it useful or use it.

    The type code can be used to refer to a type of entity, e.g. contact, case, etc. However it's preferred to use the entity schema name, e.g. contact, incident, etc.

    It's also worth remembering that is isn't reliable - especially in the case of custom entities.

    ObjectTypeCode Property

    Always use the entity schema name (SchemaName) to refer to a custom entity in code and queries. Do not use the object type code (also referred to as entity type) code because its integer value varies for custom entities in different organizations.

    Metadata Services

    You can use the metadata services to receive (and manipulate) information regarding the configuration of the system. E.g. which entities you have and what fields those entities have. It will not give you information about specific records however.

    Further reading: The metadata and data models in Microsoft Dynamics 365.