web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

(0) ShareShare
ReportReport
Posted on by

I am trying to update parent entity (Invoice) on create of child entity (InvoicePorduct).

on service.Update(Inv), i get the error (System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary)

Below is my code

Thanks for help :)

protected void ExecutePreInvoiceProductCreate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;


// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
// Entity entity = context.PostEntityImages["PostImage"];

if (context.MessageName == "Create")
{
try
{
Guid guidID = ((EntityReference)entity["invoiceid"]).Id;

if (!(guidID == Guid.Empty))
{

Entity inv = new Entity("invoice");

inv.Id = guidID;

using (ServiceContext svcContext = new ServiceContext(service))
{

var ts =

(from New_payments in svcContext.New_paymentSet
where
New_payments.new_invoiceid.Equals(guidID)
// group New_payments by New_payments.new_invoiceid into g

select new
{
new_Invoiceid = New_payments.new_invoiceid,
New_Amount = New_payments.New_Amount
});

foreach (var paymentAmount in ts)
{
if (paymentAmount != null)
{
paymentTotal = paymentTotal + Convert.ToDecimal(paymentAmount.New_Amount.Value);
}

else { paymentTotal = 0; }
}

var invAmount = (from i in svcContext.InvoiceSet
where
i.InvoiceId == guidID
select new
{
Invoiceid = i.InvoiceId,
TotalAmount = i.TotalAmount
});

foreach (var inc in invAmount)
{

if (inc != null)
{
totalAmount = totalAmount + Convert.ToDecimal(inc.TotalAmount.Value);
}

else { totalAmount = 0; }

}

balance = totalAmount - paymentTotal;

if (inv.Attributes.Contains("new_paymenttotal"))
{
inv.Attributes["new_paymenttotal"] = new Money(paymentTotal);
}
else
{
inv.Attributes.Add("new_paymenttotal", new Money(paymentTotal));
}

if (inv.Attributes.Contains("new_balance"))
{
inv.Attributes["new_balance"] = new Money(balance);
}
else
{
inv.Attributes.Add("new_balance", new Money(balance));
}
service.Update(inv);

}
}
}

catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the Invoice Product Create plugin:-", ex);
}

}
}}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Montek,

    You have any other plugin working during invoice update?

    Thanks.

  • Verified answer
    Pramod M Profile Picture
    1,445 on at

    Since you are mentioning that the error happens at service.Update(Inv), as Aileen mentioned there may be a dependent plugin firing on update of invoice where you have trying to refer an item which may not be present in the target.  Which may be an attribute  that you are not updating in your code. Please go ahead and add a contains check in that plugin.

  • Community Member Profile Picture
    on at

    Thanks Aileen and Guys . the suggestion worked :)

  • Venkatesh Ekkaluri Profile Picture
    150 on at

    Hi pramod, I have month and Year field in the form, Based on that i should get MonthYear in this format (02/2020) for this am using plugin. on update of record plugin will trigger. If i change only month or Year . it is throwing the same error. "System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.". i need to run the plugin either Month/Year Change .

  • Hameed Hussain Profile Picture
    250 on at

    Lakshmi,

    I believe for the object types, like, EntityReference, and Date fields, if you do not have a value in the field, the plugin throws this KeyNotFound exception error. Try with a value inside the field and check if the plugin displays the same error. I ran into this error recently with the EntittyReference field, but it was able to pull values if the field had a lookup value.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans