Skip to main content

Notifications

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

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Message: The given key was not present in the dictionary.Detail:

    Please, don't create duplicated threads.

  • Ray Profile Picture
    1,507 on at
    RE: Message: The given key was not present in the dictionary.Detail:

    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)

  • Community Member Profile Picture
    on at
    RE: Message: The given key was not present in the dictionary.Detail:

    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>

  • Community Member Profile Picture
    on at
    RE: Message: The given key was not present in the dictionary.Detail:

    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>

  • necsa Profile Picture
    3,455 on at
    RE: Message: The given key was not present in the dictionary.Detail:

    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
    RE: Message: The given key was not present in the dictionary.Detail:

    need help asap

  • Community Member Profile Picture
    on at
    RE: code error need to check

    need this asap plz

  • Community Member Profile Picture
    on at
    code error need to check

    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
    Message: The given key was not present in the dictionary.Detail:

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

    }

    }

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 671 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans