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 365 | Integration, Dataverse...
Suggested Answer

Reading optionset value if it s null return null

(0) ShareShare
ReportReport
Posted on by 595

i'm retrieving optionset value through plugin on create of record i'm calling the plugin...

my problem is i have option set field with optional (not mandatory) but if it is not selected any values... then i'm getting exception... if i selected any option then it is returning value....

is there any suggestion to this, i need to get null if optionset is not selected

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I believe you use code that is similar to following:

    var optionsetValue = (OptionSetValue)entity["optionsetfieldname"];

    in order to resolve your issue try to use following syntax instead:

    var optionsetValue = entity.GetAttributeValue<OptionSetValue>("optionsetfieldname");

    If there will be no value - it will return null.

  • Suggested answer
    Filipe Relvas Profile Picture
    on at

    Hi Abhilash,

    When you create or update a record and trigger a Plugin, the Plugin context usually comes with the attributes that were filled or changed by the user i.e. attributes are seen as dirty.

    Any other attributes will not be part of the context, hence why they come as null, but can be obtained through an Organization Service request using the GUID of the Entity record that triggered it.

    If you do have the necessity to get the Option Value even if it's not mandatory I would suggest one of two options:

    Hope this helps! 

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You  can also check if entity object contains that attribute and then get the value.

    int optionSetValue=-1;

    if(entity.Attributes.Contains("fieldschemaname") && entity["fieldschemaname"]!=null)

    optionSetValue=((OptionSetValue)entity["fieldschemaname"]).Value;

    Now if field is null then it will return -1 else it will return the optionset value selected by the user on UI.

    If found helpful, Please mark my answer verified.

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans