Referencing the attributes of an entity in a plug-in? RRS feed

  • Question

  • Hi,

    I have just started writing a plug-in and have stumbled upon this problem.  I have the plug-in registered to the Case entity and it would trigger on a create or an update.

    In the code I am trying to reference the Case's attributes but I have been getting this error: "The given key was not present in the dictionary."

    I have been researching online and I am under the impression that on an update, not all attributes are available so this error occurs as I am trying to reference attributes that are not available.

    I then registered a post image for the update step, collecting all the attributes.  However, I am still getting this error.  Here is the code that I have.

            Public Sub Execute(ByVal context As IPluginExecutionContext) Implements IPlugin.Execute
                Dim entity As DynamicEntity = Nothing
                If context.InputParameters.Properties.Contains(ParameterName.Target) AndAlso TypeOf context.InputParameters.Properties(ParameterName.Target) Is DynamicEntity Then
                    entity = CType(context.InputParameters.Properties(ParameterName.Target), DynamicEntity)
                    If (entity.Name IsNot EntityName.incident.ToString()) Then Return
                End If
                    Dim crmService As ICrmService = context.CreateCrmService(True)
                    Dim testOutput As String
                    testOutput = CType(context.PostEntityImages.Properties("new_testEntityid"), Lookup).Value.ToString()
                Catch ex As System.Web.Services.Protocols.SoapException
                    Throw New InvalidPluginExecutionException(String.Format("An error occurred in the {0} plug-in.", Me.GetType().ToString()), ex)
                End Try
            End Sub

    Basically I am trying to access the "new_testEntityid", which is a Lookup field on the Case that links to the "new_testEntity".  Can anyone shed some light on this issue?

    On another note, if I have a lookup field like the above, is there any way to retrieve the actual "new_testEntity" object and extract details on the instance of the entity that this is linked to?

    Any help would be greatly appreciated!


    Monday, January 11, 2010 10:46 PM


  • Hi Steve

    You'll only be able to get the "new_testentityid" on update if the user changes the value, on create it should be available as long as the user selected a value for it.

    I would recommend getting the case/incident id, then run a retrieve query to get the full case, from there you will be able to access any attribute.
    Execute() ...
        Guid incidentId = get the id from the Parameter.Target
        DynamicEntity incident = execute a retrieve query to find the incident by incidentId
        check if the incident.Properties collection has "new_testentityid"
            get the value...
    XrmLinq - LINQ to Dynamics CRM http://www.xrmlinq.com - The smart way to query crm
    Monday, January 11, 2010 11:05 PM