user2968949
user2968949

Reputation: 1

CRM 2011 - How to set data in records from subgrid in Entity using C# code

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

Upvotes: 0

Views: 1337

Answers (1)

Daryl
Daryl

Reputation: 18895

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

Upvotes: 2

Related Questions