web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Useful JS Functions in CRM/Dynamics 365

Spring Wave 2016 Profile Picture Spring Wave 2016 325

Show/Hide Loading in Dynamics 365 (online) Version 9

You can show or hide loading by using a supported way by using namespace Xrm.Utility. Here is code

Xrm.Utility.showProgressIndicator("Loading contact email..");

Xrm.Utility.closeProgressIndicator();

See complete example from this link here

Set a default form by JS

Call this function on OnLoad of form

function switchForm() {
            var item = Xrm.Page.ui.formSelector.getCurrentItem();
            itemLabel = item.getLabel();

            if (itemLabel != "Information") {
                var items = Xrm.Page.ui.formSelector.items.get();
                for (var i in items) {
                    var form = items[i];
                    var formId = form.getId();
                    var formLabel = form.getLabel();
                    //Check condition either on ID or Label from Form
                    if (formLabel == "Information") {
                        form.navigate();
                    }
                }
            }
        }

Check user has a certain role

Call the function “currentUserHasRole” where you need in your JS code

function ExecuteMultipleQuery(webApiQuery, successFunction) {
    var results = null;
    var serverUrl = Xrm.Page.context.getClientUrl();
    // Adjust URL for differences between on premise and online 
    if (serverUrl.match(/\/$/)) {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);
    }
    // Creation of HTTP response header
    var ODataURL = serverUrl + "/api/data/v8.2/" + webApiQuery;
    var req = new XMLHttpRequest();
    req.open("GET", ODataURL, 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.send();

    if (req.readyState === 4) {
        req.onreadystatechange = null;
        if (req.status === 200) {
            results = JSON.parse(req.response);
            if (successFunction != null) {
                successFunction(results);
            }
            else {
                return results;
            }
        }
        else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }

    return results;
}

function getRoleIdByRoleName(roleName) {
    var successFunction = null;
    var webApiQuery = "roles?$select=roleid&$filter=name eq '" + roleName + "'";
    var results = ExecuteMultipleQuery(webApiQuery, successFunction);
    if (results != null && results.value.length > 0) {
        return results.value[0].roleid;
    }
}

function currentUserHasRole(roleName) {
    var roleId = getRoleIdByRoleName(encodeURIComponent(roleName));
    roleId = roleId.replace(/\{|\}/gi, '');
    var roles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < roles.length; i++) {
        roles[i] = roles[i].replace(/\{|\}/gi, '');
        if (roles[i] === roleId) {
            return true;
        }
    }
    return false;
}

Refresh HTML web resource after saving the current record

Call save in your JS code where you need

Xrm.Page.data.save().then(refreshWebResourceAfterSave, errorOnSave);

function refreshWebResourceAfterSave() {
    debugger;
    var webResourceControl = null;
    webResourceControl = Xrm.Page.getControl("WebResource_CustomerValidation");
    if (webResourceControl == null) {
        webResourceControl = Xrm.Page.getControl("WebResource_CustomerValidation1");
    }

    if (webResourceControl != null) {
        var src = webResourceControl.getSrc();
        webResourceControl.setSrc(null);
        webResourceControl.setSrc(src);
    }
}

function errorOnSave() {

}

Validate Date in format YYYY/MM/DD

function isValidDate(dateString) {
            // First check for the pattern
            var regex_date = /^\d{4}\/\d{1,2}\/\d{1,2}$/;

            if (!regex_date.test(dateString)) {
                return false;
            }

            // Parse the date parts to integers
            var parts = dateString.split("/");
            var year = parseInt(parts[0], 10);
            var month = parseInt(parts[1], 10);
            var day = parseInt(parts[2], 10);

            // Check the ranges of month and year
            if (year < 1000 || year > 3000 || month == 0 || month > 12) {
                return false;
            }

            var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

            // Adjust for leap years
            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
                monthLength[1] = 29;
            }

            // Check the range of the day
            return day > 0 && day <= monthLength[month - 1];
        }

References:

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/0ce5ab74-f1a8-40f2-b6c1-ec764f968194/dynamics-crm-2016365-in-entity-grid-javascript-to-show-loading-imagemessage-during-the-execution?forum=crm

https://stackoverflow.com/questions/48658365/show-progress-when-click-button-dynamic-crm-365

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


This was originally posted here.

Comments

*This post is locked for comments