Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Unanswered

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

Posted on by Microsoft Employee

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
    a33ik 84,313 Super User on at
    RE: Message: The given key was not present in the dictionary.Detail:

    Please, don't create duplicated threads.

  • Ray Profile Picture
    Ray 1,432 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
    Community Member Microsoft Employee 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
    Community Member Microsoft Employee 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
    necsa 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
    Community Member Microsoft Employee on at
    RE: Message: The given key was not present in the dictionary.Detail:

    need help asap

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

    need this asap plz

  • Community Member Profile Picture
    Community Member Microsoft Employee 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
    Community Member Microsoft Employee 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;
    }
    }
    }

    }

    }

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans