Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Switching Forms based on Option Set

Posted on by Microsoft Employee

We have a custom entity that has multiple forms depending on an optionset value on the form.  

When the page loads for an existing record a "changeForm" function is called last and evaluates the form that is loaded versus the value of the optionset field. If the form type does not correspond to the option set navigation is changed to the correct form.  This works fine for existing records .

For a new record the user selects the value desired for the optionset and like above navigation proceeds to the correct form based on the optionset value chosen.  However, the on-load event for the correct form loads and it calls the same change event but this time the value chosen by the user seems to be lost and is now undefined.  When this happens the code is set to default to a specific form.   This only seems to happen with one optionset type. Is there a way to maintain what the user chose ?

*This post is locked for comments

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: Switching Forms based on Option Set

    When the form on the state "Create", you can't just redirect the value to another form. The easiest way is you need to force save the entity first and then you can redirect it to another form. Or else you need to store the value on the sessionStorage and your code will have to look on that one if exists.

    developer.mozilla.org/.../sessionStorage

    So what I think is on your optionset change:

    if(Xrm.Page.ui.getFormType() === 1){

     sessionStorage.setItem("type", Xrm.Page.getAttribute("new_type").getValue();

    }

    Then on your form on load:

    var type = Xrm.Page.getAttribute("new_type").getValue() || sessionStorage.getItem("type");

    //because you said that your type is empty, then you need to set the attribute if type is not empty

    var typeValue = Xrm.Page.getAttribute("new_type").getValue();

    if(!typeValue && type){

     Xrm.Page.getAttribute("new_type").setValue(type);

     //clear the storage either have value or not have value

     sessionStorage.removeItem("test");

    }

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Switching Forms based on Option Set

    Hi,

    You can keep your code to just called for the existing record by checking form type, and onchange of the radio button you can open specific form directly by passing form id something like below

    var parameters = {};

    parameters["formid"] = "Form GUID";

    Xrm.Utility.openEntityForm(name,id,parameters);

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Switching Forms based on Option Set

    Hi,

    For the new record, the values selected are not actually stored so it is expected that your function will not find the previously selected value. Ideally you shouldn't be running the function on create form i.e. put a check that it doesn't run on create form.

    What do you mean when you say "This only seems to happen with one optionset type.". Is it not working on specif optionset value? If yes then you need to check the value returned and the corresponding form name. If you are not handling that value in your logic then it won't work.

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans