Skip to content

Tag: Javascript

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.

ClickDimensions Quick Send from any entity

Posted in ClickDimensions, Dynamics 365, and Power Platform

Last year I had a customer requirement to start a ClickDimensions Quick Send on a custom entity. By default, QuickSend is only available on lead, contact and opportunity. ClickDimensions has already an article on how creating a Quick Send like dialog to send a single email from any entity, but dialogs are deprecated. Therefore I made a deep dive into how the Quick Send button works and will share my findings with you.

The button

I will only focus on the command, I think button labels and button icons should be no problem for customizers.

Rebuild the command and use your own javascript in yellow marked areas.

The javascript

The following is just to give you a starting point. You need to fill it with additional logic to refer it to a contact or lead.

function openQuickSendForm() 
{
   // [CUSTOMIZE
   var type = 2;
   var typeName = "contact";
   var id = "{332514B1-14F7-E711-A95F-000D3AB6ED20}";
   // CUSTOMIZE]

   var accountKey = getAccountKey();
   var region = getRegion();
   var crmVersion = getCrmVersion();
   var orglcid = parent.Xrm.Page.context.getOrgLcid();
   var orgName = Xrm.Page.context.getOrgUniqueName();
   var lcid = Xrm.Page.context.getUserLcid();
   var userId = Xrm.Page.context.getUserId();

   var params = 
      '&id=' + id + 
      '&orglcid=' + orglcid + 
      '&orgname=' + orgName + 
      '&type=' + type + 
      '&typename=' + typeName + 
      '&userlcid=' + lcid + 
      '&userid=' + userId;
   
   var url = 
      'https://app' + region + '.clickdimensions.com' +
	  '/MSCRM/v' + crmVersion + '/pages/quicksendemailtemplateForm.aspx' 
	  + '?accountKey=' + accountKey + params;
   
   _security.CreateSecuritySession(function (sessionId) 
   {
      url += '&sessionId=' + sessionId;
      window.open(
	     url, 
		 'quicksendWindow', 
		 'height=800px,width=730px,status=0,toolbar=0,resizable=1,scrollbars=1'
      );
   });
}

Filter freemail provider on ClickDimensions forms

Posted in ClickDimensions, Dynamics 365, Power Platform, and Revive

Currently I have the request to filter freemail provider on ClickDimensions forms, because the customer wants only B2B contacts in his database.

ClickDimensions already provides a setting to filter freemail provider and it would be great first step to met the requirement if the editor would save the setting correctly.

At the moment, everytime you reopen the editor the above setting is unchecked and it doesn’t work in the form.

Apart from that, only four freemail providers are offered to filter.
That’s why I took a look into the Javascripts of ClickDimensions and found and extended the original code for it.

The following code will override the original code to add an additional validation. You can add other or even more freemail providers to met your requirements. Just insert it in the CodeEditor of the forms designer.

// enable or disable the filter functionality
var filterFreemail = "true";

// customize the message provided to the user when a freemail address has been entered
var clickd_MSG_FREE_EMAIL = "We accept only business email addresses.";

// customize the array of blocked freemail providers to met your requirements
var blockedFreeEmail = ["hotmail.com", "gmail.com", "yahoo.com", "aol.com"];

// default ClickDimensions function, extended with our own validation
function FormValid()
{
    var isValid = true,
        isAllowed = true,
        reqFieldList = clickd_jquery("input[name='reqField']", "#clickdimensionsForm");

    for (var i = 0; i < reqFieldList.length; i++)
    {
        // default validation
        isValid = ValidField(clickd_jquery(reqFieldList[i]));
        if (!isValid)
        {
            break;
        }

        // custom validation
        isAllowed = AllowedField(clickd_jquery(reqFieldList[i]));
        if (!isAllowed)
        {
            isValid = false;
            break;
        }
    }
    return isValid;
}

// own validation, copied and modified from ClickDimensions original
function AllowedField(hidden)
{
    var fieldType = hidden.attr("alt");
    var fieldID = hidden.val();
    var fieldString = "";

    var field = clickd_jquery("#" + fieldID);
    if (clickd_jquery("#cont_id_" + fieldID).attr("wasskipped") == "1")
    {
        return true;
    }

    var infoId = "required_info_" + fieldID;
    infoId = infoId.replace("f_upload", "f");
    var info = clickd_jquery("#" + infoId);

    var infoText = clickd_jquery(info).text();
    if (infoText != "")
    {
        Un_SelectNotValidInput(info, field);
    }


    if (fieldType.toLowerCase() == "email")
    {
        fieldString = clickd_jquery(field).val();

        if (fieldString.length > 0)
        {
            fieldString = fieldString.replace(/\s+$/gm, '');
        }
    }
    
    if (fieldType.toLowerCase() == "email" && fieldString.length > 0)
    {
        var domain = fieldString.split("@")[1].toLowerCase();

        // set field invalid for freemail provider
        if (filterFreemail.toLowerCase() == "true")            
        {
            for (var i = 0; i < blockedFreeEmail.length; i++)
            {
                if (domain == blockedFreeEmail[i])
                {
                    SelectNotValidInput(info, field, clickd_MSG_FREE_EMAIL);
                    return false;
                }
            }
        }
    }
    return true;
}

Disable a section on profile form on Dynamics 365 portal dynamically

Posted in Dynamics 365, Power Platform, PowerApps Portals, and Revive

At the moment I’m setting up a community portal, where it is planned that the users can register themselves.

The portal users should also be able to enter their company name.
To do this, I use the field “adx_organizationname” provided by the portal because with an account lookup everyone could see our customers. The backoffice then checks whether this contact is related to an existing company or a new one. As soon as the contact is then connected to an account record, the organization name should no longer be changeable by the portal user.

What not worked

  • Javascript on CRM form.
  • Business Rule on CRM form (because it is Javascript).
  • Add the field twice to the form to have one editable and one readonly and hide the the not applicable with jQuery in the portal.
    The same fields two times on the profile form let the portal crash.
  • Make the field readonly on the CRM form and enable it in the portal with jQuery did not save the data back to CRM.

Solution

I have created a separate section for the company-related fields

and make it read only when parentcustomer is not empty.

This creates following tag in the HTML structure.

The result looks like this.

Oh wait, happy times, Dynamics portals has arrived my blog for the first time!