5
votes

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
4
Could you please show some of the code? How do you associate the records?ccellar
@ckeller: I have updated my original question with a code snippet.user1081934
Also, slightly off-topic, but have you tried to set up a remote debugging session to see what the values of these variables are at run-time?Peter Majeed
@PeterMajeed - Yes. I have remote debugging set up and I see that the repcodeMember object and Id are created and returned, but when I say SaveChanges it times out.user1081934
Anybody have any ideas where I might be going wrong with this code?user1081934

4 Answers

2
votes

I've encountered this problem before as well. I believe the problem is that in CRM 2011, both the Pre and Post operations occur while you're still inside the database transaction.

The way we got around this was to flip the plugin to run asynchronously as a synchronous result was not necessary.

I'm not sure if there is another way around this with your current code structure. I haven't tried this either, but considering you can create the entity just fine, can you create the repcodeMember entity with the lookup populated? Is there a real need to do the create, retrieve, and then update? If you have some code that is running on create of the related entity, you may be able to share that with this plugin such that you can just do a straight create since it is the update that is giving you problems.

2
votes

Give credit to @rocksolid and @patricgh, I just took their suggestions, combined them, and put a code example around it.

You should drop the use of the Service Context for this operation and use CRM's default CRUD functionality. Your EntityReference is incorrect because you must have a logical name, but since you already have an Entity you should just use EntityReference to set the value.

Entity repcodeEntity = _context.InputParameters["Target"] as Entity;                 

Ct_repcodemember repcodeMember = new Ct_repcodemember();    

repcodeMember.ct_repcodeid = repcodeEntity.ToEntityReference(); 

_service.Create(repcodeMember);
1
votes

I think what you have done: creating, finding and updating an entity via context is not a good way. You can just create an entity I mean the object update and then create it it via service. I have put the code as below.

Entity repcodeEntity = _context.InputParameters["Target"] as Entity;                 
Guid repcodeId = repcodeEntity.Id;

Ct_repcodemember repcodeMember = new Ct_repcodemember();    

repcodeMember.ct_repcodeid = new EntityReference { Id = repcodeId }; 

_service.Create(repcodeMember);
1
votes

You could try also setting the Logical Name for the ct_repcodeid Entity Reference, it looks like you are only setting the Id.