web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Check MC Option Set for value, setVisible if value is selected

(0) ShareShare
ReportReport
Posted on by

Hi,

I'm trying to achieve the following:

I've got a field on the account form called "scp_relationshiptype_mc", which is a multi-select option set with several values.

Depending on which value is selected, another field will be shown.

Eg.: If someone picks options "A", "B", and "C" in the MC option set field, three otherwise hidden fields "scp_A", scp_B" and "scp_C" will become visible. If they pick "A" and "B", only "scp_A" en "scp_B" ought to be shown. 

It is disappointing that I can use MC option sets, but I can't do anything with them in a business rule or a workflow, so I've resorted to using JavaScript. I'm not a developer, so pardon any rookie mistakes.

I've gotten this far:

function onLoad() {
ShowHideRT();
}

function ShowHideRT() {

if ("scp_relationshiptype_mc" !== null)
{
//EV
if (Xrm.Page.getAttribute("scp_relationshiptype_mc").getValue().includes(798200000))
{
Xrm.Page.getControl("scp_rt_specifyev").setVisible(true);
Xrm.Page.getControl("scp_relationshiptypecomment").setVisible(true);
}
//TechPartner
else if (Xrm.Page.getAttribute("scp_relationshiptype_mc").getValue().includes(798200001))
{
Xrm.Page.getControl("scp_rt_specifytechpartner").setVisible(true);
Xrm.Page.getControl("scp_relationshiptypecomment").setVisible(true);
}
//GridOp
else if (Xrm.Page.getAttribute("scp_relationshiptype_mc").getValue().includes(798200002))
{
Xrm.Page.getControl("scp_rt_specifygridoperator").setVisible(true);
Xrm.Page.getControl("scp_relationshiptypecomment").setVisible(true);
}
//ServComp
else if (Xrm.Page.getAttribute("scp_relationshiptype_mc").getValue().includes(798200003))
{
Xrm.Page.getControl("scp_rt_specifyservicecompany").setVisible(true);
Xrm.Page.getControl("scp_relationshiptypecomment").setVisible(true);
}
//BuildConstr
else if (Xrm.Page.getAttribute("scp_relationshiptype_mc").getValue().includes(798200004))
{
Xrm.Page.getControl("scp_rt_specifybuildingconstruction").setVisible(true);
Xrm.Page.getControl("scp_relationshiptypecomment").setVisible(true);
}
//NonTrad
else if (Xrm.Page.getAttribute("scp_relationshiptype_mc").getValue().includes(798200005))
{
Xrm.Page.getControl("scp_specifynontraditional").setVisible(true);
Xrm.Page.getControl("scp_relationshiptypecomment").setVisible(true);
}
//Other...

}
}

This JS is called in the onLoad of the Account page and the onChange of the "scp_relationshiptype_mc" field. 

When I try to open the account page, I get the error:

TypeError: Cannot read property 'includes' of null at ShowHideR

I had hoped that .include method would help me retrieve the necessary values to check on.

Help is much appreciated!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Dmitri Profile Picture
    34 on at

    It says you cannot call 'includes' method, if a value of the field is null. So, first of all, check if the Xrm.Page.getAttribute("scp_relationshiptype_mc").getValue() is not null.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi , i would suggest you to compare your code with MSDN blog : msdn.microsoft.com/.../gg594433(v=crm.7).aspx

  • Verified answer
    tw0sh3ds Profile Picture
    5,600 on at

    So first of all, you should not use Xrm.Page anymore, you should use formContext:

    [View:https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/executioncontext/getformcontext:750:50]

    So just pass execution context to your onload function and your ShowHideR function and get form context and you can simply replace all Xrm.Page with formContext.

    Now - the error clearily states that you multiselect does not have any value selected. You should do something like

    var multiValueArray = formContext.getAttribute("scp_relationshiptype_mc").getValue();
    
    if(multiValueArray == null){
       //no value is selected
       return;
    }
    
    if(multiValueArray.includes(xxxx)){
    }
    
    etc.


    Just be aware that "includes" version is available only in modern browsers and not all of them.

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    keep in mind that "includes" (when used on arrays that is the value returned by the multiselect optionsets) is not officially supported on Internet Explorer or Edge, you should use "indexOf" instead.

  • Community Member Profile Picture
    on at

    This is what I needed, thank you!

    I've assembled most of the code through different other approaches I found in the community.

    Just one quick question, when I use .includes, I have to refer to the optionset value and not the optionset text, right?

    Thanks again!

    @all the others, also a big thank you for helping out. Much appreciated!

  • tw0sh3ds Profile Picture
    5,600 on at

    yes, you should refer to option set value

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans