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 :
Customer experience | Sales, Customer Insights,...
Unanswered

Message: The given key was not present in the dictionary.Detail:

(0) ShareShare
ReportReport
Posted on by

public void Execute(IServiceProvider serviceProvider)
{

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
trace.Trace("optain the tracking service");
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity target = (Entity)context.InputParameters["target"];
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


try
{
trace.Trace("Inside try");


//Inheritance OBJECT


Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));
trace.Trace("entity: " + entity.Id);
//target does have regardingobjectid proprty?

EntityReference regardingCaseRef = target["regardingobjectid"] as EntityReference;

Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
trace.Trace("incident");
QueryExpression qeRelatedTasks = new QueryExpression("incident");
// ID to find a record
qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef.Id));

EntityCollection allRelatedTasks = service.RetrieveMultiple(qeRelatedTasks);

trace.Trace("bool");
if (AreAllRelatedTasksCompleted(allRelatedTasks))
{
trace.Trace("Case title: " + regardingCase["title"]);
// update case description = all tasks completed
Entity incident = new Entity("incident")
{
Id = regardingCaseRef.Id
};

incident["description"] = "All Task Completed";

service.Update(incident);


}
//var checkComplete = AreAllRelatedTasksCompleted( allRelatedTasks) ;

}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}


}

public bool AreAllRelatedTasksCompleted(EntityCollection relatedTasks)
{


if (relatedTasks.Entities.All(x => ((OptionSetValue)x["cos_csutaskstatus"]).Value == 769190000))
{
//Console.WriteLine("All task completed");
return true;
}
else
{
return false;
}
}

I have the same question (0)
  • Community Member Profile Picture
    on at

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Crm.Sdk;
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    using System.Security.Principal;
    using Microsoft.Xrm.Sdk.Query;

    namespace pluginproject
    {
    public class TaskCheck : IPlugin
    {
    //logic call name regarding:regardingobjectid
    //csu tasok name cos_csutaskstatus
    //if task of a case is completed
    //check all the task of the same case
    // if all other task are completed too update the descriptions field of the case to all task completed

    // Update the Assembly first
    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);
    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    trace.Trace("optain the tracking service");

    try
    {
    trace.Trace("Inside try");
    Entity target = (Entity)context.InputParameters["target"];

    //Inheritance OBJECT
    trace.Trace("regarding");

    EntityReference regardingCaseRef = target["regardingobjectid"] as EntityReference;

    Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
    trace.Trace("incident");
    QueryExpression qeRelatedTasks = new QueryExpression("incident");
    qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef));

    EntityCollection allRelatedTasks = service.RetrieveMultiple(qeRelatedTasks);

    trace.Trace("bool");
    if (AreAllRelatedTasksCompleted(allRelatedTasks))
    {
    trace.Trace("Case title: " + regardingCase["title"]);
    // update case description = all tasks completed
    Entity incident = new Entity("incident")
    {
    Id = regardingCaseRef.Id
    };

    incident["description"] = "All Task Completed";

    service.Update(incident);


    }
    //var checkComplete = AreAllRelatedTasksCompleted( allRelatedTasks) ;

    }
    catch (FaultException ex)
    {
    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
    }


    }

    public bool AreAllRelatedTasksCompleted(EntityCollection relatedTasks)
    {


    if (relatedTasks.Entities.All(x => ((OptionSetValue)x["cos_csutaskstatus"]).Value == 769190000))
    {
    //Console.WriteLine("All task completed");
    return true;
    }
    else
    {
    return false;
    }
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Crm.Sdk;
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    using System.Security.Principal;
    using Microsoft.Xrm.Sdk.Query;

    namespace pluginproject
    {
    public class TaskCheck : IPlugin
    {
    //logic call name regarding:regardingobjectid
    //csu tasok name cos_csutaskstatus
    //if task of a case is completed
    //check all the task of the same case
    // if all other task are completed too update the descriptions field of the case to all task completed

    // Update the Assembly first
    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);
    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    trace.Trace("optain the tracking service");

    try
    {
    trace.Trace("Inside try");
    Entity target = (Entity)context.InputParameters["target"];

    //Inheritance OBJECT
    trace.Trace("regarding");

    EntityReference regardingCaseRef = target["regardingobjectid"] as EntityReference;

    Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
    trace.Trace("incident");
    QueryExpression qeRelatedTasks = new QueryExpression("incident");
    qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef));

    EntityCollection allRelatedTasks = service.RetrieveMultiple(qeRelatedTasks);

    trace.Trace("bool");
    if (AreAllRelatedTasksCompleted(allRelatedTasks))
    {
    trace.Trace("Case title: " + regardingCase["title"]);
    // update case description = all tasks completed
    Entity incident = new Entity("incident")
    {
    Id = regardingCaseRef.Id
    };

    incident["description"] = "All Task Completed";

    service.Update(incident);


    }
    //var checkComplete = AreAllRelatedTasksCompleted( allRelatedTasks) ;

    }
    catch (FaultException ex)
    {
    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
    }


    }

    public bool AreAllRelatedTasksCompleted(EntityCollection relatedTasks)
    {


    if (relatedTasks.Entities.All(x => ((OptionSetValue)x["cos_csutaskstatus"]).Value == 769190000))
    {
    //Console.WriteLine("All task completed");
    return true;
    }
    else
    {
    return false;
    }
    }
    }

    }

    }

  • Community Member Profile Picture
    on at

    public void Execute(IServiceProvider serviceProvider)
    {

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    try
    {
    trace.Trace("Inside try");
    Entity target = (Entity)context.InputParameters["target"];
    //Inheritance OBJECT

    EntityReference regardingCaseRef = target["regardingobjectid"] as EntityReference;
    Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
    QueryExpression qeRelatedTasks = new QueryExpression("incident");

    qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef));

    EntityCollection allRelatedTasks = service.RetrieveMultiple(qeRelatedTasks);
    bool AreAllRelatedTasksCompleted = false;

    trace.Trace("Case title: " + regardingCase["title"]);
    // update case description = all tasks completed
    Entity incident = new Entity("incident")
    {
    Id = regardingCaseRef.Id
    };

    incident["description"] = "All Task Completed";

    service.Update(incident);


    }
    catch (Exception ex)
    {
    throw (ex);
    }


    }


    public bool AreAllRelatedTasksCompleted(EntityCollection relatedTasks)
    {

    if (relatedTasks.Entities.All(x => ((OptionSetValue)x["cos_csutaskstatus"]).Value == 769190000))
    {
    //Console.WriteLine("All task completed");
    return true;
    }
    else
    {
    return false;
    }
    }
    }

  • Community Member Profile Picture
    on at

    need this asap plz

  • Community Member Profile Picture
    on at

    need help asap

  • necsa Profile Picture
    3,455 on at

    Can you add also error message here. But my first advice check your plugin registration tool. Do you have all fields what you need in Update Image?

  • Community Member Profile Picture
    on at

    Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]

    Message: The given key was not present in the dictionary.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">

    <ActivityId>ced23bf9-042c-4983-935d-171ffee666d0</ActivityId>

    <ErrorCode>-2147220956</ErrorCode>

    <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />

    <HelpLink>go.microsoft.com/.../

    <Message>The given key was not present in the dictionary.</Message>

    <Timestamp>2022-08-31T02:15:39.9319507Z</Timestamp>

    <ExceptionRetriable>false</ExceptionRetriable>

    <ExceptionSource>PluginExecution</ExceptionSource>

    <InnerFault i:nil="true" />

    <OriginalException>PluginExecution</OriginalException>

    <TraceText>

    optain the tracking service

    Inside try

    </TraceText>

    </OrganizationServiceFault>

  • Community Member Profile Picture
    on at

    Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]

    Message: The given key was not present in the dictionary.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">

    <ActivityId>fbf7a320-5ddf-4a00-bd3e-4949e88fd111</ActivityId>

    <ErrorCode>-2147220956</ErrorCode>

    <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />

    <HelpLink>go.microsoft.com/.../

    <Message>The given key was not present in the dictionary.</Message>

    <Timestamp>2022-08-31T02:43:40.5518465Z</Timestamp>

    <ExceptionRetriable>false</ExceptionRetriable>

    <ExceptionSource>PluginExecution</ExceptionSource>

    <InnerFault i:nil="true" />

    <OriginalException>PluginExecution</OriginalException>

    <TraceText>

    optain the tracking service

    Inside try

    </TraceText>

    </OrganizationServiceFault>

  • Ray Profile Picture
    1,537 on at

    Try to replace your code 

    if (relatedTasks.Entities.All(x => ((OptionSetValue)x["cos_csutaskstatus"]).Value == 769190000))
    with 
    if (relatedTasks.Entities.All(x=>x.GetAttributeValue("cos_csutaskstatus")?.Value == 769190000)

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Please, don't create duplicated threads.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans