CRM Software Logo

Microsoft Dynamics 365 & Dynamics CRM experts provide reviews and opinions to aid professionals with the Dynamics 365 selection process.

 
 
Dianna Davis - Dynamics Objects

Populate the Email “FROM” Field with the Email Address of the User’s Queue

Email | Print

When you send an Email from Dynamics CRM, you can manually modify the from field of the outgoing email to a queue. Most probably you want emails sent by users from the service department to be sent from Service Queue and those emails sent from the sales department to be sent from the Sales Queue.

This simple JavaScript will update the” from” field of the email with the user’s queue.

To implement this solution, either follow these instructions or contact us and we will provide you the JavaScript as complete CRM solution (you only need to import the solution & publish).

First create a web resource called EmailOnLoad.

Click Settings>Customization>Customize the System and select Web Resources from the left pane

Click>New

Enter the name and display name as in the image. Select Type as Script (Jscript) and either use the Text Editor or Upload from text file the Java Scripts in this post.

untitled

Save and close

Select the Entities node, from left pane and then expand the Email Entity and open the form “Email” from the list of 3 available forms.

Click the Form Properties on the ribbon

In the Form Libraries click Add and select the EmailOnLoad web resource

In the Event Handler section select the control as Form and the event as OnLoad.

Click the Add button and select Library as EmailOnLoad

Enter function as: emailonLoad

Click OK  to save and close all opened windows

Publish and test.

untitled

JavaScript Code:

function emailonLoad()

{
var formTypeCreate=1;
var formType = Xrm.Page.ui.getFormType();
if (formType ==formTypeCreate)
{
var userid=Xrm.Page.context.getUserId();
var id=userid.replace('{',"").replace('}',"");
var trerive=retriveTeams(id);

}
}
function retriveTeams(id)
{
var reqXML="";
reqXML+="";
reqXML+=" <s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
reqXML+=" <s:Body>";
reqXML+=" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
reqXML+=" <query i:type=\"a:QueryExpression\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
reqXML+=" <a:ColumnSet>";
reqXML+=" <a:AllColumns>true</a:AllColumns>";
reqXML+=" <a:Columns xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />";
reqXML+=" </a:ColumnSet>";
reqXML+=" <a:Criteria>";
reqXML+=" <a:Conditions />";
reqXML+=" <a:FilterOperator>And</a:FilterOperator>";
reqXML+=" <a:Filters />";
reqXML+=" </a:Criteria>";
reqXML+=" <a:Distinct>false</a:Distinct>";
reqXML+=" <a:EntityName>team</a:EntityName>";
reqXML+=" <a:LinkEntities>";
reqXML+=" <a:LinkEntity>";
reqXML+=" <a:Columns>";
reqXML+=" <a:AllColumns>false</a:AllColumns>";
reqXML+=" <a:Columns xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />";
reqXML+=" </a:Columns>";
reqXML+=" <a:EntityAlias i:nil=\"true\" />";
reqXML+=" <a:JoinOperator>Inner</a:JoinOperator>";
reqXML+=" <a:LinkCriteria>";
reqXML+=" <a:Conditions />";
reqXML+=" <a:FilterOperator>And</a:FilterOperator>";
reqXML+=" <a:Filters />";
reqXML+=" </a:LinkCriteria>";
reqXML+=" <a:LinkEntities>";
reqXML+=" <a:LinkEntity>";
reqXML+=" <a:Columns>";
reqXML+=" <a:AllColumns>false</a:AllColumns>";
reqXML+=" <a:Columns xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />";
reqXML+=" </a:Columns>";
reqXML+=" <a:EntityAlias i:nil=\"true\" />";
reqXML+=" <a:JoinOperator>Inner</a:JoinOperator>";
reqXML+=" <a:LinkCriteria>";
reqXML+=" <a:Conditions>";
reqXML+=" <a:ConditionExpression>";
reqXML+=" <a:AttributeName>systemuserid</a:AttributeName>";
reqXML+=" <a:Operator>Equal</a:Operator>";
reqXML+=" <a:Values xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
reqXML+=" <b:anyType i:type=\"c:guid\" xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/\">"+id+"</b:anyType>";
reqXML+=" </a:Values>";
reqXML+=" <a:EntityName i:nil=\"true\" />";
reqXML+=" </a:ConditionExpression>";
reqXML+=" </a:Conditions>";
reqXML+=" <a:FilterOperator>And</a:FilterOperator>";
reqXML+=" <a:Filters />";
reqXML+=" </a:LinkCriteria>";
reqXML+=" <a:LinkEntities />";
reqXML+=" <a:LinkFromAttributeName>systemuserid</a:LinkFromAttributeName>";
reqXML+=" <a:LinkFromEntityName>teammembership</a:LinkFromEntityName>";
reqXML+=" <a:LinkToAttributeName>systemuserid</a:LinkToAttributeName>";
reqXML+=" <a:LinkToEntityName>systemuser</a:LinkToEntityName>";
reqXML+=" </a:LinkEntity>";
reqXML+=" </a:LinkEntities>";
reqXML+=" <a:LinkFromAttributeName>teamid</a:LinkFromAttributeName>";
reqXML+=" <a:LinkFromEntityName>team</a:LinkFromEntityName>";
reqXML+=" <a:LinkToAttributeName>teamid</a:LinkToAttributeName>";
reqXML+=" <a:LinkToEntityName>teammembership</a:LinkToEntityName>";
reqXML+=" </a:LinkEntity>";
reqXML+=" </a:LinkEntities>";
reqXML+=" <a:Orders />";
reqXML+=" <a:PageInfo>";
reqXML+=" <a:Count>0</a:Count>";
reqXML+=" <a:PageNumber>0</a:PageNumber>";
reqXML+=" <a:PagingCookie i:nil=\"true\" />";
reqXML+=" <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>";
reqXML+=" </a:PageInfo>";
reqXML+=" <a:NoLock>false</a:NoLock>";
reqXML+=" </query>";
reqXML+=" </RetrieveMultiple>";
reqXML+=" </s:Body>";
reqXML+=" </s:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", getClientURL() + "/XRMServices/2011/Organization.svc/web", false);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple");
req.onreadystatechange = function ()
{
if (req.readyState == 4 /* complete */)
{
req.onreadystatechange = null; //Addresses potential memory leak issue with IE
if (req.status == 200)
{
var guidId;
var email = req.responseXML;
if (email != null)
{
var emailAttributes = null;
if (window.navigator.appName == "Microsoft Internet Explorer" || window.navigator.appName == "Netscape")
{
if (navigator.appVersion.indexOf("Chrome") == -1)
emailAttributes=email.getElementsByTagName("a:Entities");
else
emailAttributes = email.getElementsByTagName("Entities");
}
else if (navigator.appVersion.indexOf("Chrome") != -1)
{
emailAttributes=email.getElementsByTagName("a:Entities")
}
for (i = 0; i < emailAttributes.length; i++)
{
var entity=null;
if (navigator.appVersion.indexOf("Chrome") == -1)
entity=emailAttributes[i].getElementsByTagName("a:Entity")
else
entity=emailAttributes[i].getElementsByTagName("Entity")
for(j=0;j<entity.length;j++)
{
var attr=null;
if (navigator.appVersion.indexOf("Chrome") == -1)
attr=entity[j].getElementsByTagName("a:Attributes")
else
attr=entity[j].getElementsByTagName("Attributes")
for(k=0;k<attr.length;k++)
{
try
{
var val= attr[k].childNodes[0].childNodes[0].nodeTypedValue;
var val1=attr[k].childNodes[0].childNodes[0].textContent;
if(val=="queueid")
{
if(attr.length!=0 && attr[k]!=null && attr[k].childNodes[0].childNodes[0].nodeTypedValue=="queueid")
{
var guidId=attr[k].childNodes[0].childNodes[1].childNodes[0].nodeTypedValue;
var name=attr[k].childNodes[0].childNodes[1].lastChild.text;
var qName=name.replace('<',"").replace('>',"");
lookupSetting(guidId,qName);
break;
}
}
else
{
if(attr.length!=0 && attr[k]!=null && attr[k].childNodes[0].childNodes[0].textContent=="queueid")
{
var guidId=attr[k].childNodes[0].childNodes[1].childNodes[0].textContent;
var name=attr[k].childNodes[0].childNodes[1].lastChild.textContent;
var qName=name.replace('<',"").replace('>',"");
lookupSetting(guidId,qName);
break;
}
}
}
catch(ex){}
}
}
}
}
}
}
};
req.send(reqXML);
}
function getClientURL()
{
var url=Xrm.Page.context.getClientUrl();
return url;
}
function lookupSetting(guidId,qName)
{
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id =guidId;
lookup[0].name =qName;
lookup[0].entityType = "queue";
Xrm.Page.getAttribute("from").setValue(lookup);
}

by DNN Integration

Dynamics-Object-logo

