Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
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?

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);
}
}
}
}

Categories:
  • vashti hatami Profile Picture
    vashti hatami 40 on at
    RE: 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?

    hi

    you should check "regarding" with "null" :

    if(regarding!=null){

    //your fields update codes

    }

  • Suggested answer
    MehranBre Profile Picture
    MehranBre 730 on at
    RE: 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?

    hi,

    in what line you get this error?

    and also send stacktrace to more help

    Regards

  • Beuz Profile Picture
    Beuz 35 on at
    RE: 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?

    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.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,552 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans