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