Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Send Email to the Team members ms crm javascript

Posted on by 225

Hello Experts,

Below, is my javscript code ,email not send to the team members, from team to all Team members by using the below please suggest me.

function teamMember()
{
debugger;

//var defaultqueue = Xrm.Page.getAttribute.getValue("new_defaultqueue");
var Userlookup = Xrm.Page.getAttribute("new_defaultqueue");

if (Userlookup != null)
{
lookUpObjectValue=Userlookup.getValue();
if ((lookUpObjectValue != null))
{
var defaultqueue = lookUpObjectValue[0].id.slice(1, -1);
var sampleGuid = GetAllTeamUsers(defaultqueue);

}
}
}

function GetAllTeamUsers(defaultqueueId)
{
debugger;
var listdata = new Array();
var arrteamid = new Array();
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"+
"<entity name='systemuser'>"+
"<attribute name='fullname' />"+
"<attribute name='businessunitid' />"+
"<attribute name='title' />"+
"<attribute name='address1_telephone1' />"+
"<attribute name='positionid' />"+
"<attribute name='systemuserid' />"+
"<order attribute='fullname' descending='false' />"+
"<link-entity name='teammembership' from='systemuserid' to='systemuserid' visible='false' intersect='true'>"+
"<link-entity name='team' from='teamid' to='teamid' alias='ac'>"+
"<filter type='and'>"+
"<condition attribute='teamid' operator='eq' uiname='light19' uitype='team' value='"+defaultqueueId+"' />"+
"</filter>"+
"</link-entity>"+
"</link-entity>"+
"</entity>"+
"</fetch>";
var encodedFetchXml = encodeURI(fetchXml);
var queryPath = "/api/data/v9.1/systemusers?fetchXml=" + encodedFetchXml;
var requestPath = Xrm.Page.context.getClientUrl() + queryPath;
var req = new XMLHttpRequest();
req.open("GET", requestPath, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
debugger;
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var returned = JSON.parse(this.responseText);
var results = returned.value;
if(results.length > 0)
{
for (var i = 0; i < results.length; i++)

{
teamid = results[i]["systemuserid"];
arrteamid.push(teamid)
//listdata = listArray(arrteamid);
}
}
if(results.length = arrteamid.length)
{
//GetIds();
CreateEmail(arrteamid,defaultqueueId);

}
}
}
};
req.send();

}


function CreateEmail(arrteamid,defaultqueueId) {
debugger;
// var fromFieldIdSystemAdmin = "53536B3D-4D71-4FC2-857C-0477750A92BE"; //todo Email from User GUID
//var toPartyUsersId = ["53536B3D-4D71-4FC2-857C-0477750A92BE"]; //todo Email To user array
var quoteId = Xrm.Page.data.entity.getId();
quoteId = quoteId.replace(/[{}]/g, "");
var customerLookUp = Xrm.Page.getAttribute("customerid");
var accountName = null;
if (customerLookUp != null)
accountName = customerLookUp.getValue()[0].name;
var webapiPath = Xrm.Page.context.getClientUrl() + "/api/data/v9.1/emails";
var email = {};
//Lookup
//email["regardingobjectid_quote@odata.bind"] = "/quotes(" + quoteId + ")"; //Regarding is quote
email["subject"] = "Quote Sent ";
email["description"] = "See attachment for the Quote that was set as Won";
var parties = [];
//ActivityParty (From)
var sender = {};
sender["partyid_systemuser@odata.bind"] = "/team(" + defaultqueueId + ")";
sender["participationtypemask"] = 1; //From
parties.push(sender);
for (i = 0; i < arrteamid.length; i++) {
var receiver = {};
receiver["partyid_systemuser@odata.bind"] = "/systemusers(" + arrteamid
[i] + ")";
receiver["participationtypemask"] = 2; //To
parties.push(receiver);
}
email["email_activity_parties"] = parties;
var service = new XMLHttpRequest();
service.open("POST", webapiPath, false);
service.setRequestHeader("Accept", "application/json");
service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
service.setRequestHeader("OData-MaxVersion", "4.0");
service.setRequestHeader("OData-Version", "4.0");
service.send(JSON.stringify(email));////Getting error in this line
if (service.readyState == 4) {
if (service.status == 204) {
var uri = service.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var emailGblId = matches[1];
SendEmail(emailGblId);
} else {

Xrm.Utility.alertDialog(this.statusText);
}
}
}

function SendEmail(emailGblId) {
debugger;
var parameters = {};
parameters.IssueSend = true;
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/emails(" + emailGblId + ")/Microsoft.Dynamics.CRM.SendEmail", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(parameters));
}

function successCallback() {
}
function errorCallback() {
}

  • Suggested answer
    manojd  Profile Picture
    manojd 1,397 on at
    RE: Send Email to the Team members ms crm javascript

    Hi,

    I would recommend to use the Dynamics 365 Workflow tools which has this feature as a part of workflow where you can select the email template & team name to send the email to all team members of selected team, you can follow the link mentioned below to understand the feature & also can download the same as Published solution or source code in case you need to change the behavior 

    https://github.com/demianrasko/Dynamics-365-Workflow-Tools/blob/master/docs/Email%20To%20Team.md

    Hope this helps

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: Send Email to the Team members ms crm javascript

    Is there any limitation while using plugin, workflow or custom workflow to send email in your requirement ?

    I would suggest you to call custom action through JS and create & send email through c# code. It is better to send email using c# code rather than using JS, even you can use email template require in case of c# code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans