Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Getting Forms - FormSelector Get Parameters & Form Collections

  • 1.  Getting Forms - FormSelector Get Parameters & Form Collections

    GOLD CONTRIBUTOR
    Posted 8 days ago
    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 7 days ago

    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 yesterday
    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

    TOP CONTRIBUTOR
    Posted 16 hours ago
    Edited by Rex Kenley Tan 16 hours ago
    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.
    ------------------------------