Tuesday, April 17, 2012

Changing the reply email address to the queue's email address when replying from a queue in crm 2011

The problem was that we have multiple queues with email addresses in crm 2011. When an email arrives it goes to a particular queue depending on the address that was used. Now what happens when you try to reply to that email? Well nothing much only that the "FROM address" lookup defaults to the currently logged on user instead of defaulting to the queue's email address.
There are various ways you can solve this issue by writing plugin or workflow custom activity etc, but the crux of the matter is that the crm user replying from a queue needs to the see what the from email address is.
This can be best achieved by using Java script.
Please note that the code below is an unsupported customization, but it works.
function CheckEnquiryReplyAddress() {
 // Only complete this validate on Create Form
 var formType = Xrm.Page.ui.getFormType();
 var emailStatus = GetAttributeValue("statecode");
                    var emailDirection = GetAttributeValue("directioncode");



 if (formType == 1 || (formType == 2 && emailStatus == "Open")) {  
                                             
                                       
  if (emailDirection == "1"){
                                                              var previousEmailId=getExtraqsParam("_InReplyToId", window.parent.location.search);

   //getting context from the parent window
   var context = Xrm.Page.context;

   try {
    var serverUrl = context.getServerUrl();
    //The XRM OData end-point
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
                                                                                     var query="/EmailSet?$select=ActivityId,ActivityTypeCode,DirectionCode,";
                                                                                     query=query+"ToRecipients,Email_QueueItem/QueueId&$expand=Email_QueueItem&$filter=ActivityId eq guid'" + previousEmailId +"'";
    query =serverUrl+ODATA_ENDPOINT+ query;

    var request= new XMLHttpRequest();
    request.open("GET", query, false);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    request.onreadystatechange=function(){ CompleteEnquiryReplyCheck(request,serverUrl);}
    request.send(null);
   }
   catch(e) {
    alert(e.Description);
   }
  }
 }

          
}
function CompleteEnquiryReplyCheck(request,url)
{
 if (request.readyState==4) {
  if(request.status==200) {
   var queue=JSON.parse(request.responseText).d.results[0];
   
   if (queue != null) {
    var queueId = queue.Email_QueueItem.results[0].QueueId.Id;
    var lookup = new Array();
      var lookupItem = new Object();
  
    lookupItem.id = queueId;
    lookupItem.name = queue.Email_QueueItem.results[0].QueueId.Name;
    lookupItem.typename = "queue";
     
    lookup[0] = lookupItem;
  
    Xrm.Page.getAttribute("from").setValue(lookup);
   }
  }
    }
}
The key here is the _InReplyToId query string parameter from here we are able to query the oData service and get the "To" email address of the original email and then it is simply a matter of putting the "To" address in the "From" lookup. Also note that I am checking that this is an outgoing email. Find the code for getExtraqsParam here. Happy Crming!.

12 comments:

  1. Is this accomplished by adding the java script as a we resrouce and then adding the web resource to the email form onload event?

    ReplyDelete
  2. Replies
    1. Great, I noticed that this only works if you hit reply. What would I need to change to have it work if someone replys to all.

      Delete
  3. Would this still apply if using the CRM Outlook Client? or would i need to use a workflow instead?

    ReplyDelete
    Replies
    1. I haven't tested in outlook but give it a try. It should work.

      Delete
  4. This looks like exactly what I need, which is fantastic thankyou.

    I'm having an issue with the event handler for the OnLoad event though.

    There are 2 functions in the code above, which one do you call with the event?
    Also which parameters do you pass?

    Sorry, I am just taking my first steps with using javascript for CRM customisation.

    ReplyDelete
    Replies
    1. Hey, did you figure this out. I tried to use this code as onLoad event, but no luck.

      Delete
  5. Very helpful! Thank you. Needed to add json2 from sdk to form first since parse was failing.

    ReplyDelete
  6. What changes are needed to make this work with Reply to All and not just Reply.

    Thanks

    ReplyDelete
  7. Does this work with CRM 2013?

    ReplyDelete
  8. This does not work with CRM 2013 as the window.parent.location.search argument ends up being undefined

    ReplyDelete
  9. I tried using this code but I am getting error: Object doesn't support property or method getAttributeValue.

    Any help?

    ReplyDelete