Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Plugin error

(0) ShareShare
ReportReport
Posted on by 1,703

I have been looking at code to de-activate child records in contacts, when the parent is de-activated. I keep getting the following error

<s:Envelope xmlns:s="schemas.xmlsoap.org/.../envelope"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring xmlns:xml="www.w3.org/.../namespace" xml:lang="en-GB">Unexpected exception from plug-in (Execute): Deactivate_Child_Records.DeactivateChildRecords.VbDeactivateChildRecords: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.</faultstring><detail><OrganizationServiceFault xmlns="schemas.microsoft.com/.../Contracts"><ErrorCode>-2147220956</ErrorCode><ErrorDetails /><Message>Unexpected exception from plug-in (Execute): Deactivate_Child_Records.DeactivateChildRecords.VbDeactivateChildRecords: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.</Message><Timestamp>2015-04-27T17:49:40.4671725Z</Timestamp><InnerFault xmlns:i="www.w3.org/.../XMLSchema-instance" i:nil="true" /><TraceText>
[Deactivate Child Records: Deactivate_Child_Records.DeactivateChildRecords.VbDeactivateChildRecords]
[622658a6-05ed-e411-80c4-00155d01db0a: Deactivate_Child_Records.DeactivateChildRecords.VbDeactivateChildRecords: SetStateDynamicEntity of account]

</TraceText></OrganizationServiceFault></detail></s:Fault></s:Body></s:Envelope>

Plugin code

Imports Microsoft.Crm.Sdk.Messages
Imports Microsoft.Xrm.Sdk
Imports Microsoft.Xrm.Sdk.Query
 
 
' ReSharper disable once CheckNamespace
Namespace DeactivateChildRecords
    Public Class VbDeactivateChildRecords
        Implements IPlugin
 
        Public Sub Execute(serviceProvider As IServiceProvider) Implements IPlugin.Execute
 
 
            Dim context As IPluginExecutionContext =
                    DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
            Dim serviceFactory As IOrganizationServiceFactory =
                    DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), 
                               IOrganizationServiceFactory)
            Dim service As IOrganizationService = serviceFactory.CreateOrganizationService(context.UserId)
 
            If _
                context.InputParameters.Contains("EntityMoniker") AndAlso
                TypeOf context.InputParameters("EntityMoniker") Is EntityReference Then
 
                Dim account As EntityReference = DirectCast(context.InputParameters("EntityMonkier"), EntityReference)
 
                Dim state As OptionSetValue = DirectCast(context.InputParameters("State"), OptionSetValue)
                ' Deactive child records
                If state.Value = 1 Then
                    Dim _
                        contact As _
                            New QueryExpression() _
                            With {.EntityName = "contact", .ColumnSet = New ColumnSet("contactid", "parentcustomerid")}
                    contact.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, account.Id)
                    contact.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0)
                    Dim retrievecontact As EntityCollection = service.RetrieveMultiple(contact)
                    If retrievecontact.Entities.Count > 0 Then
                        For Each a As Object In retrievecontact.Entities
                            Dim contactsetStateReq As New SetStateRequest()
                            contactsetStateReq.EntityMoniker = New EntityReference(a.LogicalName,
                                                                                   New Guid(a.Id.ToString()))
                            contactsetStateReq.State = New OptionSetValue(1)
                            contactsetStateReq.Status = New OptionSetValue(-1)
                            service.Execute(contactsetStateReq)
                        Next
                    End If
                End If
 
            End If
        End Sub
    End Class
End Namespace

any help would be helpful

*This post is locked for comments

  • Verified answer
    Royal King Profile Picture
    27,686 on at
    RE: CRM Plugin error

    It looks like state Input parameter not exists in the context but you are trying to retrieve it without checking it , That may be causing the issue. Check the inputParameters availability before retrieving in your code like below

    if (context.InputParameters.Contains("State"))

    {

    OptionSetValue state = (OptionSetValue)context.InputParameters("State");

    }

  • Verified answer
    Mahadeo Matre Profile Picture
    17,021 on at
    RE: CRM Plugin error

    PeteN,

    CRM stores record State  in statecode attribute. It is look like you are using attribute "State".. change it to statecode

    Dim state As OptionSetValue = DirectCast(context.InputParameters("State"), OptionSetValue)

    To

    Dim state As OptionSetValue = DirectCast(context.InputParameters("statecode"), OptionSetValue)

    Hope this will help..

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
dkrishna Profile Picture

dkrishna 6

Featured topics

Product updates

Dynamics 365 release plans