Tags

, , ,

Hello Everyone,

CRM 2011 introduced the feature to design more than one form for an Entity and assign form to the users based on their security role.

I had a requirement wherein I was suppose to create Multiple Forms for an Entity and display the records saved in their respective forms.

Creating Multiple Forms for an Entity was not a big task, but opening the records saved in their respective forms was a challenge.  As we are familiar that:

  • A user can select between multiple forms using the drop-down menu based on their criteria while saving a record. But, Microsoft CRM automatically remembers which form the user last selected and shows them the same form next time they open the record, irrespective of the record saved in its particular form. 

I found this helpful article in MSDN for FormSelector and its Item Methods. We can use them in JScript to overcome the by default behavior of Form Navigation.

  • So to start with, I first created 2 extra Forms named “Ashwani” and “Ashwin” along with the “Information” and a Option-Set field(formtype) on the Form which contains the Form Names. I made Option-Set field as read-only so that it cannot be changed accidentally anytime while updating a record.

 

  • Then the first task was to fetch the current Form-Label which can be done by:
var item = Xrm.Page.ui.formSelector.getCurrentItem();
itemLabel = item.getLabel();
  • And update the Option-Set Field-Value to the same while the from is under CReate Mode.
var formType= Xrm.Page.ui.getFormType();
if(formType == 1)
{
if (itemLabel == "Information")
{
Xrm.Page.getAttribute('new_formtype').setValue(1) ;
}
if (itemLabel == "Ashwani")
{
Xrm.Page.getAttribute('new_formtype').setValue(2) ;
}
if (itemLabel == "Ashwin")
{
Xrm.Page.getAttribute('new_formtype').setValue(3) ;
}
}
  • Now, If the Form is in Update mode then we well Change the Form-Type as per the Option-Set field value saved in the record. 
if (Xrm.Page.getAttribute('new_formtype').getText() == "Information" )
{
 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();
if (formLabel == "Information") //Check condition either on ID or Label from Form
{ 
 form.navigate();
 } 
 }
 } 
}
  • Finally my whole Function will be: 
function pagetype() {
var item = Xrm.Page.ui.formSelector.getCurrentItem();
itemLabel = item.getLabel();
//alert(itemLabel);
var formType= Xrm.Page.ui.getFormType();
if(formType == 1)
{
if (itemLabel == "Information")
{
Xrm.Page.getAttribute('new_formtype').setValue(1) ;
}
if (itemLabel == "Ashwani")
{
Xrm.Page.getAttribute('new_formtype').setValue(2) ;
}
if (itemLabel == "Ashwin")
{
Xrm.Page.getAttribute('new_formtype').setValue(3) ;
}
}
else{
if (Xrm.Page.getAttribute('new_formtype').getText() == "Information" )
{
 if (itemLabel != "Information")
 {
 //load Information form
 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")
{ 
 //alert(formId );
 //alert(formLabel);
 form.navigate();
 } 
 }
 } 
}
else if (Xrm.Page.getAttribute('new_formtype').getText() == "Ashwani" )
{
 if (itemLabel != "Ashwani")
 {
 //load Ashwani form
 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 == "Ashwani")
{ 
 //alert(formId );
 //alert(formLabel);
 form.navigate();
 } 
 }
 } 
}
else if (Xrm.Page.getAttribute('new_formtype').getText() == "Ashwin" )
{
   if (itemLabel != "Ashwin")
   {
  //load Ashwin form
 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 == "Ashwin")
{ 
 //alert(formId );
 //alert(formLabel);
 form.navigate();
 } 
 }
   }       
}
}
}
  • I will call my above function on Page-Load and it will change the Form as per the Option-Set value in the record.

This will result the page under ribbon to blink because first(by default behavior) the CRM will open the Form last viewed by the user and then the above JScript function on page load will change the Form type accordingly.

Hence this resolution to change the Form type dynamically worked perfectly as expected.

Hope that was Helpful. 

Thanks !!!

😀