public class InterviewersPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service =
factory.CreateOrganizationService(context.UserId);
var fetchXml = $@"<?xml version=""1.0"" encoding=""utf-16""?>
<fetch>
<entity name=""cr884_recruitmentprojectinterviewers"">
<attribute name=""cr884_interviewnumber"" />
<attribute name=""cr884_recruitmentproject"" />
</entity>
</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
var x = 0;
Entity entity = (Entity)context.InputParameters["Target"];
Entity ret = service.Retrieve("cr884_recruitmentprojectinterviewers", entity.Id, new ColumnSet(true));
Guid id = ((EntityReference)ret.Attributes["cr884_recruitmentproject"]).Id;
foreach (Entity e in result.Entities)
{
if (!id.Equals(((EntityReference)e.Attributes["cr884_recruitmentproject"]).Id))
{
continue;
}
else
{
x++;
}
}
string count = x.ToString();
Entity ent = new Entity("cr884_recruitmentprojectinterviewers");
ent.Id = context.PrimaryEntityId;
ent["cr884_interviewnumber"] = count;
service.Update(ent);
}
}
}
}
Im getting The given key was not present in the dictionary. can someone tell me why please ?