Answered by:
"The Given Key Was Not Present In The Dictionary" Error Message

Question
-
My codes (pasted below) shows the following error message:
"System.Collection.Generic.KeyNotFoundException: The given key was not present in the dictionary."
Please could someone assist me in debugging these codes? I could not figure out where the error lies.
Thank you.
Longinus.
public class AppointmentStartDateUpdateNextActivityDate : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
ITracingService tracingService = context.GetExtension<ITracingService>();
IWorkflowContext wfcontext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory fact = context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService _service = fact.CreateOrganizationService(wfcontext.UserId);
ColumnSet attributes = new ColumnSet(true);
Entity apptmtRecord = _service.Retrieve("appointment", wfcontext.PrimaryEntityId, attributes);
Guid appointmentId = apptmtRecord.Id;
Entity acctRecord = new Entity("account");
Guid acctId = (Guid)acctRecord.Attributes["accountId"];
DateTime acctDate = new DateTime();
acctDate = (DateTime)acctRecord.Attributes["new_nextactivitydate"];
//int taskDate = (int)taskRecord.Attributes["actualstart"];
DateTime apptmtDate = new DateTime();
apptmtDate = (DateTime)apptmtRecord.Attributes["scheduledstart"];
if ((ScheduledStartHasValue(_service, wfcontext)) && (NextActivityDateIsEmpty(_service, wfcontext)))
{
acctDate = apptmtDate;
}
else if ((ScheduledStartIsFuture(_service, wfcontext)) && (ScheduledStartLessThanNextActivityDate(_service, wfcontext)))
{
acctDate = apptmtDate;
}
else if (NextActivityDateIsPast(_service, wfcontext))
{
tracingService.Trace("This date is in the past, please reset your Next Activity Date.");
}
else
{
acctDate = apptmtDate;
}
_service.Update(acctRecord);
}
public bool NextActivityDateIsEmpty(IOrganizationService _service, IWorkflowContext wfcontext)
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "account";
qe.ColumnSet = new ColumnSet("true");
qe.Criteria = new FilterExpression();
qe.Criteria.AddCondition("new_nextactivitydate", ConditionOperator.LessThan, 1);
return true;
}
public bool ScheduledStartHasValue(IOrganizationService _service, IWorkflowContext wfcontext)
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "appointment";
qe.ColumnSet = new ColumnSet(true);
qe.Criteria = new FilterExpression();
qe.Criteria.AddCondition("scheduledstart", ConditionOperator.GreaterThan, 1);
return true;
}
public bool ScheduledStartIsFuture(IOrganizationService _service, IWorkflowContext wfcontext)
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "appointment";
qe.ColumnSet = new ColumnSet(true);
qe.Criteria = new FilterExpression();
qe.Criteria.AddCondition("scheduledstart", ConditionOperator.GreaterThan, DateTime.Now);
return true;
}
public bool ScheduledStartLessThanNextActivityDate(IOrganizationService _service, IWorkflowContext wfcontext)
{
ColumnSet attributes = new ColumnSet(true);
Entity acctRecord = _service.Retrieve("account", wfcontext.PrimaryEntityId, attributes);
QueryExpression qe = new QueryExpression();
qe.EntityName = "appointment";
qe.ColumnSet = new ColumnSet(true);
qe.Criteria = new FilterExpression();
qe.Criteria.AddCondition("scheduledstart", ConditionOperator.LessThan, acctRecord.Attributes["new_nextactivitydate"]);
return true;
}
//Evaluate whether the Start Date of the Appointment is already past.
public bool NextActivityDateIsPast(IOrganizationService _service, IWorkflowContext wfcontext)
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "account";
qe.ColumnSet = new ColumnSet("accountid", "new_nextactivitydate");
qe.Criteria = new FilterExpression();
qe.Criteria.AddCondition("new_nextactivitydate", ConditionOperator.LessThan, DateTime.Now);
return true;
}
}
}
LOE
Monday, July 24, 2017 10:10 PM
Answers
-
Thanks to Aaron, Faisal and David. Your contributions were helpful.
I had to contact Microsoft for assistance, and when done I saw that my errors were partly covered in your contributions.
Thank you very much.
Longinus.
LOE
- Marked as answer by western Canada Business Technologies Sunday, October 29, 2017 8:37 PM
Sunday, October 29, 2017 8:37 PM
All replies
-
A few thoughts:
- That is quite a bit of code... can you post the whole exception so we can see which line is throwing the exception?
- The methods containing the Query Expressions return true without actually running the queries. Did you set it that way for debugging?
- For debugging purposes you might want to create this as a console app.
Tuesday, July 25, 2017 1:16 AM -
Normally you get this error either schema name of field is not correct or you are trying to get a value which is null.
Regards Faisal
Tuesday, July 25, 2017 8:20 AM -
As per the previous post, I expect you're not coping with null attribute values. See http://mscrmuk.blogspot.co.uk/2013/04/the-given-key-was-not-present-in.html for a bit more on the most likely causes
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Tuesday, July 25, 2017 12:28 PMModerator -
Thanks to Aaron, Faisal and David. Your contributions were helpful.
I had to contact Microsoft for assistance, and when done I saw that my errors were partly covered in your contributions.
Thank you very much.
Longinus.
LOE
- Marked as answer by western Canada Business Technologies Sunday, October 29, 2017 8:37 PM
Sunday, October 29, 2017 8:37 PM