Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Xrm.Sdk.SaveChangesException in CRM 2011

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
like image 353
user1081934 Avatar asked Dec 05 '11 16:12

user1081934


Video Answer


2 Answers

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.

like image 92
GotDibbs Avatar answered Sep 16 '22 21:09

GotDibbs


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);
like image 24
Nicknow Avatar answered Sep 16 '22 21:09

Nicknow