2

I am new to CRM development. I have a Custom Entity "customer". This Entity has a Field called "defaultcustomer", which can be TRUE or FALSE. I am working on a Plug-In where i need to set the "defaultcustomer" to FALSE for all the "Customers". I am doing it as below:

FACTS:

I have registered the plugin for the entity "customer" itself. So when the Entity "customer" is updated, the plugin fires.

private void MakeAllNonDefault()
{

    try
    {
        QueryExpression query = new QueryExpression("customer");
        query.ColumnSet = new ColumnSet("defaultcustomer");

        EntityCollection retrieved = service.RetrieveMultiple(query);

        foreach (Entity myCustomer in retrieved.Entities)
        {

            myCustomer["defaultcustomer"] = false;
            service.Update(myCustomer);
        }

    }
    catch (Exception ex)
    {
        throw new InvalidPluginExecutionException("An error occurred in MakeAllNonDefault(): " + ex.ToString());
    }
}

ERROR: It throws error on this line:

myCustomer["defaultcustomer"] = false;

System.Collections.Generic.KeyNotFoundException: 
The given key was not present in the dictionary. 
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • 2
    This type of error goes away if you use early-binding and cast your returned entities to instances of your `Customer` entity. – Peter Majeed Mar 28 '12 at 19:52

4 Answers4

7

The error means that particular field is not present in the collection of properties. In CRM, only properties that have been set or updated are included.

Try something like:

foreach (Entity myCustomer in retrieved.Entities)
{
    if (myCustomer.Attributes.ContainsKey("defaultcustomer"))
    {
        myCustomer["defaultcustomer"] = false;
    }
    else
    {
        myCustomer.Attributes.Add("defaultcustomer", false);
    }
    service.Update(myCustomer);
}
Guido Preite
  • 14,905
  • 4
  • 36
  • 65
glosrob
  • 6,631
  • 4
  • 43
  • 73
  • 1
    This is possible even though he explicitly includes that attribute in the `ColumnSet` of a new `QueryExpression`? – Peter Majeed Mar 28 '12 at 19:55
  • 2
    @PeterMajeed - as far as I know, if there is no value in the field, it will not be returned. – glosrob Mar 29 '12 at 06:02
  • @glosrob: Maybe this is another reason not to use late-binding. Our group doesn't use late-binding at all, but I would expect if you explicitly include the attribute and that attribute's value is `NULL`, the attribute would be present in the attribute collection with a value of `NULL` and would be available for value assignment. – Peter Majeed Mar 29 '12 at 13:13
1

Have you double checked that the field really is called defaultcustomer?

If it's a custom entity then it's likely the field begins with a prefix, for instance new_defaultcustomer. Make sure you are using the name of the field, not the display name.

  • Actually that is a very good point. If it is a custom entity, all fields (other than the default createdby, created etc.) will, as far as I know, have a prefix. – glosrob Mar 29 '12 at 19:00
0

While Update all Crm fields are False accept that what you Update the field. For that you can use Pre/Post Images in Plugin. you will found that crm field key and update what you need.

Srinivas Hsk
  • 388
  • 3
  • 17
0

The solution posted by @glosrob seems fine. Are you still getting "The given key was not present in the dictionary"?

Try to use ITracingService to get more information about the plugin execution flow.

Ahmed
  • 75
  • 5