Thursday, December 19, 2013

Send email with PDF attachment

Frequently there is a need to send out an email from CRM containing a generated PDF attachment. the following approach can be used to allow an end user to manually initiate the process using the click of a button via the user interface.

But what if we wanted this to happen automatically rather than relying on the user to manually execute the action? From my experience this is by far a more common scenario. A good example might be auto-generating an invoice and then emailing it to a customer once an invoice has been approved. Another one might be to generate a quote and have it sent off once the quote has been approved. The natural solution that we can look to in such a scenario would be to leverage the CRM workflow capability.

The first step would be to design some kind of report that would be used as the template for generating the PDF document. Once this is in place we can leverage the technique outlined in my previous post for publishing the report externally and making it context sensitive which is a key requirement.

This allows us to design a workflow where we can construct a URL that will produce a report for the record in question. For example, we can design a workflow as follows:

  1. Generate GUID for the record against which the workflow is running (using the workflow productivity tools)
  2. Create the URL passing in the GUID generated above as a dynamic parameter i.e. http://crm2011db01/ReportServer/Pages/ReportViewer.aspx?%2fCRMEZE2011TEST_MSCRM%2fUser+Summary&CRM_FilteredSystemUser=select  [systemuser0].*  from  FilteredSystemUser as systemuser0  where  (systemuser0.systemuserid = N'%7B{0}%7D')&rs:Command=Render    [%7B and %7D are decodes for the { and }. And {0} is the parameter to be replaced by Param 1]
  3. Send email using the link generated above

Finally, you can also pass in the same URL and use the technique mentioned in this post to convert the report to a PDF and send as an attachment via email.

Note: Make sure that the service account under which the the asyncronous service is running has access to the SSRS report folder containing the externally published CRM report.

No comments:

Post a Comment