Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Check multiselect optionset value using query expression

Posted on by 1,000

Hi Techies,

I have one entity with multiselect optionset field and I want to check whether the options are already saved or not while saving new record.If already present then we need to throw the duplicate record warning.

for ex: I already saved one record with a,b,c options in multiselectoptionset. Now I try to save another record with a,b,d , the system should allow me to save the record. Because both options are not exact match.

If I use the below code its not working.If we use In condition it will not suitable for the below scenario.

Lets say If I try to save a,b options in multiselectoptionset It will check with existing record (a,b,c option record). The new value a,b already present in the existing record so it will return the record. But I need to check the exact options should match.

If I try to save a,b,c once again I want to throw the duplicate match  error. But If I try a,b,d or a,b or a or b the system should allow to be consider as a non duplicate.

How can I retrieve the exact match options through queryexpression ? TIA.

Entity entity = (Entity)context.InputParameters["Target"];
if (entity.Attributes.Contains("abc_multiselectfieldname"))
{
	OptionSetValueCollection collection = (OptionSetValueCollection)entity.Attributes["abc_multiselectfieldname"];                           
	QueryExp.Criteria.AddCondition("abc_multiselectfieldname", ConditionOperator.IN, collection);
}


  • Suggested answer
    vijay12872 Profile Picture
    vijay12872 1,000 on at
    RE: Check multiselect optionset value using query expression

    I'm using the following code logic to get the result,

    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.Attributes.Contains("abc_multiselectfieldname"))

    {

    OptionSetValueCollection collection = (OptionSetValueCollection)entity.Attributes["abc_multiselectfieldname"];

    int[] arrVal = new int[] {};

    foreach (var options in contigentLifeCollection)

    {

    arrVal = arrVal.Concat(new int[] { options.Value }).ToArray();

    }

    QueryExp.Criteria.AddCondition(new ConditionExpression("abc_multiselectfieldname", ConditionOperator.In, arrVal));

    }

  • Ray Profile Picture
    Ray 1,505 on at
    RE: Check multiselect optionset value using query expression

    I tried both QueryExpression and FetchXml but no lucky. I test in advance find when I select condition operator to "equal" for multiple option field, the fetchXml result will use to operator "in" to do the query. So I think you can use "in" to query first and then do an extra filter for result.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Check multiselect optionset value using query expression

    Hi,

    Can you please apply filter in advance find then download the fetchXml.

    Now use fetchXml builder tool in xrmtoolbox to convert to query expression to see the syntax.

    I think you need to use contains value operator and pass string value

  • vijay12872 Profile Picture
    vijay12872 1,000 on at
    RE: Check multiselect optionset value using query expression

     R@y If I changed the condition operator to Equal I'm getting the following error,

    Element 'schemas.microsoft.com/.../Arrays:anyType' contains data from a type that maps to the name

    'Microsoft.Xrm.Sdk:OptionSetValueCollection'. The deserializer has no knowledge of any type that maps to this name.

    Consider changing the implementation of the ResolveName method on your

    DataContractResolver to return a non-null value for name 'OptionSetValueCollection' and namespace 'Microsoft.Xrm.Sdk'.

  • Ray Profile Picture
    Ray 1,505 on at
    RE: Check multiselect optionset value using query expression

    You should use ConditionOperator.Equal rather then ConditionOperator.In.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Check multiselect optionset value using query expression

    Hi user12872,

    Perhaps you could start by getting the number of items selected for each multi-select option set field,then compare them separately.

  • vijay12872 Profile Picture
    vijay12872 1,000 on at
    RE: Check multiselect optionset value using query expression

    Amit Yadav4 Still No..

  • Amit Yadav4 Profile Picture
    Amit Yadav4 70 on at
    RE: Check multiselect optionset value using query expression

    Hi,

    Did you able to resolve this?

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans