Blog, Development

Open proper form based on record’s data – UCI way

A cool feature of the 2011 CRM version was the possibility to have multiple forms for the same entity. That feature is awesome but it has one drawback – it’s not possible to create some kind of dependency between the form to use in the context of data. Among developers, there were a few ways to address this scenario.

The first way I knew of was JavaScript that was executed during the form load. This method checked a record’s data and switched the form using formSelector – example from Magnetism Solution. That approach worked, but it was a bit confusing because of form “blink” that was caused with the reload.

The second way I knew is the “Plugin” way – the form to be shown is set in the code behind (the scenes? In the background?). All credit goes to Gonzalo Ruiz, who came up with this wonderful post.  Unfortunately, this way doesn’t work for 2015 and any later version.

So how should one address this requirement nowadays? I recalled this post from my friend and former (hopefully short-term) MVP Artem Grunin. Btw – if you haven’t subscribed to his blog and twitter do it now because he rocks. I decided to take and implement that idea.

Video

Scenario

There are 3 forms configured for the “Account” entity in CE – “Default”, “Customer” and “Partner”. When a user opens an account with “Relationship Type” that is equal to “Customer”, he should be navigated to the “Customer” form. When a user opens an account with “Relationship Type” that is equal to “Partner” he should be navigated to the “Partner” form. In all other situations the “Default” form should be presented.

JavaScript

Let’s start from JavaScript. It’s self explanatory:

Ribbon Changes

Create a solution and add the “Account” entity. Open “Ribbon Workbench” and load your solution. Add the following command to the ribbon:

The Id of the command has to be exactly “Mscrm.OpenRecordItem”. As an action, choose JavaScript and point to the WebResource you created on the previous step, point to the proper function and add “FirstSelectedItemId” as a parameter for the function.

Functionality will not work as expected without button to be added to the ribbon but I don’t want this button to be visible on the ribbon so I add “Enable Rule” that will always return false and button will not be shown. Here is screenshot of “Enable Rule”:

Once command is added – add the button with the same id – exactly “Mscrm.OpenRecordItem” and use command you created on the previous step as a command for this button:

Click the “Publish” button to import changes to the instance. Now you can open CE, navigate to accounts grid and test how it works. If you completed everything the right way you will get everything working fine.

14 Comments

  1. Great Approach, thanks.
    You mention the old javascript blink of web client.
    It didn’t look like the UCI solution suffered from that issue in the video?
    Does the above present a form switcher that doesn’t suffer from the reload blink now?

    1. David,
      The trick is that this approach doesn’t use “FormSwitcher” approach at all so the form doesn’t blink. Script navigates user to the proper form in the first place.
      Andrew

  2. Very nice article Andrew.I really like your ideas.

    I have one question here .Is the supported to override Mscrm.AddNewRecordFromSubGridStandard command also to open new record in modal form.

    1. Bhuvita,
      I haven’t tried that but I believe if you add that command and command to the subgrid of the entity with the same Mscrm.OpenRecordItem id it should override the standard way and you will be able to open the record in the modal form.
      Andrew

  3. Hi Andrew, When I tried with adding the button in either Home or Subgrid, it worked great but as soon as its added for both, it works for one and not for the other. Have you tried adding it for both? My requirement is that I need to open it from the main view as well as from the subgrid. Any help would be greatly appreciated. Cheers, Krutika

    1. Krutika,
      I can confirm that I can replicate your behavior. I would recommend to get in touch with Microsoft and ask for help.
      Andrew

  4. var RN = RN || {};
    RN.ActionRibbon = (function(){

    function openRecord(recordId) {
    //Setting formId to “Default”
    var formId = “0A706BAB-B2B0-EA11-A812-001DD83096F2”; //main form

    //Get the record
    Xrm.WebApi.retrieveRecord(“brm_action”, recordId).then(
    function(result){

    var Id = result.RecordId

    var pageInput = {
    pageType: “entityrecord”,
    entityName: “brm_action”,
    entityId: recordId
    };
    var navigationOptions = {
    target: 2,
    height: { value: 100, unit: “%” },
    width: { value: 50, unit: “%” },
    position: 1
    };

    Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
    function success() {
    // Run code on success
    },
    function error() {
    // Handle errors
    }
    );

    },

    function(error){
    //If anything goes wrong log the error
    console.log(error);

    //and open “by default” form
    Xrm.Navigation.openForm({
    entityName: “brm_action”,
    entityId: recordId,
    formId: formId
    });
    });
    }

    return {
    OpenRecord: openRecord,

    };
    })();

      1. Sorry Andrew, I think some of my message was cut off.

        I was able to modify your directions above to use the navigateTo function instead of OpenForm so that the new modal forms could be used. The button I added was on the subgrid instead of the home page. It worked great!

        However, I did notice that when the modal form window closes, the subgrid doesn’t update until the user manually clicks refresh on the main form ribbon.

        Do you know of or recommend a way to wire a refresh command to avoid the user having to manually refresh the page?

        scenario
        1. Open form
        2. On the form, there is a subgrid
        3. Click a record in the subgrid to open the modal form (this works great using your modified code)
        4. The user makes a change to the record in the modal form, and then closes the form (“e.g. save and close”)

        What is happening:
        a) The user doesn’t see the changes he made reflected in the subgrid until he hits refresh

        What I’m trying to do:
        b) After the dialog closes the form, the subgrid and/or the parent form refreshes so that the update can be seen.

Leave a Reply to Bhuvita Sisodiya Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.