Convert to Case: Displaying email contents

There are two ways to create case from an incoming email:

  1. Automatic record creation rules
  2. Convert to Case button in the email command bar

converttocase.png

Once a case is created, there may be so many other activities associated with that case and the email that was used to create the case might be all the way down in the activity feed. For quick reference you might just want to see the email content right on the case itself.

If you try to copy the email’s description field that stores the email content into a multiline text field on the case, you’ll face issues because of rich text emails. Most email sent these days are HTML emails, and Dynamics CRM doesn’t have a rich text field yet. So when you copy a rich text content into a field you will get everything i.e. content+markup. For example below is a simple email sent from outlook.

outlookemail

This is what will be copied to the text field on the case, when you copy the email entity’s description field. Now I will show you an approach to do this using a HTML webresource and Javascript. Below is the code for the HTML webresource that is embedded into the Case form.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Email Body</title>
</head>
<body>
	<h3>Case Source Email</h3>
	<div id="description"></div>
</body>
<script>
	var RYR = window.RYR || {};
	var Xrm = parent.Xrm;
	
	RYR.retrieveEntityWebApi = function(entityName,recordId,additionalCriteria) {
		var headers = new Headers({
		"Accept": "application/json",
		"Content-Type": "application/json; charset=utf-8",
		"OData-MaxVersion": "4.0",
		"OData-Version": "4.0"
		});
			
		fetch("/api/data/v8.0/"+entityName+"?$select=activityid,description&$filter=_regardingobjectid_value eq "+recordId, 
		{   method: 'GET',
			headers: headers,
			credentials: 'include' //without this header, the request will fail
		})
		.then(function(response){
			return response.json();
		})
		.then(function(c){
			if(c.value && c.value.length > 0) {
				document.getElementById('description').innerHTML = c.value[0].description;
				Xrm.Page.ui.tabs.get('general').sections.get('emailsection').setVisible(true);
			}
		})
		.catch(function(err) {
			console.log(err);
		});
	};
	
	RYR.retrieveEntityOData = function(entityName,recordId,additionalCriteria,callback) {
		var odataUrl = "/XRMServices/2011/OrganizationData.svc/"+entityName+"?$select=ActivityId,Description&$filter=RegardingObjectId/Id eq (guid'"+recordId+"')";
		if(additionalCriteria) {
			odataUrl += additionalCriteria;
		}				
		var retrieveReq = new XMLHttpRequest();
		retrieveReq.open("GET", odataUrl, false);
		retrieveReq.setRequestHeader("Accept", "application/json");
		retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
		retrieveReq.onreadystatechange = function () {
			if(callback) {
				callback(this);
			}
		};
		retrieveReq.send();
	};	
	
	var processEmailResults = function (response) {
			if (response.readyState == 4) {
				var emails = JSON.parse(response.responseText).d.results;
				if(emails.length > 0) {
					document.getElementById('description').innerHTML = emails[0].Description;
					Xrm.Page.ui.tabs.get('general').sections.get('emailsection').setVisible(true);
				}
			}
	};
	
	var retrieveEmailBody = function() {
		var recordId = Xrm.Page.data.entity.getId().substr(1,36);//Get rid of '{' and '}'
		if(!window.fetch ||
			!Xrm.Page.context.getVersion ||
			Xrm.Page.context.getVersion().split('.')[0] !== '8') {
			RYR.retrieveEntityOData('EmailSet',recordId,'&$top=1&$orderby=CreatedOn',processEmailResults);
		}
		else {
			RYR.retrieveEntityWebApi('emails',recordId,'&$top=1&$orderby=CreatedOn');
		}
	};
	
	if(Xrm.Page.ui.getFormType() === 2) retrieveEmailBody();
	
	window.RYR = RYR;
</script>	
</html>

This is how it looks on the Case form

Case

As I was just experimenting with various features, I have tried two approaches: standard XHR with the old OData endpoint and using fetch API with the Web API endpoint in Dynamics CRM 2016. I believe this is a better approach going forward. You can just include fetch polyfill and start using fetch today.

I initially didn’t want to try a code based approach. I created a Quick View Form on the email entity, stored the initial email record as a lookup field in the case and then use the Quick View Form to display the email on the case. But it didn’t display the email content.

I suspect this is because of the  warning “The email below might contain script or content that is potentially harmful and has been blocked.” displayed on the email. My theory is that once the email is tagged as unsafe by CRM, you cannot use Quick View Form to display its contents.

emailwarning

References:

  1. https://developer.mozilla.org/en/docs/Web/API/Fetch_API
  2. https://davidwalsh.name/fetch
  3. MSDN – Web API EntityType Reference
  4. MSDN – Retrieve an entity using Web API
  5. MSDN – Query data using Web API
Advertisements

13 thoughts on “Convert to Case: Displaying email contents

  1. How do I use this? I understand I need to add the webresources withe the above code, but how do I implement it? You mentioned a few steps, I’m not sure how to add the webresource and where and how to put it on the form? Finally how do I use the Fethpolyfill, do I have to?

    • There is only one webresource: html page. The code on the post is for this webresource. After you create the html webresource, you would just embed it in the CRM form using the form designer. You don’t need to use fetch if haven’t used it before. You can just use plain XrmHttpRequest.

  2. Where does the XrmHttpRequest get entered into? I have put it in the case description box in the form editor. Is this correct? Also, could you provide some type of code for the XrmHttpRequest? I am new to CRM and not the best coder so I’m just trying to follow along here. I have named the code you provided “HTMLWebResource.html” for reference.

    Thanks.

      • Hey Natraj,

        Thanks for your reply. I figured it out and have the web resource working on my case form. If I wanted to edit the look of the web resource could I do that directly in the HTML file? Or would that mess any of the coding up? Just wanting the case description box to look a bit nicer and have some uniform formatting.

        Thanks.

  3. Hi, Was able to create web resource, add it to my case form but I get an error when executing the json.parse line. The error is is “invalid character”. CRM2016 on prem.
    I put the statement in a try catch. I did put and alert also to see what was in the response and there seemed to be allot of HTML etc… could not find the text of the email body. Any idea ??
    alert(‘response text ‘ + response.responseText);
    try
    {
    var emails = JSON.parse(response.responseText).d.results;
    }
    catch (err)
    {
    alert(‘error parsing email: ‘ + err);
    }
    Thank you

  4. Do you have any advice for getting rid of the HTML tags in the actual description box provided by the CRM case form? Our IT team gets a case assignment email generated from CRM when we get a new case in the system. The email is generated by the information in the case, and because the description box contains HTML tags so does the incoming Outlook case assignment email that contains the description text.

    I have your web resource in place and I would like to keep using that. I just need some code that roughly gets rid of the tags so that they are not then passed on to the Outlook email.

    Is there a way to reference the web resource in the email template so that it draws the description for the email from that instead of the actual description box?

    Thanks for the help.

    • I created a new blank workflow that sent out an email and the body of the email was set to the description of the case. I was able to receive the rich text email properly formatted without any weird html tags, even though the description field on the case contained HTML tags. Maybe the assignment email is a plain text email? Can you please post this in the forums, so that you can share some screenshots and if this issue is resolved it will be of help to others who might face similar issues. Thank you.

      • What type of email client did you have it sent to? All of our assignment emails go to Outlook. If the assignment email was a plain text email would it contain the HTML tags when it got sent from CRM? I think the workflow we have put in place sends the assignment email after the case is generated in CRM. So it’s just simply grabbing everything that in the description box and passing it along to the designated users Outlook inbox. I know that the description box on the case form can only contain plain text which is why everyone has the HTML issues in the first place.

        I have posted this question in the CRM Dynamics forums. I’ll include links to those posts if you want to see screen shots and propose a solution.

        https://community.dynamics.com/crm/f/117/t/251290 (This is the post for the issue I described to you. It contains relevant screenshots)

        https://community.dynamics.com/crm/f/117/t/247651 (Here is my post about the initial HTML issue that you solved with this blog post in case you want to share a link to this post here to help other users)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s