POPULATE EMAIL FROM RECIPIENT FIELD WITH DEFAULT VALUE: SETTIMEOUT FUNCTION
This blog aims at populating an email from recipient field with a default value when we try to create a new email activity in Dynamics CRM. Though this is not a well renowned business requirement, but there is still a small probability you can expect such a requirement from your manager.
In Dynamics CRM, whenever you try to create a new email activity or if you reply/forward to already existing email you can notice, the moment a new email draft record is created, from recipient is automatically populated with either service account user or the current users name.
As shown in the above picture, the moment we create a draft email record, the from field gets populated with a default user.
We have recently implemented service module. As part of it, when our customer sends an email to us, a case is created automatically. One of our customer service representative navigate to a case and reads the email received. When he clicks the reply button, a new draft email is created with the from recipient automatically populated with one of our service account (user record). Whenever they reply to a case email, they have to manually change the from recipient to one of our queue. My manager asked if I could automate this feature.
We came up with a solution of developing a java script to automate this feature. The script will get triggered on email form on-load event. The JS code is:
function DefaultEmailFrom() {
if (Xrm.Page.ui.getFormType() == 2 && Xrm.Page.getAttribute("statuscode").getValue() == 1)
{
if(Xrm.Page.getAttribute("regardingobjectid").getValue() != null && Xrm.Page.getAttribute("regardingobjectid").getValue()[0].entityType == "incident" &&
Xrm.Page.getAttribute("subject").getValue() !=null )
{
setTimeout(function (){
var pl = new Array();
pl[0] = new Object();
pl[0].id = "90131BD8-A61F-E711-811E-92DB54286C00";
pl[0].name = "Customer Service";
pl[0].entityType = "queue";
Xrm.Page.getAttribute("from").setValue(pl);
},1000);
}
}
}
In the above script, first I am checking if the email status reason is draft and the form type is 2 (form type IS 2 while creating a new email record). Since my business requirement to auto populate email from field only when we try to reply an email associated with a case record, I am checking if the regarding object field (which is a lookup) is not null and the type of the regarding object field is incident. Once we click the reply button, the new reply email created will obviously contains a subject. I am checking this condition in my JS if the subject is not null. Once all the conditions are met, the main algorithm is written inside a timeout function. The reason being when you click reply button, after creation of a draft email within 0.3 to 0.5 seconds CRM is populating the from field with a default user value. So if I don’t write this delay function, then the changes I make to from field will be over written by CRM. Thus I am giving atleast a 1 second delay so as to give time for CRM to complete its process of filling the from field and once it is completed my java script with a 1 second delay over writes the from field with a default queue. Inside the timeout function, all I need to do is set the from lookup field to a default queue.

Like
Report
*This post is locked for comments