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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

GetAttributeValue returns null

(0) ShareShare
ReportReport
Posted on by

Hello,

In a c# plug-in, i try to get the value of a field with logical name "asip_categorie", type Picklist (OptionSet).

Data is filled in asip_categorie.

The method GetAttributeValue returns null and i get an exception. Why?

Code :

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];

var Categorie = entity.GetAttributeValue<OptionSetValue>("asip_categorie");

...

*This post is locked for comments

I have the same question (0)
  • Abhishek Gupta Profile Picture
    2,003 on at
    RE: GetAttributeValue returns null

    HI,

    try this

    int optionsetvalue= entityname.GetAttributeValue<OptionSetValue>("fieldname").Value;


    Please mark my answer as correct if you find so.

    Regards,
    Abhishek

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: GetAttributeValue returns null

    you need to use Value as suggested because you want to get value of the option set field and in case interested for other types please refer our blog: mahenderpal.wordpress.com/.../retrieve-ms-crm-datatype-fields-using-late-bound

  • Suggested answer
    Guido Preite Profile Picture
    54,084 Moderator on at
    RE: GetAttributeValue returns null

    you get null because

    your plugin is on update, that optionset is not inside the updated attributes so it return null

    you should use a preimage in order to get the value

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at
    RE: GetAttributeValue returns null

    Hi,

    If you're getting a null reference exception, drilling more into the null value won't help. You should always be checking for null before accessing properties:

    if (entity.Attributes.ContainsKey("asip_categorie"))

    {

    // Once you verify the property exists, then you can get into it further. I like to get it from the attibute collection

      OptionSetValue value = (OptionSetValue)entity.Attributes["asip_categorie"];

    }

    If this was helpful, I'd appreciate if you would mark this as the verified answer.

    Thanks Paul,

     Aiden

  • Community Member Profile Picture
    on at
    RE: GetAttributeValue returns null

    Hi,

    I tried to change the line with :

    int Categorie = entity.GetAttributeValue<OptionSetValue>("asip_categorie").Value;

    And i get the fallowing exception :

    Unexpected exception from plug-in (Execute): [...]: System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.

  • Community Member Profile Picture
    on at
    RE: GetAttributeValue returns null

    I changed the code as recommended :

               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

               {

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

                   if (entity.Attributes.ContainsKey("asip_categorie"))

                   {

                       // Once you verify the property exists, then you can get into it further. I like to get it from the attibute collection

                       OptionSetValue Categorie =(OptionSetValue)entity.Attributes["asip_categorie"];

                   } else

                   {

                       throw new InvalidPluginExecutionException("Key asip_categorie missing.");

                   }

    When i execute the plugin by creating a new entity, i get the exception : "Key asip_categorie missing". It means that ContainsKey method return false with "asip_categorie". However, the field "asip_categorie" exists in the entity.

  • Community Member Profile Picture
    on at
    RE: GetAttributeValue returns null

    And "asip_categorie" has a value filled.

  • Community Member Profile Picture
    on at
    RE: GetAttributeValue returns null

    My plugin is on Create.

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at
    RE: GetAttributeValue returns null

    Hi Paul,

    You need to follow Guido's suggestion about ensuring the image is passed in to the plugin, and my suggestion to check for null. You need to get it from the image so you can access the value if its set, and check for null in case its not set (because it's not always going to be).

    Thanks,

     Aiden

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at
    RE: GetAttributeValue returns null

    Get the entity from context.PreEntityImages or context.PostEntityImages

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans