Dynamics 365 – Switch Forms Dynamically by Using JavaScript | formselector

In CRM it is common that showing different forms to different user gropus. There might be some conditions that you check to switch and show different forms to users. For cases like these, formSelector() and navigate() functions save the day.

formselector.items collection: A list of all the forms accessible to the current user. Only those forms that share an association with one of the user’s security roles are available in this collection. It is useful to check if the user has access when switching between forms
formItem = formContext.ui.formSelector.items.get(arg);

  • formselector.getCurrentItem method: Returns the GUID of the form currently being shown. When only one form is available this method will return null.
    formItem = formContext.ui.formSelector.getCurrentItem();

Check the below code for full implementation.

function switchForm(executionContext) {
var mainFormId = "cf74c4d2-2696-4298-9166-e53d79a6bd07";
var alternativeFormId = "cf4fc8f1-08c6-4ae3-9dcf-45a11838a00a";
//Get formContext.
var formContext = executionContext.getFormContext();
//Get all the available forms for the current user.
var listOfAvailableForms = formContext.ui.formSelector.items.get();
//ID of the current form.
var currentFormId = formContext.ui.formSelector.getCurrentItem().getId();
var myAttribute = formContext.getAttribute("myAttribute").getValue();
//Checking if user has access to alternative form and the current form isn't the alternative.
if (myAttribute === 1 && currentFormId !== alternativeFormId) {
listOfAvailableForms.forEach(element => {
if (element.getId() === alternativeFormId) {
element.navigate();
}
});
}
}
view raw SwitchForms.js hosted with ❤ by GitHub

One thought on “Dynamics 365 – Switch Forms Dynamically by Using JavaScript | formselector

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s