0

I am making a plugin in CRM 2011. The plugin is taking data from the Entity's subgrid by using fetchXML, making some calculate with the data and at the end of the plugin I want to set the new calculated data back in the subgrid, but I can't ...

I tried few ways to do that like:

(1)

    private static OptionSetValue CreateOptionSet(int optionSetValue)
    {
        OptionSetValue optionSetInstance = new OptionSetValue();
        optionSetInstance.Value = optionSetValue;
        return optionSetInstance;
    }

(2)

    public void setVal(Entity entity, string attr, object val)
    {
        if (entity.Attributes.Contains(attr))
        {
            entity[attr] = val;
        }
        else
        {
            entity.Attributes.Add(attr, val);
        }
    }

and just

paid["zbg_paidamount"] = 400;


payment.Attributes["zbg_suggestedamount"] = paidVal;

But nothing works...

I am thinking maybe is from the type of the data that I am trying to set but not sure.

Please if you can help me I am desperate.

Thanks

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • I figure it out :) I forgot to update the subgrid after the edit. With System.Update all the set data is there. – user2968949 Nov 08 '13 at 14:41

1 Answers1

2

Even though it looks like you've resolved your issue each section of your code has an issue with it...

(1) - Use the int Constructor for OptionSetValue:

(2) - don't worry about checking the value existing or not, just set it directly on the entity (also don't worry about accessing the Attributes collection)

payment["zbg_paidamount"] = new OptionSetValue(400);

In Response to Draiden's Comments

The indexer on the Entity class will automatically handle adding or updating a value. Here is an example LinqPad program:

Linqpad Example

Daryl
  • 18,592
  • 9
  • 78
  • 145
  • hi Daryn, if he is using late bound i don't think that is a bad habit to check if the property is there – Mauro De Biasio Nov 11 '13 at 03:12
  • @Draiden `payment["zbg_paidamount"]` will already check to see if the attribute exists in it's collection and set it, and if it doesn't exist, it will add it. So the `setVal` method is completely unneeded. – Daryl Nov 11 '13 at 13:41
  • are you sure about it? Because in an early bound context you will find all the attributes in a late bound context (especially on update) you will find all the updated attributes ad the ones != null, if payment is not in the list, it will just throw an exception: "The key is not present or something similar". So you need to check if the attribute is there and eventually add it. – Mauro De Biasio Nov 11 '13 at 23:37
  • @Draiden I think you have it wrong. Reads will throw an exception if the attribute doesn't exist in the collection, but inserts and updates work without issue (see edit to answer). When accessing an attribute programatically, just use the `GetAttributeValue()` (http://msdn.microsoft.com/en-us/library/gg326129.aspx, and it will perform the check to see if the attribute exists, and if not, default the value. – Daryl Nov 12 '13 at 01:19
  • I will test it later on :) but if you are right good to know, i learnt something new! – Mauro De Biasio Nov 12 '13 at 05:00