Skip to content

Tag: Javascript

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.

Get EntitySetName from Metadata

Posted in Dynamics 365, Power Platform, and Revive

Everybody who has worked with the Web Api, in the system formerly known as Dynamics CRM (honestly, I do not know how to call it now), must have notice that you can’t pass the EntityLogicalName to it. It expects the EntitySetName, which is a kind of plural name for the entity.
Not to be confused with the plural display name you can configure in the entity.

So far I had a little Javascript function that tried to imitade the rules wich are responsible for the generation of the EntitySetName. But since there can be some exceptions, I’m now the opinion that it would be more reliable to query the metadata directly from the system.

Here it is: Get EntitySetName from Metadata

function getEntitSetName(strEntityLogicalName)
{
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" +
        "EntityDefinitions(LogicalName='" + strEntityLogicalName + "')?$select=EntitySetName", 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);
                return result.EntitySetName;
            }
            else
            {
                return null;
            }
        }
    };
    req.send();
}

Export a solution via Javascript

Posted in Dynamics 365, Power Platform, and Revive

Last week I got an error during a solution export, but the error message wasn’t very helpfull. I then had the idea to export the solution via Javascript with the hope to get an more meanfull error.
Fortunately it was so and by the way I’ve learned how to export a solution via Javascript.

//Just to make the selection oh the target version easier
var targetVersions = {
    v_7_0 : "7.0.0.0",
    v_7_1 : "7.1.0.0",
    v_8_0 : "8.0.0.0",
    v_8_1 : "8.1.0.0",
    v_8_2 : "8.2.0.0",
};

function exportSolution(strSolutionName, asManaged, strTargetVersion, objParameter)
{
    var parameters = {
        SolutionName: strSolutionName,
        Managed: asManaged,
        TargetVersion: strTargetVersion,
        ExportAutoNumberingSettings: objParameter.ExportAutoNumberingSettings || false,
        ExportCalendarSettings: objParameter.ExportCalendarSettings || false,
        ExportCustomizationSettings: objParameter.ExportCustomizationSettings || false,
        ExportEmailTrackingSettings: objParameter.ExportEmailTrackingSettings || false,
        ExportGeneralSettings: objParameter.ExportGeneralSettings || false,
        ExportMarketingSettings: objParameter.ExportMarketingSettings || false,
        ExportOutlookSynchronizationSettings: objParameter.ExportOutlookSynchronizationSettings || false,
        ExportRelationshipRoles: objParameter.ExportRelationshipRoles || false,
        ExportIsvConfig: objParameter.ExportIsvConfig || false,
        ExportSales: objParameter.ExportSales || false,
        ExportExternalApplications: objParameter.ExportExternalApplications || false,
    };

	var req = new XMLHttpRequest();
	req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/ExportSolution", true);
	req.setRequestHeader("OData-Version", "4.0");
	req.setRequestHeader("OData-MaxVersion", "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)
			{
                //you will get a download notification
			}
			else
			{
				Xrm.Utility.alertDialog(this.statusText);
			}
		}
	};
	req.send(JSON.stringify(parameters));
}

How to load Javascript from a webresource

Posted in Dynamics 365, Power Platform, and Revive

Imagine an usecase where you can dynamically load a Javascript from the CRM webresources because it doesn’t need to be loaded on every single form load. Perhaps a polyfill to add missing browser functions or a Javascript library like jQuery. Here it is…

function loadJavascriptFromWebresource(strWebresourceName, async)
{
    //sync or async (default)
    if (async == undefined) { var async = true; }

    //build a new webrequest to get the content of the webresource by its name
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/webresourceset?" +
        "$select=content&$filter=name eq '" + strWebresourceName + "'", async);
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("OData-MaxVersion", "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)
            {
                //get the result of the request
                var result = (JSON.parse(this.response)).value[0].content;

                //decode the base64 encoded result
                var script = atob(result);

                //make an indirect eval call to make it globally available
                window.eval(script);
            }
            else
            {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
}

My personal usecase was to add a promises polyfill to the Internet Explorer. So, if you combine this post, my post about “Internet Explorer and promises” and you have a promises polyfill in your CRM webresources, you can use promises and do only load the polyfill in case the browser doesn’t support it.