Hi,
I want to perform conditional updates on the CRM database. For this, I want to perform Create-Update and Update-Delete in a single transaction so that if anything goes wrong with the any request, everything will be rollback.
I am aware of Rollback. But my question here is on the executing a single transaction with multiple request ex. one with CreateRequest and 2nd with UpdateRequest. Can we execute these requests in a single transaction?
Something like below code:
var transReq = new ExecuteTransactionRequest() { // Create an empty organization request collection. Requests = new OrganizationRequestCollection(), ReturnResponses = true }; var account = new Account() { Name = "Acme, Inc." }; var createReq = new CreateRequest { Target = account }; transReq.Requests.Add(createReq); account.NumberOfEmployees = 100; var updateReq = new UpdateRequest { Target = account }; transReq.Requests.Add(updateReq); var response = (ExecuteTransactionResponse)svc.Execute(transReq);
Any thoughts or ideas to achieve this?
*This post is locked for comments
That helped. Thank you.
It's not recommended because crm is using sql guid generation.. some kind of optimization is happening there.. other than that, it should work. I don't think you can do it any other way since you will need to get that good for the update/delete, and you won't be able to. Can only assign it in advance.
Yes I saw that but there was a comment you have added not recommended so I thought to ask a little different approach in my question, to try with different entity. Would you please go through that comment and let me know if it is possible, please.
Thank you,
Pratik Soni.
Think I just explained it above..
Yes Sir, in this case yes it is having a problem. Because 2nd request will not be able to fetch the ID created in request 1. But is there any way that we can even execute transaction in 2 different entities?
i.e. req1 will be Create request on Account entity while req2 will update the existing record of Contact entity.
If yes that we can do it... then can you please let me know how to do that?
Sorry.. I get it now.. if you create a record and want to update it after.. try setting the Id in your code..
Account.id = Guid.NewGuid()
Not recommended, in general, but should work.
Is there a problem with your code? Looms like it should do exactly that - execute both requests in the same transaction
Sorry if my question is misleading. Please have a look for updated question for better understanding.
ExecuteTransactionRequest will execute all requests in the same transaction. If one request fails, all others will be rolled back
ExecuteMultipleRequest will execute each request in its own transaction, so there is no rollback for all requests. If one request fails, what happens after depends on what you pass to the ContinueOnError parameter
Sure Gulcan, I am aware of Rollback. But my question here is on the executing a single transaction with multiple request ex. one with CreateRequest and 2nd with UpdateRequest. Can we execute these requests in a single transaction?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156