locked
The given key was not present in the dictionary CRM (2013) Connection plugin RRS feed

  • Question

  • Hello,

    I have the following code which is triggered when a new connection is created:

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Get the target entity from the input parameters
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    if (entity.LogicalName == "connection")
                    {
                        // Get the Contact in the From field (record1id)
                        EntityReference fromNameId = (EntityReference)entity.Attributes["record1id"];
                        Guid _contactId = fromNameId.Id;
                        // Get the Connection Role Id (record2roleid)
                        EntityReference connectionRoleId = (EntityReference)entity.Attributes["record2roleid"];
                        Guid _connectionRoleId = connectionRoleId.Id;
    
                        // Use fetchxml to find other active Connection roles with the same id
                        string fetch2 = @"
                            <fetch mapping='logical' distinct='false'>
                              <entity name='connection'>
                                <attribute name='record2id' />
                                <attribute name='record2roleid' />
                                <attribute name='connectionid' />
                                <order attribute='record2id' descending='false' />
                                <filter type='and'>
                                  <condition attribute='statecode' operator='eq' value='0' />
                                  <condition attribute='record1id' operator='eq' uitype='contact' value='{" + _contactId + @"}' />
                                  <condition attribute='record2roleid' operator='eq' uiname='Third Party Authorities' uitype='connectionrole' value='{757A9E2B-B844-E411-80D4-984BE14D9011}' />
                                </filter>
                              </entity>
                            </fetch>";
    
                        EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetch2));
    

    The 'Get the Contact in the From field' works without any issues, but when I try to get the Connection Role (record2roleid) I get the above error.  If the Connection role is coded in the FetchXML it works.  I cannot hard code the value as the plugin will run on different systems and the Connection Role will have different GUIDs.

    Thanks in advance

    Duane

    Sunday, September 28, 2014 8:24 AM

Answers

  • Sorted it myself.

    Replace the following line:

    EntityReference connectionRoleId = (EntityReference)entity.Attributes["record2roleid"];

    With:

    EntityReference connectionRoleId = entity["record2roleid"] as EntityReference;

    Duane

    • Marked as answer by Duane Stead Sunday, September 28, 2014 9:48 AM
    Sunday, September 28, 2014 9:48 AM