Search code examples
c#javascriptasp.netwcfdynamics-crm-4

Best approach to execute stored procedures from within CRM using a Custom Button


I am looking for some advice. We have multiple systems that sync from Microsoft Dynamics 4 (CRM) on a nightly basis. However I am often asked to manually execute the SP during the day as the sync is sometimes required immediately. This SP is on a different server to CRM.

I am wanting to add a custom button to CRM that allows the user to sync CRM to other systems themselves if they need it immediately. I do not want this to be a Workflow that occurs every time a record is updated, it will be the a user decision.

I have done some Googling and I'm at a crossroads as to how best to approach this.. 1) Executing a service from the button's javascript or 2) Using a Workflow to execute an SSIS and calling this from the button.

If I use Option 1, what would be the best approach to creating the web service? Baring in mind that the Sync procedure can take a fair few minutes to run depending on internet speed between sites, I have to be careful of timeouts. If I use a .Net 3.5 asmx web service, is there any way to immediately respond, and run the SP in the background? I know I can call this from Javascript. I believe the alternative is to use a WCF Workflow Service (which is completely new to me) which will allow for background processing - but can this be called from Javascript?

If I use Option 2, how do I execute a local SSIS from CRM? I've read it's possible, but I was unable to find an example.

Sorry for the wall of text. Any guidance or recommendations on approach would be greatly appreciated. Many Thanks.

TL;DR: I am looking for some advice on how to execute stored procedures on a different database in Dynamics 4 using a Custom Button.


Solution

  • You would have to create a web service. It can be a WCF service or classic ASMX service. Deploying it on the CRM server will be slightly complex but it can be done (deploy the application within ISV folder and then modify your application's web.config file so that it restores all the custom stuff CRM sets in its own configuration). If you have a separate web server (or at least a separate web site on the CRM server) it will be much easier.

    The service has to be marked as OneWay - so that it returns to the caller as soon as it is invoked. Unfortunately it also means that you won't be able to return error messages - those will have to be written in a log.

    Now create a button in the CRM form and call the service using JavaScript. You should be able to configure your service so that it can be invoked by passing any arguments in the URL parameters.