Search code examples
javascriptdynamics-crmdynamics-crm-webapi

Retrieving Entity Metadata using web api


we have a requirement where we have to retrieve an Entity's Metadata. Exact requirement: Im reading a field value on a form which ia having "entity schema name". With that I need to get that Entity's primary key schema name. Is it possible? If so please help me. Eg: in that field if I enter "lead" , that web api should fetch me "leadid" and store it in another field. 2. If I enter "incident" , that web api should get me "incidentid"


Solution

  • You don't need do retrieve entity metadata for that, primary key is always "entity schema name" + "id", for entities other than activities. If you want a generic solution though, you should be able to get this info from metadata:

    https://crmaddress/api/data/v9.1/EntityDefinitions(LogicalName='account')
    

    and simply getting the "PrimaryIdAttribute" of the result, so the example code would be:

    fetch("/api/data/v9.1/EntityDefinitions(LogicalName='account')")
       .then(response => response.json())
       .then(data => console.log(data.PrimaryIdAttribute));