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)

object reference not set to an instance of an object

(0) ShareShare
ReportReport
Posted on by
I am having an issue with the following code. It is mostly working, but when I create a variable (status) to obtain whether the file is active or inactive (looking for statecode == 0), it is throwing an error saying object reference not set to an instance of an object, but my understanding is that the statecode is an <OptionSetValue>.

I have created a custom plugin which should take in an entity, and if field2 is set to false, it should search for all files related to this entity and update the field2 on those files to equal false.
I have debugged the code and the error is occuring on the line where the var "status" is created, but I cannot understand why this isn't working. Can anyone help?

Here is the main part of the code:

try { Entity entity = (Entity)context.InputParameters["Target"]; Guid recordId = entity.Id; if (entity != null) { Entity Account = service.Retrieve("account", recordId, new ColumnSet("field1", "field2")); var x1 = Account.GetAttributeValue<OptionSetValue>("field1").Value; var x2 = Account.GetAttributeValue<bool>("field2"); //var status = Account.GetAttributeValue<OptionSetValue>("statecode").Value; if ((x2 == false) && (x1 == 10)) { // Query on entity reference to check for files with related GUID QueryExpression filesquery = new QueryExpression("incident"); filesquery.ColumnSet = new ColumnSet("field1", "field2"); filesquery.Criteria.AddCondition(new ConditionExpression("field3", ConditionOperator.Equal, recordId)); var filesqueryresults = service.RetrieveMultiple(filesquery); foreach (Entity a in filesqueryresults.Entities) { var status = a.GetAttributeValue<OptionSetValue>("statecode").Value; // issue with statecode being recognised - object ref not set to instnace of an object if ((a.GetAttributeValue<bool>("field2") != false) && (status == 0) ) { a["field2"] = false; service.Update(a); } } } else if((x2 == true) && (x1 != (10))) { //Other logic } } } catch (Exception e) { throw new InvalidPluginExecutionException(e.Message); }

*This post is locked for comments

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

    Hello,

    Main suggestion is following - before you get anything from record check that there is a property to get like:

    if (entity.Contains("field")) {

    //your logic here

    }

    or

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

    if (optionsetValue != null){

    //your logic here

    }

    Have fun!

  • Community Member Profile Picture
    on at

    Thank you, I have included this, but naturally the issue is still occurring.

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

    you didn't include statecode inside the columnset of your retrieve, so it returns null when you do a GetAttributeValue against it, your error is that you try to access the Value property directly without checking if it is null first.

    so you need:

    1) to include statecode inside the columnset

    2) check if it is not null and after get the Value property

  • Community Member Profile Picture
    on at

    Thanks Guido,

    The problem is the statecode is for a different entity - it is for the child file I want to update, so I can't call for it in the initial columnset.

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

    I post your code:

    filesquery.ColumnSet = new ColumnSet("field1", "field2"); // here you are telling you just want field1 and field2

    var status = a.GetAttributeValue<OptionSetValue>("statecode").Value;  // here you are trying to get a property not specified inside the columnset without checking if it's null first

    your variable "a" comes from the retrieve you are doing that put the result inside "filesqueryresults" (and after you do a foreach on the Entities property)

  • Verified answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    Update your column set:

     QueryExpression filesquery = new QueryExpression("incident");

                           filesquery.ColumnSet = new ColumnSet("field1", "field2","statecode");

                           filesquery.Criteria.AddCondition(new ConditionExpression("field3", ConditionOperator.Equal, recordId));

                           var filesqueryresults = service.RetrieveMultiple(filesquery);

  • Community Member Profile Picture
    on at

    Apologies that should have been obvious, many thanks for the help, that has resolved that issue.

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