I recently started working with plugins in CRM 2011 and I am facing issues with plugins registered on the Create message as a Post-operation.
When I register the create as a post-operation, I would expect that when I hit the plugin code, the entity has already been created in the database and I should be able to create a related entity(related to the newly created entity by a foreign key) in the plugin. But when I create the related entity and update it, and say SaveChanges(), it gives me a Microsoft.Xrm.SaveChangesException "An error occurred while processing this request"
And if I drill down to the inner exception, it just points to the OrganizationServiceFault. The stack trace it shows is:
Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results
I face this issue only on the create message, if I do the same operation on an update or delete, it works fine. Has anybody faced this issue? Please provide some inputs on the what I can try to resolve this issue. Thanks in advance!
Also, here is my plugin code.
The plugin gets fired when the ct_repcode entity is created and then in my plugin I create a ct_repcodeMember entity which has a ct_repcodeid field which links to the actual ct_repcode entity.
Entity repcodeEntity = _context.InputParameters["Target"] as Entity;
Guid repcodeId = repcodeEntity.Id;
//Create a new Ct_repcodemember object
Ct_repcodemember repcodeMember = new Ct_repcodemember();
Guid repCodeMemberId = _service.Create(repcodeMember);
repcodeMember = _serviceContext.Ct_repcodememberSet.Where(a => a.Id == repCodeMemberId).FirstOrDefault();
repcodeMember.ct_repcodeid = new EntityReference { Id = repcodeId };
//Update the object and save the changes in crm
_serviceContext.UpdateObject(repcodeMember);
_serviceContext.SaveChanges(); // --- The timeout error happens here