CRM Plugin - Parent and Child Pipeline

This concept tends to confuse people often - and I was no exception. I came across this problem when trying to create custom auto-numbering for one of my projects.

Creating auto-number for Opportunity is easy. This is because the user creates opportunity straight from the home screen - thus the plugin runs on parent pipeline. However, it's a different story if you are dealing with Quote entity.

Quote can be created in 2 ways, through the home screen OR the opportunity screen. If you create it from the latter, it will run on the child pipeline and you will find that your plugin will not work.

Registering it as a child pipeline is easy, but CRM makes it harder since you can't use ICRMService. Instead, you have to create CRMService manually by using this piece of code:



Not only that, with the child plugin you can't use Query expression to fetch other entities in your code unless you register it as ASYNCHRONOUS plugin. Mark Kovalcson in his blog found out that:

A CrmService running Synchronously in a child pipeline is limited to the following:

Create
Delete
Update
RetrieveExchangeRate


However a child plugin running in Asynchronous Execution Mode is not limited this way.

Those 4 allowed actions are hard coded by MS whenever the plugin is executed inside a transaction, and it appears that all synchronous child pipeline events occur inside a transaction.

However you can do a query in child pipeline if it is registered as asynchronous.

In review:

Use ICrmService for all parent pipelines when at all possible.
Use CrmService for child pipelines in asynchronous execution mode.
There is very limited functionality allowed with CrmService for any plugin registered in a synchronous child pipeline
Querying CRM data in a plug-in registered as a synchronous Pre Create in a child process is currently very unsupported. (ie. direct SQL access)


In the end, if you want to generate a custom auto-number for QuoteNo, you have to create a child post create plugin and register it as asynchronous (in order to query the quote and update the entity). The code is shown below:



In this case I'm creating quote with the format SQ00001. The rest of the related code is below:





Note that for autonumbering:
  • I created new custom entity new_autonumber
  • Create 2 attributes : new_name and new_autonumbervalue
  • Create new record. "Quote" as new_name, 0 as new_autonumbervalue

I hope you find this useful. People who think the CRM autoprefix is ugly will agree with me :)

Andreas

Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

Sitecore custom publish agent from specific node and at a specific time