1

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"

3 Answers 3

1

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));
Sign up to request clarification or add additional context in comments.

2 Comments

There are some major exceptions to that rule. All activities do not follow that convention. Task's id column is activityid, not taskid.
@Polshgiant thanks for pointing that out. I updated my answer and added some sample code.
1

Yeah, I do agree that there is no need to retrieve if it's primary key schema name as for each entity it's entity schema name + id (Lead + id = leadid). But, we felt it's not a good practice. We achieved this with the following code... It's perfectly working fine. When we provide correct Entity Schema Name, it will automatically populate that Primary Id Attribute into another field. new_primarykey - where I am populating the Primary Key Schema Name on entering Entity Schema Name in new_entityschemaname fieldon the form.

function getPrimaryKey() {
    var Oldprimary = Xrm.Page.data.entity.attributes.get("new_primarykey").getValue();
    var req = new XMLHttpRequest();
    var entityName = Xrm.Page.data.entity.attributes.get("new_entityschemaname").getValue();
    var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + "EntityDefinitions?$select=PrimaryIdAttribute&$filter=SchemaName eq '" + entityName + "'";
    req.open("GET", url, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        Xrm.Page.data.entity.attributes.get("new_primarykey").setValue("");
        if (this.readyState === 4) {
           
              
            
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
                var primarykey = results.value[0].PrimaryIdAttribute;
                Xrm.Page.data.entity.attributes.get("new_primarykey").setValue(primarykey);
 
            }
            else {
                Xrm.Utility.alertDialog("Error");
            }
        }
    }
    req.send();
};
 

enter image description here

Comments

0

First of all, thanks for Sharing. I am currently working on a global button on form level which JavaScript that should get the primary key of the specific entity. I also started with entityName + "id", but this will fail on activity entities like email etc. So I started to implement the above.

When you get the entity's logical name via a form in javascript:

var entityName = Xrm.Page.data.entity.getEntityName();

You get for example "opportunity" and when you add this as the var for entityName to the getPrimaryKey this will fail because the SchemaName of the entity is Opportunity and not opportunity. Also for Account etc.

So my advice is when you work with .getEntityName() to use LogicalName instead of SchemaName which results in the following URL:

var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + "EntityDefinitions?$select=PrimaryIdAttribute&$filter=LogicalName eq '" + entityName + "'";

1 Comment

Welcome to Stack Overflow. Stack Overflow is not a discussion forum, it is a Question and Answer site where you can ask a specific programming question that can be answered rather than discussed. We are not sharing here, we are asking and answering. The answer question field should only be used for complete answers to the question. Anything that is not an answer should either be a comment (which you will be able to do once you have earned enough reputation) or a new question altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.