Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Field Service forum / CRM Dynamics Plugin Er...
Field Service forum

CRM Dynamics Plugin Error : The given key was not present in the dictionary (When I trigger plugin on BookingDate Create)

(0) ShareShare
ReportReport
Posted on by

When I trigger plugin on BookingDates Create 

indeed to update some filed related to BookingSetups

and use that Plugin

IOrganizationService Service = serviceProxy;
Entity entity = new Entity("msdyn_agreementbookingdate");

var fetchXmlDates = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
fetchXmlDates += "<entity name='msdyn_agreementbookingdate'>";
fetchXmlDates += "<attribute name='msdyn_bookingsetup' />";
fetchXmlDates += "<attribute name='msdyn_bookingdate' />";
fetchXmlDates += "<attribute name='msdyn_agreement' />";
fetchXmlDates += "<attribute name='msdyn_agreementbookingdateid' />";
fetchXmlDates += "<order attribute='msdyn_bookingdate' descending='true' />";
fetchXmlDates += "<filter type='and'>";
fetchXmlDates += "<condition attribute='msdyn_agreement' operator='eq' uitype='msdyn_agreement' value='368093c6-a856-e911-a964-000d3a4647a5' />";
fetchXmlDates += "<condition attribute='msdyn_agreementbookingdateid' operator='ne' value='" + entity.Id + "' />";
fetchXmlDates += "</filter>";
fetchXmlDates += "<link-entity name='msdyn_agreementbookingsetup' from='msdyn_agreementbookingsetupid' to='msdyn_bookingsetup' alias='bookRec'>";
fetchXmlDates += "<attribute name='new_bookingrecurring' />";
fetchXmlDates += "<attribute name='msdyn_agreementbookingsetupid' />";
fetchXmlDates += "<attribute name='new_customerasset' />";
fetchXmlDates += "<filter type='and'>";
fetchXmlDates += "<condition attribute='new_bookingrecurring' operator='not-null' />";
fetchXmlDates += "</filter>";
fetchXmlDates += "</link-entity>";
fetchXmlDates += "</entity>";
fetchXmlDates += "</fetch>";

EntityCollection resultDates = Service.RetrieveMultiple(new FetchExpression(fetchXmlDates));

var multipleRequest = new ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
Requests = new OrganizationRequestCollection()
};

foreach (Entity c in resultDates.Entities)
{

var Bookingsetupidfetch = c.Attributes["msdyn_agreementbookingsetupid"];
var BookingsetupidEntity = entity.Attributes["msdyn_bookingsetup"];

if (Bookingsetupidfetch == BookingsetupidEntity)
{
//Console.WriteLine("BookingDate: {0}", c.Attributes["new_customerasset"]);
UpdateRequest updateRequest = new UpdateRequest { Target = c };
multipleRequest.Requests.Add(updateRequest);
c.Attributes["new_customerasset"] = c.Attributes["new_customerasset"];
count++;
}
}

ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)Service.Execute(multipleRequest);

and that error appears 

0458.Untitled.jpg

but when  i use Static GUID Update operation is Work successfully done

