DYNAMICS 365 HOW TO CREATE AND MANAGE POLYMORPHIC LOOKUP ATTRIBUTE IN WEBAPI

Previously, we saw how to create a Polymorphic Lookup Attribute using the Polymorphic Lookup Manager plugin and how to create a Polymorphic Lookup Attribute using SDK

In this post, we will see how to create a Polymorphic Lookup Attribute using WebAPI. We will see as well, what are the different actions that can be done against a polymorphic lookup attribute using this method.

  1. In order to apply these actions, I will be using Postman to call the needed APIs requests
  2. First, you have to create an App Registration in Azure and enable its options as explained in this post to be able to connect to Dynamics 365 environment through Postman using the OAUTH authentication
  3. After connecting to Dynamics 365 environment in Postman, you have to send a POST request to create the Polymorphic Lookup Attribute with its relationships.

    In this exemple, I will create the Polymorphic Lookup Attribute with four relationships under the account table
    • Method: POST
    • API request URL: https://<ORGURL>/api/data/v9.2/CreatePolymorphicLookupAttribute
    • Request body:
      {
      "OneToManyRelationships": [
      {
      "SchemaName": "account_cak_contractor",
      "ReferencingEntity": "account",
      "ReferencedEntity": "cak_contractor"
      },
      {
      "SchemaName": "account_cak_education",
      "ReferencingEntity": "account",
      "ReferencedEntity": "cak_education"
      },
      {
      "SchemaName": "account_cak_health",
      "ReferencingEntity": "account",
      "ReferencedEntity": "cak_health"
      },
      {
      "SchemaName": "account_cak_university",
      "ReferencingEntity": "account",
      "ReferencedEntity": "cak_university"
      }
      ],

      "Lookup": {
      "AttributeType": "Lookup",
      "AttributeTypeName": {
      "Value": "LookupType"
      },

      "Description": {
      "@odata.type": "Microsoft.Dynamics.CRM.Label",
      "LocalizedLabels": [
      {
      "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
      "Label": "Parent Sector",
      "LanguageCode": 1033
      }
      ],

      "UserLocalizedLabel": {
      "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
      "Label": "Parent Sector",
      "LanguageCode": 1033
      }
      },

      "DisplayName": {
      "@odata.type": "Microsoft.Dynamics.CRM.Label",
      "LocalizedLabels": [
      {
      "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
      "Label": "Parent Sector",
      "LanguageCode": 1033
      }
      ],

      "UserLocalizedLabel": {
      "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
      "Label": "Parent Sector",
      "LanguageCode": 1033
      }
      },

      "SchemaName": "cak_ParentSectorId",
      "@odata.type": "Microsoft.Dynamics.CRM.ComplexLookupAttributeMetadata"
      }
      }
    • Reponse: N:1 Relationship Guids and Polymorphic Lookup Attribute Guid
    Polymorphic lookup webapi 1

    Polymorphic lookup webapi 2

  4. Once the Polymorphic Lookup Attribute is created, you can achieve the following actions against it using WebAPI
    • Add new relationship to an existing Polymorphic Lookup Attribute
      • Method: POST
      • API request URL: https://<ORGURL>/api/data/v9.2/RelationshipDefinitions
      • Request body:
        {
        "SchemaName": "cak_account_contact",
        "@odata.type": "Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata",
        "ReferencedEntity": "contact",
        "ReferencingEntity": "account",
        "Lookup": {
        "AttributeType": "Lookup",

        "AttributeTypeName": {
        "Value": "LookupType"
        },

        "Description": {
        "@odata.type": "Microsoft.Dynamics.CRM.Label",
        "LocalizedLabels": [
        {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Parent Sector",
        "LanguageCode": 1033
        }
        ],

        "UserLocalizedLabel": {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Parent Sector",
        "LanguageCode": 1033
        }
        },

        "DisplayName": {
        "@odata.type": "Microsoft.Dynamics.CRM.Label",
        "LocalizedLabels": [
        {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Parent Sector",
        "LanguageCode": 1033
        }
        ],

        "UserLocalizedLabel": {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Parent Sector",
        "LanguageCode": 1033
        }
        },
        "SchemaName": "cak_ParentSectorId",
        "@odata.type": "Microsoft.Dynamics.CRM.LookupAttributeMetadata"
        }
        }
      • Reponse: No content response with 204 Status that indicates the success of the request
      Polymorphic lookup webapi 3

      Polymorphic lookup webapi 4

    • Delete a relationship from an existing Polymorphic Lookup Attribute
      • Method: DELETE
      • API request URL: https://<ORGURL>/api/data/v9.2/RelationshipDefinitions(<RELATIONSHIPGUID>)
      • Request body: Empty body
      • Reponse: No content response with 204 Status that indicates the success of the request
      Polymorphic lookup webapi 5

    • Delete an existing Polymorphic Lookup Attribute
      • Method: DELETE
      • API request URL: https://<ORGURL>/api/data/v9.2/EntityDefinitions(<ENTITYGUID>)/Attributes(<ATTRIBUTEGUID>)
      • Request body: Empty body
      • Reponse: No content response with 204 Status that indicates the success of the request
      Polymorphic lookup webapi 6


Hope This Helps!

Comments

Popular posts from this blog

DYNAMICS 365 HOW TO HIDE RECENT RECORDS FOR LOOKUP FIELD IN UCI

SEARCH BY GUID IN DYNAMICS 365

SAVE FORM IN DYNAMICS 365 JAVASCRIPT