Complete a transaction and throw exception in CRM Plug-in
Recently I was asked a question in my blog on how to achieve below scenario in plug-in.
- User try to save Account record by providing data in multiple fields.
- Create Plug-in should check whether any other Account already exists in CRM with provided data.
- If match found, update the existing record and display “Cannot save the Account as Account with data already exists”
- If no match found, create an Account.
Challenge:
- If ‘Account’ match found, system should update matched Account and at the same time throw message “Cannot save the Account as Account with data already exists”.
- To display message Plugin must throw Execution with message which would roll back the update operation.
Proposed Design:
- Register a Plug-in on “PreCreateAccount” with following logic, if matching ‘Account’ already exists with input data.
- To complete update operation,
- Use “ExecuteMultipleRequest” with option “ContinueOnError = true” which completes transaction.
- Instantiate ‘UpdateRequest’ and execute using “ExecuteMultipleRequest”
- In next line, throw exception with message
Plug-in Code:
// Get the matched Account
Entity matchedAccount = new Entity(“account”);
UpdateRequest reqUpdate = new UpdateRequest();
reqUpdate.Target = matchedAccount;// Use this to complete the update operation even on an exception
ExecuteMultipleRequest reqMultiple = new ExecuteMultipleRequest(){
Requests = new OrganizationRequestCollection(),
Settings = new ExecuteMultipleSettings(){
ContinueOnError = true,
ReturnResponses = false
}
};// Add Update Request to Multiple Request.
reqMultiple.Requests.Add(reqUpdate);// Trigger the Execute Multiple.
crmOrgService.Execute(reqMultiple);// Use this to display message to user
throw InvalidPluginExecutionException(“Cannot save the Account as Account with data already exists”);
🙂
Hi Rajeev,
Thanks for your Post.
Requirement 1: I am looking for the below scenario.(Just a scenario to understand the plugin execution and not real time requirement)
Suppose if i try to create three request in a single plugin (Suppose a plugin fire on post create of some X entity)
1) Creation of Account – create an object with some attributes added
2) Creation of Contact -create an object with some attributes added
3) Creation of Case-create an object with some attributes, without adding customerid
Here in OOB, for Case creation customerid is mandatory and that is not added in the Case object.
Now i want the account and contact to be created without rollback and the exception for case in realtime time.
Requirement 2 : from the below blog, It is mentioned that if we keep request in transaction then it will be rollback if any issue.
But when i tried the requirement 1 with the simple service.create(object) for 1,2 & 3 the entire transaction is getting rollback, Is the transaction process got changed from 2016 ??? or it is the same from 2011 ??
If it is the same from 2011??? where the below blog post is useful.
http://www.magnetismsolutions.com/blog/ahmed-anwar's-blog/2015/08/07/executing-messages-in-a-single-transaction-in-dynamics-crm-2015
As you are a CRM expert Could you please throw some light to understand the transaction process clearly with a real time example and also the difference between versions as well if any.
Hi Rajeev,
Thanks for your Post.
Requirement 1: I am looking for the below scenario.(Just a scenario to understand the plugin execution and not real time requirement)
Suppose if i try to create three request in a single plugin (Suppose a plugin fire on post create of some X entity)
1) Creation of Account – create an object with some attributes added
2) Creation of Contact -create an object with some attributes added
3) Creation of Case-create an object with some attributes, without adding customerid
Here in OOB, for Case creation customerid is mandatory and that is not added in the Case object.
Now i want the account and contact to be created without rollback and the exception for case in realtime time.
Requirement 2 : from the below blog, It is mentioned that if we keep request in transaction then it will be rollback if any issue.
But when i tried the requirement 1 with the simple service.create(object) for 1,2 & 3 the entire transaction is getting rollback, Is the transaction process got changed from 2016 ??? or it is the same from 2011 ??
If it is the same from 2011??? where the below blog post is useful.
http://www.magnetismsolutions.com/blog/ahmed-anwar's-blog/2015/08/07/executing-messages-in-a-single-transaction-in-dynamics-crm-2015
As you are a CRM expert Could you please throw some light to understand the transaction process clearly with a real time example and also the difference between versions as well if any.
Hi Rajeev,
Thanks for your Post.
Requirement 1: I am looking for the below scenario.(Just a scenario to understand the plugin execution and not real time requirement)
Suppose if i try to create three request in a single plugin (Suppose a plugin fire on post create of some X entity)
1) Creation of Account – create an object with some attributes added
2) Creation of Contact -create an object with some attributes added
3) Creation of Case-create an object with some attributes, without adding customerid
Here in OOB, for Case creation customerid is mandatory and that is not added in the Case object.
Now i want the account and contact to be created without rollback and the exception for case in realtime time.
Requirement 2 : from the below blog, It is mentioned that if we keep request in transaction then it will be rollback if any issue.
But when i tried the requirement 1 with the simple service.create(object) for 1,2 & 3 the entire transaction is getting rollback, Is the transaction process got changed from 2016 ??? or it is the same from 2011 ??
If it is the same from 2011??? where the below blog post is useful.
http://www.magnetismsolutions.com/blog/ahmed-anwar's-blog/2015/08/07/executing-messages-in-a-single-transaction-in-dynamics-crm-2015
As you are a CRM expert Could you please throw some light to understand the transaction process clearly with a real time example and also the difference between versions as well if any.