Tuesday, November 18, 2014

Generate a note with a PDF attachment from a CRM report

Hi guys,

it's about time to make another entry on my blog.
I have been struggling for almost 2 days to get this to work and I finally got it.

A customer of ours had a requirement to generate a report to PDF, save the report to an entity and afterwards, make a mail with the attachment.

This blog is about the first part: generating the report and save it as a note attachment.

First I Googled for a starting point. I found this one:
http://xrmmatrix.blogspot.sk/2011/06/creating-report-as-pdf-attachment-in.html

This solution made use of a html web resource. In short: you put the code in a html web resource, and place the web resource on the form. The solution shows a button to trigger the generation of the report and also makes use of an unsupported VB script.

I wanted to do the same, but without the button, without the VB script and during the OnSave of the form.

There were a couple of problems to start with: the VB script needed to be converted to JavaScript and has to work in most modern browsers (IE, Chrome, ...)

Since I'm still pretty new to CRM and web development, I'm sure there are still a couple of things that can be done better. For instance: now I provide the report name and report id hardcoded in the script. I want to modify this a bit so I only have to provide the report name and the id is retrieved from the system.

Another thing I'm not sure about: to make it work in most browsers I detect if the script is being run in Internet Explorer or something else. In IE we can use responseBody from the XMLHttpRequest. In other browsers there is no responeBody propertie for the XMLHttpRequest. In the IE method I'm making use of the VBArray function to convert the response, but I'm not 100% sure if this is comptable with every IE version.

Anyway, long story short, here is my modified code. Enjoy:

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Merci d'avoir partagé des informations utiles, précieuses et utiles. Abacus Consulting fournit un service complet de Comptable Belgique dont vous avez besoin.

    ReplyDelete