web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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

I have the same question (0)
  • Verified answer
    Mahadeo Matre Profile Picture
    17,021 on at

    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..

  • Verified answer
    Royal King Profile Picture
    27,686 on at

    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");

    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans