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 ?
you can't think that the lookup is not null but somewhere is.
Adding to a33ik response (that is enough to solve your problem) I explain you why.
The error "The given key was not present in the dictionary." can be generated when accessing a dictionary with a key that doesn't exist. In your code the only dictionary is the "Attributes" property you are accessing of the Entity objects.
in particular line
Guid id = ((EntityReference)ret.Attributes["cr884_recruitmentproject"]).Id;
and line
if (!id.Equals(((EntityReference)e.Attributes["cr884_recruitmentproject"]).Id))
one of the two lines is giving you the error (and if you debug you can which one, if the current entity or the one returned by the fetchxml you executed).
to solve use the GetAttributeValue (as suggested by a33ik ) and check if the lookup is value is not null.
If you still think that the lookup is not null, check the data and if all the records have the lookup cr884_recruitmentproject filled then open a support ticket to Microsoft. please let us know the result.
Thanks
the lookup is not null
the logic of my code and the syntax is right
I am still not figuring out what the reason of this error
Please advise
Hello,
I'm pretty sure that you have an error in one of 2 following lines:
Guid id = ((EntityReference)ret.Attributes["cr884_recruitmentproject"]).Id;
or
if (!id.Equals(((EntityReference)e.Attributes["cr884_recruitmentproject"]).Id))
I recommend using the following approach to reference the attribute:
var reference = record.GetAttributeValue<EntityReference>("lookup field schema");
If the lookup doesn't have any value (that's what is happening in your case I believe) reference will be null.
So in order to avoid this issue you should handle that in a proper way.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156