-2

Registered a plugin on create (post operation). It works, but after that I added two fields on the form. On create I am updating the field values of the form. It works but it fails while comparing result entity attribute:

QueryExpression QrySales = new QueryExpression() { EntityName = Sales.LogicalName, ColumnSet = new ColumnSet(true) };
EntityCollection SalesCollec = Service.RetrieveMultiple(QrySales);
foreach (Entity sales in SalesCollec.Entities)
{
  if (sales["new_membername"].ToString() ==  UpdateSales["new_membername"].ToString())
  {
     index = index + 1;
  }
}
UpdateSales["new_index"] = index + 1;
Service.Update(UpdateSales);
Sulthan
  • 128,090
  • 22
  • 218
  • 270
haRshad
  • 21
  • 2
  • Please edit your post because it's unclear and I don't understand the description of your problem. – pen2 Oct 20 '16 at 07:16

1 Answers1

0

You are explicitly casting an attribute's value to string which very well could not exist in the attribute collection. Either check if an attribute with the name new_membername exists in the returned entity attribute collection or use the helper methods in the Microsoft.Xrm.Client library to safely fetch the attribute values.

if (sales.GetAttributeValue<string>("new_membername") == 
    UpdateSales.GetAttributeValue<string>("new_membername"))
{
    index = index + 1;
}

You might also want to re-consider your compare logic as you might end up comparing nulls, which is probably not what you want I guess.

dynamicallyCRM
  • 2,980
  • 11
  • 16