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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

my plugin is working only on profiling ,if triger generally i'm getting an error like this "Object reference not set to an instance of an object" what is the issue can any one help me here?

(0) ShareShare
ReportReport
Posted on by 85

THIS IS MY CODE

MY IS BELOW:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;


namespace ActionStepsExecutionStatusDecisionPlugin
{
public class ActionStepsExecution : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity _target = (Entity)context.InputParameters["Target"];
EntityReference regarding = null;

QueryExpression querytask = new QueryExpression(_target.LogicalName);
querytask.Criteria.AddCondition("activityid", ConditionOperator.Equal, _target.Id);
querytask.ColumnSet = new ColumnSet("regardingobjectid");
EntityCollection task_entity = service.RetrieveMultiple(querytask);
foreach (Entity task_res in task_entity.Entities)
{
regarding = (EntityReference)task_res.Attributes["regardingobjectid"]; //ActionSteps

}

if (regarding.LogicalName == "qbd_actionsteps")
{
string InputField = string.Empty;
Entity action_entity = service.Retrieve(regarding.LogicalName, regarding.Id, new ColumnSet("qbd_actionstepssource"));//Retrive source field schema name
if (action_entity.Contains("qbd_actionstepssource"))
{
InputField = action_entity.GetAttributeValue<string>("qbd_actionstepssource");
}
else
{
throw new InvalidPluginExecutionException("Action Steps Source cannot be null");
}


EntityReference parent_entityref = null;

#region Get Total Task Count
QueryExpression querycount = new QueryExpression(_target.LogicalName);
querycount.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regarding.Id);
querycount.ColumnSet = new ColumnSet("subject");
EntityCollection taskcount = service.RetrieveMultiple(querycount);
int task_count = taskcount.Entities.Count;
#endregion

#region Task Loop - Action Step Status Update
QueryExpression query_as = new QueryExpression(regarding.LogicalName);//ActionSteps
query_as.Criteria.AddCondition("qbd_actionstepsid", ConditionOperator.Equal, regarding.Id);
query_as.ColumnSet = new ColumnSet("qbd_name", "qbd_actionsteptaskstatus", InputField);//qbd_actionstepsidcc
EntityCollection asfetch = service.RetrieveMultiple(query_as);
foreach (Entity actionsteps in asfetch.Entities)
{
parent_entityref = (EntityReference)actionsteps.Attributes[InputField];

#fields update
}
}
#endregion

#region Action Step Loop - Parent Entity Status Update
Entity parent_entity = service.Retrieve(parent_entityref.LogicalName, parent_entityref.Id, new ColumnSet("qbd_name", "qbd_executionstatus"));//Retrive Parent

QueryExpression query_actions = new QueryExpression(regarding.LogicalName);//ActionSteps
query_actions.Criteria.AddCondition(InputField, ConditionOperator.Equal, parent_entityref.Id);
query_actions.ColumnSet = new ColumnSet("qbd_name", "qbd_actionsteptaskstatus", InputField);
EntityCollection actionsfetch = service.RetrieveMultiple(query_actions);
foreach (Entity all_actionsteps in actionsfetch.Entities)
{

#fields update
}
#endregion

}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}

I have the same question (0)
  • Beuz Profile Picture
    35 on at

    Hi,

    you don't test "parent_entityref" after retrieve it, when i read your code it seems that it is the only test you forgot.

    "Object reference not set to an instance of an object" occurs when you try to apply a method on null object.

  • Suggested answer
    MehranBre Profile Picture
    732 on at

    hi,

    in what line you get this error?

    and also send stacktrace to more help

    Regards

  • vashti hatami Profile Picture
    40 on at

    hi

    you should check "regarding" with "null" :

    if(regarding!=null){

    //your fields update codes

    }

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 88 Super User 2026 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 40 Most Valuable Professional

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 35 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans