Microsoft.Xrm.Sdk.SaveChangesException in CRM 2011

I recently started working with plugins in CRM 2011, and I ran into problems with plugins registered in the Create message as Post-Operation.

When I register creation as a post-operation, I expect that when I delete the plugin code, the object is already created in the database, and I would have to create a related object (linked to the newly created object using a foreign key) in the plugin. But when I create a linked object and update it and say SaveChanges (), it gives me a Microsoft.Xrm.SaveChangesException "Error processing this request"

And if I move on to the internal exception, it just points to OrganizationServiceFault. The stack trace shows:

Server stack trace: in System.ServiceModel.Channels.ServiceChannel.HandleReply (operation ProxyOperationRuntime, ProxyRpc & rpc) in System.ServiceModel.Channels.ServiceChannel.Call (String action, Boolean oneway, ProxyOperationRuntime operation Object,] outs, TimeSpan timeout) in System.ServiceModel.Channels.ServiceChannelProxy.InvokeService (IMethodCallMessageCall, ProxyOperationRuntime method) in System.ServiceModel.Channels.ServiceChannelProxy.Invoke (message with message)

Exception thrown at [0]: in System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage (IMessage reqMsg, IMessage retMsg) in System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke (MessageData & msgData in Microsoft Int32 .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 encounter this problem only when creating a message, if I do the same action when updating or deleting, it works fine. Has anyone encountered this problem? Please provide some materials that I can try to solve this problem. Thanks in advance!

Also, here is my plugin code.

The plugin starts when the ct_repcode object is created, and then in my plugin I create the ct_repcodeMember object, which has a ct_repcodeid field that refers to the real ct_repcode object.

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
source share
4 answers

I have encountered this problem before. I believe the problem is that in CRM 2011, both Pre and Post operations occur while you are still in the database transaction.

The way we did this is to flip the plugin to run asynchronously, since a synchronous result is not needed.

I'm not sure if there is another way with your current code structure. I have not tried it either, but considering that you can create an object just fine, can you create a repcodeMember object with a filled search? Is there a need to create, retrieve, and then upgrade? If you have code that runs to create a linked object, you can share it with this plugin so you can just create a direct creation, as this update creates problems.

+2
source

You can also try setting a logical name for the Entity Reference ct_repcodeid link, it looks like you are setting only the identifier.

+1
source

Pay tribute to @rocksolid and @patricgh, I just accepted their suggestions, combined them and placed a code sample around it.

You must abandon the use of Service Context for this operation and use the CRM CRUD function by default. Your EntityReference incorrect because you must have a logical name, but since you already have Entity , you should simply 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
source

I think what you did: creating, searching, and updating an object through context is not a good way. You can just create an object, I mean updating the object, and then create it through the service. I put the code as shown 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); 
0
source

Source: https://habr.com/ru/post/1384738/


All Articles