Skip to content

Tag: WebApi

“Get attribute by attribute ID” bookmarklet

Posted in Dynamics 365

Background information

Currently, I have a problem while applying a solution upgrade for our managed solution. The message in the solution history gives me the name of the attribute that blocks the uninstallation and its ID, but it did not tell me to which entity the attribute belongs.

Get attribute by attribute ID

To identify the right entity for the attribute professionally and not by trail and error, I looked into the “Query table definitions using the Web API” article on Microsoft Docs and build a browser bookmarklet for an easier use.

The “Get attribute by attribute ID” bookmarket

Copy and paste the following code as URL of a bookmark in your browser and execute it on any D365 page.

javascript:function getDet(){var r=JSON.parse(this.responseText);console.info("MetaData for Entity: " + r.LogicalName);console.dir(r);console.info("MetaData for Attribute: " + r.Attributes[0].LogicalName);console.dir(r.Attributes[0])};function getAtt(){var r=JSON.parse(this.responseText).value,etn,atn;for (let i=0; i < r.length; i++) {if (r[i].Attributes.length > 0) {Xrm.Navigation.openAlertDialog({confirmButtonLabel:"Close",text:"Entity: "+r[i].LogicalName+"\nAttribute: "+r[i].Attributes[0].LogicalName+"\n-\nOpen Browser Console for more MetaData Details (F12)",title:"Found Attribute"},{height:250,width:350});var oReq=new XMLHttpRequest();oReq.addEventListener("load", getDet);oReq.open("GET","/api/data/v9.0/EntityDefinitions(LogicalName='"+r[i].LogicalName+"')?$expand=Attributes($filter=MetadataId%20eq%20"+id+")");oReq.send();break;}}}id=prompt("Enter Attribute ID", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");var oReq=new XMLHttpRequest();oReq.addEventListener("load", getAtt);oReq.open("GET","api/data/v9.0/EntityDefinitions()?$select=LogicalName&$expand=Attributes($select=LogicalName;$filter=MetadataId%20eq%20"+id+")");oReq.send();

You can paste the attribute ID and receive the logical name of the attribute and of its entity. On top, you can open the browser console to inspect the full output of the WebApi for the attribute.

RetrieveTotalRecordCount Bookmarklet

Posted in Dynamics 365, and Power Platform

Counting records in CRM can be tricky and time consumption when there are more than 5.000 records in the table. There are several ways and tools to do this that are already well described in the community.

When you are fine with the fact that the numbers you receive are static from a count last night, you can use the RetrieveTotalRecordCount function.
There are descriptions in the community on how to use it, too:

I combined both and created a bookmarklet, so that you don’t need to to memorize the url path or modify a bookmark everytime when you are in a different system or need another table (entity).

The RetrieveTotalRecordCount Bookmarklet

Create a normal bookmark in you browser and replace its url with the following code:

javascript:function proc(){var r = JSON.parse(this.responseText).EntityRecordCountCollection,t = [];for (let i = 0; i < r.Keys.length; i++) {t.push(r.Keys[i]+"="+r.Values[i])}prompt("Result", t.join(","))} etn = prompt("EntitySchemaName as CSV?", "account,contact").split(",").map((str) => str.replace(/\s/g,''));var oReq = new XMLHttpRequest();oReq.addEventListener("load", proc);oReq.open("GET","api/data/v9.1/RetrieveTotalRecordCount(EntityNames=['"+etn.join("', '")+"'])");oReq.send();

You can use it at any place inside of D365 CE. You only need to write in a SchemaName of an entity (table) or several, comma separated SchemaNames and it answers you with the EntitySetName and the number of records in it (counted last night).

EntitySetName Bookmarklet

Posted in Dynamics 365, Power Automation, and Power Platform

Almost six years after my last post about bookmarklets, I’m proud to present you a new bookmarklet for Dynamics 365 CE.

It can be used to retrieve the EntitySetName for for an entity (table).

In case you don’t know what the EntitySetName, here is what Microsoft writes about it.

This value is used in the resource path for this entity in the Web API. For custom entities, you can change the name of the entity set used. By default it is the same as the LogicalCollectionName.

Source: Microsoft Docs

In other words, you need when you use the WebApi or the Common Data Service (current environment) connector in PowerAutomate.

The EntitySetName Bookmarklet

Create a normal bookmark in you browser and replace its url with the following code:

javascript: etn=prompt("SchemaName?","account");var xhr=new XMLHttpRequest;xhr.open("GET","/api/data/v9.0/EntityDefinitions(LogicalName='"+etn+"')?$select=EntitySetName",!0),xhr.onload=function(t){4===xhr.readyState&&200===xhr.status&&prompt("EntitySetName:",JSON.parse(xhr.responseText).EntitySetName)},xhr.send();

You can use it at any place inside of D365 CE. You only need to write in the SchemaName of an entity (table) and it answers you with the EntitySetName from the EntityDefinitions.

Get isActivity from Metadata

Posted in Dynamics 365, Power Platform, and Revive

This week I had to differentiate between normal entities and activities in Javascript. I had a look into the SDK and remembered my previous post “Get EntitySetName from Metadata“. A little modification and I get true or false from the ‘isActivity’ information in the metadata.

Here it is fo you: Get isActivity from Metadata

function isActivity(strEntityLogicalName)
{
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" +
        "EntityDefinitions(LogicalName='" + strEntityLogicalName + "')?$select=IsActivity", 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.onreadystatechange = function ()
    {
        if (this.readyState === 4)
        {
            req.onreadystatechange = null;
            if (this.status === 200)
            {
                var result = JSON.parse(this.response);
                var isActivity = result.IsActivity;
            }
        }
    };
    req.send();
}

isActivity

CalculateRollupField with WebApi function in Javascript

Posted in Dynamics 365, Power Platform, and Revive

Microsoft added with Service Pack 1 a new function called “CalculateRollupField” in Dynamics CRM (v 8.1) which enables us to recalculate a rollup field on demand.
I will show you here how you can use it in Javascript with a http request against the WebApi.

The CalculateRollupField function inside the webrequest needs a few parameter to know which rollup field you want to to calulate:

CalculateRollupField in Javascript:

function calcRollupField(strTargetEntitySetName, strTargetRecordId, strTargetFieldName)
{
    strTargetRecordId = strTargetRecordId.replace("{", "").replace("}", "");
    var req = new XMLHttpRequest();
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" +
        "CalculateRollupField(Target=@p1,FieldName=@p2)?" +
        "@p1={'@odata.id':'" + strTargetEntitySetName + "(" + strTargetRecordId + ")'}&" +
        "@p2='" + strTargetFieldName + "'", true);

    req.onreadystatechange = function ()
    {
        if (this.readyState === 4)
        {            
            req.onreadystatechange = null;
            if (this.status === 200)
            {
                var results = JSON.parse(this.response);
            }
            else
            {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };    
    req.send(JSON.stringify({}));
}

The answer of the webservice for the CalculateRollupField function contains the value for the target field, the date of the last calculation and its state.