3 Responses to “Populate the Email “FROM” Field with the Email Address of the User’s Queue”

  1. Mitu says:

    Hi there,

    I did the same thing in CRM 2011 but it’s not changing the “FROM”.

    Any tips?

    Thanks

  2. Soma says:

    Use this function for CRM 2011

    function emailonLoad()
    {
    var formTypeCreate=1;
    var formType = Xrm.Page.ui.getFormType();
    if (formType ==formTypeCreate)
    {
    var userid=Xrm.Page.context.getUserId();
    var id=userid.replace(‘{‘,””).replace(‘}’,””);
    var trerive=retriveTeams(id);

    }
    }
    function retriveTeams(id)
    {
    var reqXML=””;
    reqXML+=””;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=” “;
    reqXML+=” “;
    reqXML+=”
    true“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=”
    And“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” false“;
    reqXML+=” team“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=”
    “;
    reqXML+=”
    false“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=”
    Inner“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=”
    And“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=”
    “;
    reqXML+=”
    false“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=”
    Inner“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=”
    “;
    reqXML+=”
    systemuserid“;
    reqXML+=” Equal“;
    reqXML+=” “;
    reqXML+=” “+id+”“;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=” And“;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    reqXML+=”
    systemuserid“;
    reqXML+=” teammembership“;
    reqXML+=” systemuserid“;
    reqXML+=” systemuser“;
    reqXML+=” “;
    reqXML+=” “;
    reqXML+=” teamid“;
    reqXML+=” team“;
    reqXML+=” teamid“;
    reqXML+=” teammembership“;
    reqXML+=” “;
    reqXML+=” “;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=”
    0“;
    reqXML+=” 0“;
    reqXML+=” “;
    reqXML+=”
    false“;
    reqXML+=” “;
    reqXML+=” false“;
    reqXML+=” “;
    reqXML+=” “;
    reqXML+=”
    “;
    reqXML+=” “;
    var req = new XMLHttpRequest();
    req.open(“POST”, getClientURL() + “/XRMServices/2011/Organization.svc/web”, false);
    req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
    req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
    req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple”);
    req.onreadystatechange = function ()
    {
    if (req.readyState == 4 /* complete */)
    {
    req.onreadystatechange = null; //Addresses potential memory leak issue with IE
    if (req.status == 200)
    {
    var guidId;
    var email = req.responseXML;
    if (email != null)
    {
    var emailAttributes = null;
    if (window.navigator.appName == “Microsoft Internet Explorer” || window.navigator.appName == “Netscape”)
    {
    if (navigator.appVersion.indexOf(“Chrome”) == -1)
    emailAttributes=email.getElementsByTagName(“a:Entities”);
    else
    emailAttributes = email.getElementsByTagName(“Entities”);
    }
    else if (navigator.appVersion.indexOf(“Chrome”) != -1)
    {
    emailAttributes=email.getElementsByTagName(“a:Entities”)
    }
    for (i = 0; i < emailAttributes.length; i++)
    {
    var entity=null;
    if (navigator.appVersion.indexOf("Chrome") == -1)
    entity=emailAttributes[i].getElementsByTagName("a:Entity")
    else
    entity=emailAttributes[i].getElementsByTagName("Entity")
    for(j=0;j<entity.length;j++)
    {
    var attr=null;
    if (navigator.appVersion.indexOf("Chrome") == -1)
    attr=entity[j].getElementsByTagName("a:Attributes")
    else
    attr=entity[j].getElementsByTagName("Attributes")
    for(k=0;k<attr.length;k++)
    {
    try
    {
    var val= attr[k].childNodes[2].childNodes[0].textContent;
    //var val1=attr[k].childNodes[0].childNodes[0].textContent;
    if(val=="queueid")
    {
    if(attr.length!=0 && attr[k]!=null && attr[k].childNodes[2].childNodes[0].nodeTypedValue=="queueid")
    {
    var guidId=attr[k].childNodes[2].childNodes[1].childNodes[0].nodeTypedValue;
    var name=attr[k].childNodes[2].childNodes[1].lastChild.text;
    var qName=name.replace('’,””);
    lookupSetting(guidId,qName);
    break;
    }

    else
    {
    if(attr.length!=0 && attr[k]!=null && attr[k].childNodes[2].childNodes[0].textContent==”queueid”)
    {
    var guidId=attr[k].childNodes[2].childNodes[1].childNodes[0].textContent;
    var name=attr[k].childNodes[2].childNodes[1].lastChild.textContent;
    var qName=name.replace(”,””);
    lookupSetting(guidId,qName);
    break;
    }
    }
    }
    }
    catch(ex){}
    }
    }
    }
    }
    }
    }
    };
    req.send(reqXML);
    }
    function getClientURL()
    {
    var url=Xrm.Page.context.getClientUrl();
    return url;
    }
    function lookupSetting(guidId,qName)
    {
    var lookup = new Array();
    lookup[0] = new Object();
    lookup[0].id =guidId;
    lookup[0].name =qName;
    lookup[0].entityType = “queue”;
    Xrm.Page.getAttribute(“from”).setValue(lookup);
    }

  3. Brian says:

    I tried this using CRM 2016 Update 1 but it did not seem to work. Do you have an updated version?

Ask This Expert a Question / Leave a Comment

 

 
 
Show Buttons
Hide Buttons