Sunday 5 July 2015

Utilizing CRM Custom Action for Transaction Rollback Purposes

Introduction

In CRM 2013, we have a new feature, so-called Custom Action.

Well, I have posted about this feature long ago.


That time I was rarely using that because got problem in deploying the Custom Action which I believed has been fixed and now my colleague’s opening again my mind to re-consider using this feature.

Custom Action Features

Do you know that beside its great features such as:

1.  For replacing Configuration entity
2. Or even to validate something that need server side code which you are no longer need plugin again since its capacity to be called without triggering it by creating or changing field value like what plugin does,
3. Or to call this using javascript,
4. Plus even new amusing feature in CRM 2015 Update 1 to call it through Workflow,
5. Again, you can add more action as well as what workflow has been doing for you the whole time.

What I want to emphasize and spot in this post is its capability to do rollback.

Example or Scenario

Imagine, in daily transaction, in CRM, you have Header and Detail, Parent and Child, and Main and Line Item, Transaction and Transaction Detail.

Then also for major impact transactions, such as Bank Account, posting to Finance, Order and Order Detail, you need to secure that all of records must be done properly and 100% success rate, otherwise fail it! You cannot deliver an Order to customer if not all the details were created successfully and also very common situation you need to flag a record that you have updated in other entity, you need to make sure both are success, you cannot just in halfway, success in one transaction while fail to another, you need BOTH of them SUCCESS and EITHER one is FAIL, ALL must be stated FAIL and roll it back as the original one. This table is very important because it is Financial record, a –finance-related means A Money related, so both must be in sync, one fail, both must fail.

Well, CRM also gives good example in term of reciprocal and must done transaction, that is fund transferring:

“The classic example is transferring funds between two bank accounts. If you withdraw funds from one account you must deposit them in the other. If either fails, both must fail.”

The Research and The Implementation

I think what you all are waiting for is the real example in term of coding or implementation

Now, let’s say I have two entities, Entity A and B.

If Entity A is fail created and I need to make sure B also fail and vice versa.

I have 5 Entity A records and 5 Entity B records.
1 of them is failed then I must fail them.

*All of the example is within the CRM DB Context, not other system (external) involved that can be rolled-back, okay.

Standard CRM Transaction CRUD Request

There is no rollback for this standard CRUD Request because it’s processing one by one record.

Execute Multiple Request CRM

Well, now let’s move little bit to more advance, what will happen if we use the Execute Multiple Request, will it be rolled-back?

The answer is no..It will stop you to the next action if you use the skip or ignore error parameter to No, but in fact, it does not roll back the previous made transaction.

Custom Action

This is what I want to emphasize.
Assuming, I have this Action:

image

*For creating an action you can refer to my blog posts or other good example through the internet and good explanation from Power Object:

Then, inside the Custom Action code, I have the Create Record like I did in the Code-1 but I put in the CRM Action by parsing the EntityCollection as input

image

*Inside the collapsed region:

image

This one if you enable, means “no error” so that it won’t enable roll-ed back because assuming this action never throw an error, so you might need to re-think when you implement this.

For roll-back purposes, do not catch error for each single transaction.

You can do that in lump sum then just throw it all!

Okay, now, so what happened to my record after I trigger this action through executing it programmatically?

Well, it throw me error and there is no record at all!

*First entity
image

*Second Entity

image

*Well, as we can see here is, one fail, make the others also fail.

Other Research

Now, to make it lively, I increase the number of record to 800.
Then I make the failure in 789th row.
image
See what happened when I keep refreshing… then in fact I failed in almost the last row.
*First Refresh
image
*as you can see we have 130 records here
Then..
keep refreshing
*Second Refresh…
image

208 records here..

*Third time..

image

667 records here..

And until my code was stopped..

image

Yeah, it does stop in the record no 789.

And now..

I try to refresh again the advanced find..

And guess what…

All records….It’s gone..

image

So, it has the concept, once one is failed, then the other must failed as well!

What if I disable the Roll-Back

To ensure my curiosity and my doubt about the rollback function (well even for such example I still have doubt little bit but annoying) then I try to disable the rollback checkbox to ensure this usage.
image
And here is the result when I turn it off.
image

*As you can see here is the record is created anyway, no rollback same as what CRM Normal CRUD or Execute Multiple did before.

This does stop the transaction, but not rollback the previous committed transaction, so it proves the concept of rollback that Custom Action has.

So, hopefully this article is not so long for you to read and you can get advantage by reading it.

I intend to document it because in case I forgot it will become our e-memory and to encourage anyone to keep using this feature for your important couple of transactions that needs rollback each other.

Thanks and have a nice day!

6 comments:

  1. Your blog is really awesome and I got some useful information from your blog. This is really useful for me. Thanks for sharing such a informative blog. Keep posting.
    AX Training | Microsoft Dynamics CRM Online Training

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. the flexibility in running this with transaction rollback have made action very useful whenever i need something done in a all-or-nothing fashion. other than plugin, this is another alternative, which i found in some scenario more direct than plugin.

    ReplyDelete
  4. Beautiful journal containing very interesting and useful information on your website. Thank you for sharing the diary and this beautiful information that facilitates our work .... We will change this message with new data.

    DedicatedHosting4u.com

    ReplyDelete
  5. Hi there, just became aware of your blog through Google, and found that it is truly informative. I am gonna watch out for brussels. I’ll be grateful if you continue this in future. Numerous people will be benefited from your writing. Cheers!
    machine learning ecommerce

    ReplyDelete

My Name is..