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

4 responses to “Dynamics 365 – Switch Forms Dynamically by Using JavaScript | formselector

  1. […] A list of all the forms accessible to the current user. Only those forms … Continue reading Dynamics 365 – Switch Forms Dynamically by Using JavaScript | formselector Read Complete Post and Comments SBX – Two Col […]

  2. THIRU VENKAT N avatar
    THIRU VENKAT N

    giving error that navigate is not a function, any fix for it?

    1. Furkan Karacan avatar

      Check if your listOfAvailableForms contains any value and validate that you defined your formContenxt correctly. formContext.ui.formSelector.items.get();

  3. vinodprasanth avatar
    vinodprasanth

    Hi, Thanks for this. I have different requirements. When the user open an existing record, switch the form based on owning Business unit and when the user create a new form, switch the form based users current business unit? Can you please let me know? thanks

Leave a comment