Switch Forms in Microsoft Dynamics CRM depending on Business Logic

Recently in our project, we had a requirement where the customer wanted to show different forms of the same entity depending on some business logic. Off-course we could design the form with multiple sections with appropriate fields and show/ hide the sections based on business logic.

However if the above approach does not suit you, you have the Xrm Scripts in CRM 2011 and CRM 2013 to enable you to switch forms dynamically. Please check for the pseudo code below. All you need to to set up a default form for the entity. In the default form load check your logic and perform the redirection as required.

var formId= “<Id of the form to navigate>”

if( logic == true )
{
   Xrm.Page.ui.formSelector.items.get(formId).navigate();
}

To get the Id of the form to navigate to, you can write the following code in the onload of the form and copy it and use it in the default form.

var currentFormId= Xrm.Page.ui.formSelector.getCurrentItem().getId( );

Please note that getCurrentItem() method would not work for Microsoft Dynamics CRM for tablets.

Hope this helps!


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

6 thoughts on “Switch Forms in Microsoft Dynamics CRM depending on Business Logic”

  1. I have a grid with rows and is present in two different stages in the Business Process Flow. I want to show the Form 1 if the user opens the record from Stage 1, and Form 2 if is opened from Stage 2. How can I pass the parameters to the Form ? Any ideas ?

    1. Hi,
      Thanks for reading my blog.
      To accomplish the same, you do not need to pass parameters to the form. Please follow the following steps.
      1. Make a default form for the record.
      2. In the onload of the default form check for the stageid value using Xrm.Page.getAttribute(“stageid”).getValue();
      3. Query the “processtage” entity in crm with the stageid you get in the previous point
      4. Get the stagename from the result
      5. Depending on the stagename you can switch the forms easily as described in this blog article.
      Hope this helps!

  2. Thanks for the reply.
    I tried using Xrm.Page.getAttribute(“stageid”).getValue(); on the form load but I am getting null with Xrm.Page.getAttribute(“stageid”). I tried with Parent.Xrm.Page.getAttribute(“stageid”).getValue(); as well but still no luck.

    1. Hey Ravi,
      Thanks for getting back to me. Since you are getting the stageid and processid as null, let me ask you the following questions to understand your system better.
      1. Is this entity upgraded from CRM 2011 and then you applied business process flows for this entity? In this case the records created from crm 2011 would have the stageid and the processid set as blank. you have to update the records using custom code with the appropriate stage and processid as per your business requirements.
      2. Say you move the stage of the record and the record is now from stage1 to stage2. When the record is in stage2 and you query Xrm.Page.getAttribute(“stageid”).getValue() is still returning null?

  3. Hi, this approach will work up to the point when users will be coming back to the entity for second time. Then there is gonna be the latest form on which they were before (it has like a visit history) and “default” one will be skipped.
    Or is there some clever way how to force default form?
    Cheers,
    Jiri

    1. Hi Jiri,
      Thanks for reading my post. This trick mentioned in the post is only when you need to switch forms based complex business logic which you cannot configure OOB.
      In case you want to set the default form for the entity, you can follow the steps mentioned below.
      1. Navigate to Settings->Customizations->Customize the System->Entities-><Your entity >
      2. Expand the entity and select Forms.
      3. Check the form you wish to make default form and click the Enable Security Roles button.
      4. Either select “Display to everyone” radio button or Select the security roles for which you need to apply this setting in the “Assign security role settings” dialog.
      5. Check the Enabled fall back setting in the Fall back setting in the same dialog.
      4. Do “Publish all customizations”.
      The above steps will make your customized entity as the default form in CRM for authenticated security roles you have selected.

Comments are closed.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading