Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

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

Posted on by Microsoft Employee

using Microsoft.Xrm.Sdk;
using System;
using System.ServiceModel;


namespace its_ain_boring
{
public class Class1 : IPlugin
{
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");
Entity entity = (Entity)context.InputParameters["target"];
if (entity.LogicalName == "account")
{

try
{
trace.Trace("inside try");
//latebound
Entity createcontact = new Entity("contact");
createcontact["firstname"] = "Minh";
createcontact["lastname"] = "Nguyen";
createcontact["emailaddress1"] = "nhatminhnguyen1122@gmail.com";
createcontact["description"] = "boss";
if (context.OutputParameters.Contains("id"))
{
createcontact["parentcustomerid"] = new EntityReference { Id = new Guid(context.OutputParameters["id"].ToString()), LogicalName = entity.LogicalName };
}
service.Create(createcontact);

}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}

}
}
}

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Exception Message: The given key was not present in the dictionary.

    Hi,

    Instead of getting the account id from output parameter you can get from target like below

    createContact["parentcustomerid"]=new EntityReference(entity.LogicalName, entity.Id);

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Exception Message: The given key was not present in the dictionary.

    Hi minh nhat,

    It seems the issue might be in the highlighted below lines: 

    if (context.OutputParameters.Contains("id"))
    {
      createcontact["parentcustomerid"] = new EntityReference { Id = new Guid(context.OutputParameters["id"].ToString()), LogicalName = entity.LogicalName };
    }

    You need to write  something similar to the below in your if statement:

    context.OutputParametrs.Contains("id") && context.OutputParametrs.GetAttributeValue("id") != null

    Let me know if I can provide you with more details.

    Thanks
    Regards,

    Abdul Wahab
    Power Platform/Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp: 923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit
    Linkedin: www.linkedin.com/.../

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans