Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Getting Forms - FormSelector Get Parameters & Form Collections

Jump to Best Answer
  • 1.  Getting Forms - FormSelector Get Parameters & Form Collections

    GOLD CONTRIBUTOR
    Posted Jan 07, 2019 01:24 PM
    How does one get each of the forms available to a user via javascript?

    I'm trying to configure our system to change forms based on a project type field value. The following code correctly gets the current form, and recognizes that the user can see multiple forms, but I am unable to get these other forms by their name (String).

    function selectFormType(executionContext) {
    
        //Determine Form currently loaded. THIS WORKS.
        var selectedForm = formContext.ui.formSelector.getCurrentItem();
        console.log("SelectedForm: " + selectedForm.getLabel());
    
        //Testing: Log the count of forms available to user in the form collection. THIS WORKS.
        console.log("Found " + formContext.ui.formSelector.items.getLength() + " forms");
    
        //Get references to each of the target forms. GET METHOD DOESN'T WORK WITH STRINGS NAMES OF FORMS.
        var targetResidentialForm = formContext.ui.formSelector.items.get("Residential Lead Form");
        if (targetResidentialForm) {
            console.log("Found Form: " + targetResidentialForm.getLabel());
        }
        else {
            console.log("Could Not Find Residential Lead Form");
        }
    //... Remaining code omitted from example. 
    }


    MSFT Documentation for FormSelector: formContext.ui.FormSelector (Client API reference) in Dynamics 365 for Customer Engagement apps

    MSFT Documentation for GET function of collections: get method for collections (Client API reference) in Customer Engagement


    Can someone who has used JS to switch forms and worked through this explain how to get the other forms? 



    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Energy
    ------------------------------


  • 2.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    TOP CONTRIBUTOR
    Posted Jan 08, 2019 03:29 PM

    Ryan

    I don't think there is a native way to do that. So you have two options

    1.) Create your own web api call to return the necessary guids
    2.) Create a Map object that would store your form name and guids

    I prefer to use the map object since this information is pretty much static.

    Cheers!



    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 3.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    GOLD CONTRIBUTOR
    Posted Jan 14, 2019 01:39 PM
    Hi Rex / any other experienced developers,

    I hard coded the guids and am able to to get the forms to switch... however,  this now causes a race condition. Any tips on how to prevent this?  Code sample is below.  Note the comments on the last few lines that call the navigate method. This function is called on page load to verify if the user is using the correct form.  When the navigate method is called, the new form is refreshed faster than the save operation completes. The new form then runs the same function, seeing the old value, and causing it to navigate back to the original.   I tried adding an explicit save operation before the navigate function, but this causes the system to prompt the user to save unsaved changes... which doesn't make much sense to me, given that I am calling a save function.  Ideas? 
    function selectFormType(executionContext) {
        //Get project type field value.
        var formContext = executionContext.getFormContext();
        var projectType = formContext.getAttribute("new_type").getText();
        console.log("projectType: " + projectType);
    
        //Determine Form currently loaded:
        var selectedFormLabel = formContext.ui.formSelector.getCurrentItem().getLabel();
        console.log("SelectedForm: " + selectedFormLabel);
    
        //If the selected form already matches project type, just stop.
        if (
            ((projectType === "Primary Residence" || projectType === "Secondary Residence") && selectedFormLabel === "Residential Lead Form") ||
            ((projectType === "Commercial") && selectedFormLabel === "Commercial Lead Form")
        ) {
            console.log("Correct form loaded.");
            return;
        }
    
        //Set targetFormGuid
        var targetFormGuid = "";
        switch(projectType) {
            case "Primary Residence":
            case "Secondary Residence":
            targetFormGuid = "e4aab8a0-d5b9-458b-a713-23bf1c6ab3f6";
            break;
            case "Commercial":
            targetFormGuid = "d97f43e6-ad00-4beb-b084-5f2d9efa5816";
            break;
        }
        console.log("targetFormGuid Set: " + targetFormGuid);
        //Get Target Form
        targetForm = formContext.ui.formSelector.items.get(targetFormGuid);
    
        //Navigate to targetForm if found
        if (targetForm) {
            console.log("Saving Changes");
            formContext.data.entity.save(); //Attempt to prevent race condition, but prompts user to save changes. 
            console.log("Navigating to target form."); 
            targetForm.navigate() // Causes race condition. Record save and form change triggered simultaneously. New form doesn't see the updated value, and this same function causes it to switch back to the original form. 
        }}


    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------



  • 4.  RE: Getting Forms - FormSelector Get Parameters & Form Collections
    Best Answer

    TOP CONTRIBUTOR
    Posted Jan 15, 2019 07:00 AM
    Edited by Rex Kenley Tan Jan 17, 2019 09:15 AM
    Ryan

    Use "then" promise.

    save (Client API reference) in Dynamics 365 for Customer Engagement

    if (targetForm) {
    console.log("Saving Changes");
    formContext.data.save().then(() => {
    console.log("Navigating to target form.");
    targetForm.navigate()
    })
    }​


    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 5.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    GOLD CONTRIBUTOR
    Posted Jan 17, 2019 09:21 AM
    Thanks, Rex!

    This, combined with removing a separate function also called on the same fieldchange event fixed things.  Apparently the eventhandlers don't always run in the order listed on the form's field event list.

    Your help was very much appreciated! ​ MSFT is looking into allowing the GET function to accept a string in addition to guids.

    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------



  • 6.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    Posted Jan 18, 2019 04:56 AM
    ​Hi Ryan

    I've looked into this in the past on the case forms - trying to switch case forms dependent on case types. We found that the UX was not very good due to the resulting load time.

    We took an approach of having one form with multiple hidden tabs on the form with javascript to show the relevant tabs based on case type.

    Regards

    Jamie


    ------------------------------
    Jamie Hirst
    Functional Specialist
    AQA Education Limited
    Manchester
    ------------------------------



  • 7.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    GOLD CONTRIBUTOR
    Posted Jan 18, 2019 09:45 AM
    Thanks for the input, Jamie,

    Many of our current forms show/hide tabs using JS as you suggest. In my opinion also, this is probably the most flexible approach.  The testing of switching to an entirely different form so far has been proof-of-concept to see if the UX would pan out, given potentially longer load times. (IE we weren't sure if it would be faster to load two smaller forms than one large form with hidden tabs). I will bring your feedback up with our decision makers as another professional who has gone down this road and found the load times wanting.   Do you know, is it any better if the first loaded form contains only enough info to determine which form to load? This was my next item to test when I get back to this project.


    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------



  • 8.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    Posted Jan 20, 2019 08:33 AM
    Hi Ryan

    Glad you found it useful.

    As i understand it, hidden/collapsed tabs do not load their content until they display, so having a larger form may not have as much impact on load times as you may think. Also, as the system remembers the last form a user accessed for the entity, your suggestion of having an initial form as a landing page would only work when a user accesses that record type for the first time.

    However, there may be some mileage if on every from all tabs are hidden, with the script showing the tabs only once its confirmed the right form is being displayed.

    Hope that helps

    Jamie


    ------------------------------
    Jamie Hirst
    Functional Specialist

    ------------------------------



  • 9.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    Posted Jan 20, 2019 03:07 PM
    There is a more left-field way of doing it (assuming this is still an option).

    You can dynamically set the users "Last Viewed Form" using a plugin based on the data you require. That way you avoid the clunky form refresh OnLoad.

    Avoiding form reload when switching CRM forms based on a field
    Dynamics remove preview
    Avoiding form reload when switching CRM forms based on a field
    Quite often we define different forms for a given entity and we do a form switch based on a field rather...
    View this on Dynamics >



    ------------------------------
    Ben Bartle
    IBM
    ------------------------------



  • 10.  RE: Getting Forms - FormSelector Get Parameters & Form Collections

    GOLD CONTRIBUTOR
    Posted Jan 21, 2019 10:33 AM
    Thank you Ben,

    These articles were right up the alley and are much appreciated.  To date, we have a form with half a dozen tabs that are set visible as required.

    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role