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 ðŸ™‚

Wednesday 18 January 2023

D365 CE: Get Logged in User’s Security Roles using JavaScript

 

D365 CE: Get Logged in User’s Security Roles using JavaScript


Many times we come across requirements such as show/hide ribbon buttons based on logged in user’s security role.

Earlier, we used to get security roles of logged in user at client side using Xrm.Utility.getGlobalContext().userSettings.securityRoles which used to return array of GUID value of each security role.

Now that it’s deprecated, we can use Xrm.Utility.getGlobalContext().userSettings.roles which returns collection of objects with GUID and name of each security role that is assigned to the user directly or through the teams.

Below is the script which checks if the user has certain security roles based on names and hides the ribbon button if the user doesn’t have any of those security roles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SAB.ShowHideReopenButton = function () {
    var roles = Xrm.Utility.getGlobalContext().userSettings.roles;
 
    if (roles === null) return false;
 
    var hasRole = false;
    roles.forEach(function (item) {
        if (item.name.toLowerCase() === "cs manager" || item.name.toLowerCase() === "cs administrator") {
            hasRole = true;
        }
    });
 
    return hasRole;
}

Hope it helps !!

Git Basic working

  Develop = dev   r