Wednesday 20 October 2021

D365 Set status and statusreason from custom ribbon button using JavaScript on UCI

 

D365 Set status and statusreason from custom ribbon button using JavaScript on UCI

  1. Javascript webresouce with a function as written below

function ClickCustomButton(primaryControl) {
var formContext = primaryControl;
setStatusStatusReason(formContext, “account”, formContext.data.entity.getId(), 1, 614020000); // these need to be changed as per your context
}

function setStatusStatusReason(executionContext, entityLogicalName, recordId, statecode, statuscode) {

var id = recordId.replace(“{“, “”).replace(“}”, “”);
var data = { “statecode”: statecode, “statuscode”: statuscode };

Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(
function success(result) { executionContext.data.refresh(true); },
function (error) { executionContext.data.refresh(true); }
);
}

2.  Access Scott’s tool Ribbonworkbench plugin from Xrmtoolbox and access Account entity from the solution you intend to apply these changes.

  • Add Ribbon button on account form if you don’t have one
  • Add Command definition with JavaScript Action
  • Select the webresource and function name ‘ClickCustomButton’ from step 1
  • Set Crm parameter -> PrimaryControl

 

Publish! that should do the magic, no more long code as we done in olden days!

To test go to the account form and click the custom button from step 2, upon click it should change the status and status reason of account.

 

Note: If you don’t know how to add ribbon button on ribbon workbench click this link before jumping to this blog. If you face any status reason transitions error, then go to the status reason field customizations and edit the ‘Status Reason Transitions’ or disable it.

Make form read only using JavaScript in D365 CE

 

Make form read only using JavaScript in D365 CEedIn

WhatsAppFacebookEmailTwitter

In this blog, we will learn how to make the entire form read-only using JavaScript in D365 CE(CRM).

Code:

var formCustomizations = {
    disableForm: function (executionContext) {
        let formContext = executionContext.getFormContext();

        let formControls = formContext.ui.controls;

        formControls.forEach(element => {
            if (element.getName() != "" && element.getName() != null) {
                element.setDisabled(true);
            }
        });

    }
}

Monday 18 October 2021

D365 CE: Get Logged in User’s Security Roles using JavaScript

 

D365 CE: Get Logged in User’s Security Roles using JavaScript


Many times we come across requirements such as show/hide ribbon buttons based on logged in user’s security role.

Earlier, we used to get security roles of logged in user at client side using Xrm.Utility.getGlobalContext().userSettings.securityRoles which used to return array of GUID value of each security role.

Now that it’s deprecated, we can use Xrm.Utility.getGlobalContext().userSettings.roles which returns collection of objects with GUID and name of each security role that is assigned to the user directly or through the teams.

Below is the script which checks if the user has certain security roles based on names and hides the ribbon button if the user doesn’t have any of those security roles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SAB.ShowHideReopenButton = function () {
    var roles = Xrm.Utility.getGlobalContext().userSettings.roles;
 
    if (roles === null) return false;
 
    var hasRole = false;
    roles.forEach(function (item) {
        if (item.name.toLowerCase() === "cs manager" || item.name.toLowerCase() === "cs administrator") {
            hasRole = true;
        }
    });
 
    return hasRole;
}

Hope it helps !!

Git Basic working

  Develop = dev   r