regardes

  • RE: CRM Dynamics Plugin Error : The given key was not present in the dictionary (When I trigger plugin on BookingDate Create)

    But the change in the plugin :

    fetchXmlDates += "<condition attribute='msdyn_agreement' operator='eq' uitype='msdyn_agreement' value='" + agreementId + "' />";

    pass agreement ID Dynamically as parameter

  • Suggested answer
    RE: CRM Dynamics Plugin Error : The given key was not present in the dictionary (When I trigger plugin on BookingDate Create)

    thanks Kalpavruksh and  Rocker

    tried apply my below code in Console-application it work done

    but i tried apply the same code in New Plugin 
    that error shown :

    Entity Id must be the same as the value set in property bag

    IOrganizationService Service = serviceProxy;
    Entity entity = new Entity("msdyn_agreementbookingdate");

    var fetchXmlDates = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXmlDates += "<entity name='msdyn_agreementbookingdate'>";
    fetchXmlDates += "<attribute name='msdyn_bookingsetup' />";
    fetchXmlDates += "<attribute name='msdyn_bookingdate' />";
    fetchXmlDates += "<attribute name='msdyn_agreement' />";
    fetchXmlDates += "<attribute name='msdyn_agreementbookingdateid' />";
    fetchXmlDates += "<order attribute='msdyn_bookingdate' descending='true' />";
    fetchXmlDates += "<filter type='and'>";
    fetchXmlDates += "<condition attribute='msdyn_agreement' operator='eq' uitype='msdyn_agreement' value='2a111f0b-d56f-e911-a965-000d3a4647a5' />";
    fetchXmlDates += "<condition attribute='msdyn_agreementbookingdateid' operator='ne' value='" + entity.Id + "' />";
    fetchXmlDates += "</filter>";
    fetchXmlDates += "<link-entity name='msdyn_agreementbookingsetup' from='msdyn_agreementbookingsetupid' to='msdyn_bookingsetup' alias='bookRec'>";
    fetchXmlDates += "<attribute name='new_bookingrecurring' />";
    fetchXmlDates += "<attribute name='msdyn_agreementbookingsetupid' />";
    fetchXmlDates += "<attribute name='new_customerasset' />";
    fetchXmlDates += "<filter type='and'>";
    fetchXmlDates += "<condition attribute='new_bookingrecurring' operator='not-null' />";
    fetchXmlDates += "</filter>";
    fetchXmlDates += "</link-entity>";
    fetchXmlDates += "</entity>";
    fetchXmlDates += "</fetch>";

    var resultDates = Service.RetrieveMultiple(new FetchExpression(fetchXmlDates));

    int count = 0;

    try

    {

    foreach (Entity c in resultDates.Entities)
    {
    var fetchXml = $@"
    <fetch>
    <entity name='msdyn_agreementbookingsetup'>
    <attribute name='new_customerasset' />
    <attribute name='msdyn_agreementbookingsetupid' />
    <filter type='and'>
    <condition attribute='msdyn_agreementbookingsetupid' operator='eq' value='{c.GetAttributeValue<EntityReference>("msdyn_bookingsetup").Id}'/>
    </filter>
    </entity>
    </fetch>";
    var resultD = Service.RetrieveMultiple(new FetchExpression(fetchXml));


    foreach (Entity b in resultD.Entities)
    {
    if (c.GetAttributeValue<EntityReference>("msdyn_bookingsetup").Id == (Guid)b.Attributes["msdyn_agreementbookingsetupid"])
    {
    Console.WriteLine("BookingSetup: {0} - CustomerAsset : {1}", c.GetAttributeValue<EntityReference>("msdyn_bookingsetup").Name, b.GetAttributeValue<EntityReference>("new_customerasset").Name);

    entity.Id = (Guid)c.Attributes["msdyn_agreementbookingdateid"];
    entity.Attributes["new_customerasset"] = Convert.ToString(b.GetAttributeValue<EntityReference>("new_customerasset").Id);
    Service.Update(entity);

    count++;
    Console.WriteLine("Update No : ({0}) is Done!", count);
    }


    }


    }


    Console.ReadLine();

    }

    catch (Exception ex)

    {

    throw new InvalidPluginExecutionException(ex.Message);
    }

  • Verified answer
    Kalpavruksh D365 CoE Profile Picture
    Kalpavruksh D365 CoE 2,545 on at
    RE: CRM Dynamics Plugin Error : The given key was not present in the dictionary (When I trigger plugin on BookingDate Create)

    Hi,

    If the field value is empty in the context, it returns null and in your code, you are trying to fetch it using the following statements.

    var Bookingsetupidfetch = c.Attributes["msdyn_agreementbookingsetupid"];

    var BookingsetupidEntity = entity.Attributes["msdyn_bookingsetup"];

    Please modify the code as follows to check null values:

    if(c.contains("msdyn_agreementbookingsetupid") && c["msdyn_agreementbookingsetupid"] != null)

    var Bookingsetupidfetch = c.Attributes["msdyn_agreementbookingsetupid"]; 

    if(c.contains("msdyn_bookingsetup") && c["msdyn_bookingsetup"] != null){ 

    var Bookingsetupidfetch = c.Attributes["msdyn_bookingsetup"]; 

    }

    Please mark this as verified if the answer has helped you to resolve the issue.

  • Verified answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: CRM Dynamics Plugin Error : The given key was not present in the dictionary (When I trigger plugin on BookingDate Create)

    It seems there is no value in that particular field,  use contains to avoid this issue:

    if(c.Contains("msdyn_agreementbookingsetupid"))

    {

    var Bookingsetupidfetch = c.Attributes["msdyn_agreementbookingsetupid"];

    }

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans