Tuesday 24 January 2023

Generate report as pdf and attach - Sample

 

Send Email with attachment in CRM 2013

Hi All,

Sometimes I wonders why Microsoft don’t provide some basic & necessary functionality in CRM despite of knowing that those requirements are frequently demanded and can be achieved with minimal amount of customization. But anyways, thats where we CRM developers comes in picture.

Now the requirement was to “Generate a quote and send it to customer via email with just one click”.

When I decided to go for this requirement then I thought of 2 solutions:

  1. Generate a word document in custom workflow activity, create email activity and call this WFA via CRM workflow AND call this workflow via ribbon button using JavaScript.

Pros:

  • I can use html to prepare my word document inside custom workflow activity and attach it to email activity.
  • Manage email activity from CRM
  • Easy to maintain & modify

Cons

  • Client don’t want to send word document because client can manipulate
  1. Develop a SSRS fetchxml report using JavaScript as explained in http://xrmmatrix.blogspot.in/2011/06/creating-report-as-pdf-attachment-in.html, and create email activity using JavaScript and call the function via custom ribbon button

Pros:

  • Clients wants it this way J

Cons

  • Loads of maintenance overhead because of report modification, email activity modification in javascript
  • Slow performance because of generating pdf from ssrs report

So now I am sharing both solutions along with code.

Code for 1st option is here (Generating word document using html & C# CreateEmailWithWordDocument-https://meghshyam.files.wordpress.com/2014/12/createemailwithworddocument.doc)

 

Step 1: Prepare the word document as per your requirement

Step 2: Save the document as html

Step 3: Copy the html content by editing in notepad

Step 4: Create one custom workflow activity

Step 5: Replace the static value with dynamic values from CRM in html (see the attached code)

Step 6: Convert html string to bytes

System.Text.Encoding encoding = System.Text.Encoding.UTF8;

byte[] fileStream = encoding.GetBytes(GetHtml(QuoteId, service));

Step 7: Create attachment

Entity attachment = new Entity(“activitymimeattachment”);

attachment[“subject”] = SubjectCont;

attachment[“filename”] = “Estimate.doc”;

attachment[“mimetype”] = “application/msword”;

attachment[“body”] = Convert.ToBase64String(fileStream);

attachment[“attachmentnumber”] = 1;

attachment[“objectid”] = new EntityReference(“email”, _emailId);

attachment[“objecttypecode”] = “email”;

service.Create(attachment);

Step 8: Send the email using SendEmailRequest

Step 9: What else. It’s doneJ. Code is here.

Code for 2nd option

Step 1: First of all develop a ssrs report using fetchxml (as CRM is online). Make sure you enable the prefiltering in the query <entity name=”quote” enableprefiltering=”1″>

Step 2: Create one HTML webresource named as WebResource_SendQuote and add it on the CRM entity form

Step 3: Edit the html and Copy, paste the provided code (see the code https://meghshyam.wordpress.com/createemailwit…attachment-htm/) to the html webresource

Step 4: Create one more javascript webresource and create one function named as SendQuoteToCustomer

function SendQuoteToCustomer() {

var confirmation = confirm(“Are you sure want to continue?”);

if (confirmation == true) {

WebResource_SendQuote.createAttachment();

}

}

Step 5: Call this function from Ribbon button

There you go!!! It’s done.

Do let me know if you face any issue in implementing this solution.

Namaste ðŸ™‚

No comments:

Post a Comment

SSDT for Visual Studio (VS) 2015

In this course, we will learn to implement and build a SSRS Report in Dynamics CRM. The Microsoft’s Dynamics 365 CRM and Model Driven PowerA...