CRM 2011 introduced the feature to design more than one form for an entity and assign form to users based on their role (Role Based Form). It has also included a feature where by it would automatically open the form that was last used by the logged in user for that entity. If you would like the entity to dynamically load a particular form you can use the following piece of code
Xrm.Page.ui.formSelector.items.get(formId).navigate( );
Formid – Guid of the form that you would like to navigate to.
You can add this code on the Load event of the form. But make sure you compare the Current form unique id with the id of the form you wish to navigate to. Failure to do this would result in an infinite loop of form navigation.
Var CurrentFormId = Xrm.Page.ui.formSelector.getCurrentItem( ).getId( );
if( formId!='' && CurrentFormId !='' && formId!=CurrentFormId )
{
Xrm.Page.ui.formSelector.items.get(formId).navigate( );
}
While at this, you can use the following code to get a list of all the forms that the user has access to for the current entity
var items = Xrm.Page.ui.formSelector.items.get( );
for (var i in items)
{
var item = items[i];
// Get the Id of the Form
var itemId = item.getId( );
// Get the Label of the Form
var itemLabel = item.getLabel( );
}
Note: Since this code is written in the Onload event, the form is loaded before the script is executed and the user is navigated to the requested form. It would be a good idea to hide the controls on the form and show them in the Load Event.
Nice One... I really enjoyed read this blog.
ReplyDeleteThanks for Sharing with us
Thanks for sharing with us.
ReplyDeleteBut Xrm.Page.ui.formSelector.getCurrentItem( ).getId( ) returns null for my case.
If you have a chance, can you please check it for me.
Thank you.
It will return null when there are no other forms available to your role AND when the Navigation pane on the current form is not displayed (hidden by unchecking a box at design time).
ReplyDeleteHi,
ReplyDeleteI was using this approach but it stopped working. I am revisiting now and all my code works apart from this element: Xrm.Page.ui.formSelector.items.get(itemId).navigate();
We are using Rollup 12. Is there a different format of "navigate"?
Thanks
The code and function is still valid post UR12. The following code works in UR12 environment
ReplyDeletefunction navigateForm() {
try {
var currentFormId = Xrm.Page.ui.formSelector.getCurrentItem().getId();
var items = Xrm.Page.ui.formSelector.items.get();
//loop through the forms
for (var i in items) {
var item = items[i];
//get the form id
var itemId = item.getId();
if (itemId != currentFormId) {
Xrm.Page.ui.formSelector.items.get(itemId).navigate();
}
}
}
catch (e) {
alert("navigateForm:>>> " + e.description);
}
}
Check if user has access to multiple forms.