0
votes

I have a Synchronous plugin that runs when any opportunity create/delete/update. And in Plugin if any error comes i have made a function which insert log into database.

In table one field if EntityId, so i am writing the following code :

foreach (PropertyBagEntry entry in (IEnumerable<PropertyBagEntry>)context.InputParameters.Values)
{

        DynamicEntity entity = (DynamicEntity)entry.Value;
        foreach (Property property in (IEnumerable<Property>)entity.Properties)
        {
            if (property.GetType().Name == "KeyProperty")
            {
                str4 = ((Key)entity.Properties[property.Name]).Value.ToString();
                break;
            }
        }
}

In str4 i am getting EntityId of current process.

But it gives one exception very frequently :

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'ValueCollection[System.String,System.Object]' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Crm.Sdk.PropertyBagEntry]'

And i have identified that the following line is giving error

foreach (PropertyBagEntry entry in (IEnumerable)context.InputParameters.Values)

Anyone have idea to convert this line in another way ?

3
This looks to be CRM 4.0 code, since DynamicEntity is a 4.0 SDK entity...Daryl

3 Answers

0
votes

My understanding is that you want to get the GUID of current record, if this is the case then you can do it as:

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    try
    {
        if (context.MessageName == "Create" || context.MessageName == "Update")
        {
            if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is Entity))
            {
                Entity currentEntity = (Entity) context.InputParameters["Target"];
                Guid currentRecordGuid = currentEntity.Id;
            }

        }
    }
    catch (Exception ex)
    {

    }
}
0
votes

I believe the type of that collection varies from one message to another. If you are looking to get the Id of the record within a plugin, this helper function may come in handy:

public static Guid GetEntityIdFromContext(IPluginExecutionContext context)
{
    string messageName = context.MessageName;
    if (context.PrimaryEntityId != Guid.Empty)
    {
        return context.PrimaryEntityId;
    }
    else if (messageName == "Create")
    {
        return new Guid(context.OutputParameters["id"].ToString());
    }
    else
    {
        return context.PrimaryEntityId;
    }
}

If that doesn't help, would you mind providing the message that causes the error?

0
votes

If

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'ValueCollection[System.String,System.Object]' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Crm.Sdk.PropertyBagEntry]'

is truly your error, than your issue is not with line

foreach (Property property in (IEnumerable<Property>)entity.Properties)

but with line:

foreach (PropertyBagEntry entry in (IEnumerable<PropertyBagEntry>)context.InputParameters.Values)

The type of context.InputParameters.Values is not castable to an IEnumerable