2

We have a scenario where we need to calculate multiple fee components & share between Pre & Post stages of a Plugin. So we created the object like this.

class FeeCalculation
    {
        public string TotalFee { get; set; }
        public string FilingFee { get; set; }
        public string LegalFee { get; set; }
        public string RecordFee { get; set; }
    }

So far we have used single fee component & shared variable worked nicely. When tried to assign the whole object - the result is not fruitful.

context.SharedVariables.Add("fees", fcObject);

Is there a way to achieve this expected result?

Arun Vinoth - MVP
  • 21,521
  • 14
  • 57
  • 157

2 Answers2

5

The plugin infrastructure must be able to serialize/deserialize the objects that are in your SharedVariables collection. It has no knowledge of custom types like the FeeCalculation class and therefore cannot serialize it.

Use primitive types, common .NET types (e.g. a List of decimals should work) or CRM types (Entity, Money etc.). It's good to note that the SharedVariables collection is a key value pair collection. So, why not just add items to it with keys like "TotalFee", "FilingFee" etc?

Henk van Boeijen
  • 6,595
  • 6
  • 30
  • 41
  • Yes I agree, but not all the fee components will be applicable all the time. Will try & let you know.. thanks. – Arun Vinoth - MVP Jun 06 '17 at 22:37
  • 2
    @ArunVinoth as a possible alternative you can manually serialize/deserialize and pass serialized string as a shared variable. – Andrew Butenko Jun 07 '17 at 01:21
  • @AndriiButenko - Worked great thank you and a better answer IMHO. Used these extension methods https://stackoverflow.com/a/26026980 – Simon Nov 07 '18 at 17:02
0

I was trying to share an IEnumerable<Guid>, but collections like List did not work.

Of course you can always serialize to string, but your plugin code really shouldn't have to deal with serialization of common types.

After some trial and error, I ended up using arrays, i.e. Guid[].

avolkmann
  • 2,678
  • 2
  • 17
  • 26