Skip to main content

Notifications

Service | Customer Service, Contact Center, Fie...
Suggested answer

How to Create a Conditional Statement for fields that have multiple option sets

Posted on by 30

I know how to create conditional statements but, i have some fields that are multiple option sets. 

In my mind i have this planned: 

If Product family equals this Product, then 6 options in this field are available to pick from
If Product family equals this different Product, then 4 options in same field from above are available to pick from
If Product family equals this different Product, then 2 options in same field from above are available to pick from
If Product family equals this different  Product, then 5 options in same field from above are available to pick from
pastedimage1586396841547v1.png
Right now the only options i have are : 
Flow - Condition
Actions - Recommendation, Unlock/lock, show error message, Set field value, Set Default value, Set business require, Set visibility 
Is this able to be done? If so please help. If i have to make more than one condition then that is fine.
 
  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to Create a Conditional Statement for fields that have multiple option sets

    Hi partner,

    Yes, but you need to replace the field name and option set value  with your owns.

    And then just add the JS code into your instance, I provided the steps in my above reply.

    Regards,

    Leo

  • SeanaLee Profile Picture
    SeanaLee 30 on at
    RE: How to Create a Conditional Statement for fields that have multiple option sets

    i am not familiar with JS coding. 

    Can i copy your code and and just insert my terms and fields?

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to Create a Conditional Statement for fields that have multiple option sets

    Hi partner,

    There is no OOB way to do this, and Business Rule also doesn't support this condition.

    I recommend you to use JS code to enable this custom condition.

    For example, I have two fields on my form:

    Option base (option set)

    Label      Value

    A                1

    B                2

    Multiple condition(multiple option set)

    Label             Value

    Apple                1

    Banana             2

    Pear                  3

    Orange             4

    Now I want when I choose A in Option Base, only "Pear" and "Orange" are available in Multiple condition, when I choose B, only "Apple" and "Banana" available.

    So I created the following code with JS to archive this.

    function filterOptions(executionContext){
        var formContext=executionContext.getFormContext();
        var conditionValue=formContext.getAttribute("new_optionbase").getValue();
        var multipleField=formContext.getControl("new_multiplecondition");
        if(conditionValue!=null){
            //if we select A in conditionField
            if(conditionValue==1){
                multipleField.removeOption(1);
                multipleField.removeOption(2);
                //get current options in multiple options
                var currentOptions=multipleField.getOptions();
                if(!checkExisting(currentOptions,3)&&!checkExisting(currentOptions,4)){
                    multipleField.addOption({value:3,text:"Pear"});
                    multipleField.addOption({value:4,text:"Orange"});
                }
                
            }
            
            //if we select B in conditionField
            if(conditionValue==2){
                multipleField.removeOption(3);
                multipleField.removeOption(4);
                //get current options in multiple options
                var currentOptions=multipleField.getOptions();
            if(!checkExisting(currentOptions,1)&&!checkExisting(currentOptions,2)){
                multipleField.addOption({value:1,text:"Apple"});
                multipleField.addOption({value:2,text:"Banana"});
                }
            }
        }
    
    }
    
    function checkExisting(array, value) {
        var result = false;
        array.forEach(item => {
            if (item.value == value) {
                result = true;
            }
        })
        return result;
    }

    To avoid logical errors caused by repeated choices, I added some logical control codes.

    Add this function both in form onload event and Option Base onchange event.

    pastedimage1586416463425v2.png

    pastedimage1586416477089v3.png

    pastedimage1586416449884v1.png

    Save and publish.

    Let's see the results.

    When open a new creation form, there are 4 options in multiple condition in default.

    pastedimage1586416797707v4.png

    When we select A, then only show Pear and Orange.

    pastedimage1586416846107v5.png

    Select B, only show Apple and Banana.

    pastedimage1586420219351v6.png

    When you save the record and open it again, this rule is also enabled.

    pastedimage1586420386010v7.png

    Hope it helps.

    Best Regards,

    Leo

  • Suggested answer
    Denny Deng Profile Picture
    Denny Deng on at
    RE: How to Create a Conditional Statement for fields that have multiple option sets

    this should be easier to archive by using JS code register in form onload event

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