Notifications
Announcements
No record found.
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
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){
Have fun!
Thank you, I have included this, but naturally the issue is still occurring.
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
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.
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)
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);
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2