Reputation: 9138
I had a hard time figuring out how to instantiate an email template and then create an email with multiple to addresses using WebApi.
I came across many posts some of them targeting older versions of CRM or they used C#. This q&a shows you the culimination of that journey to working code.
These are some of the posts that I referenced: Create an email activity using REST Endpoints in CRM2011-2013
Dynamics 365 Web API Email send (specifically this answer: https://stackoverflow.com/a/47455785/44815)
Upvotes: 1
Views: 1274
Reputation: 9138
The action takes as input an object that looks like this:
var instantiateTemplateRequest = {
TemplateId: templateId,
ObjectType: objectType,
ObjectId: objectId,
getMetadata: function () {
return {
boundParameter: null,
parameterTypes: {
"TemplateId": {
"typeName": "Edm.String",
"structuralProperty": 1
},
"ObjectType": {
"typeName": "Edm.String",
"structuralProperty": 1
},
"ObjectId": {
"typeName": "Edm.String",
"structuralProperty": 1
}
},
operationType: 0,
operationName: "InstantiateTemplate"
};
}
};
Which can then be passed to:
Xrm.WebApi.online.execute(instantiateTemplateRequest)
The returned object has 2 properties: subject and description.
You need to create an email record using the CreateRecord method It takes as input an object of the following type:
var activityParties = [];
activityParties.push({
participationtypemask : participationTypeMasks.From,
"[email protected]" : "/queues("+ queueId+ ")"
});
//setup 2 send-to addresses
activityParties.push({
participationtypemask : participationTypeMasks.To,
"[email protected]" : "/accounts(" + accountIdTo1 + ")"
});
activityParties.push({
participationtypemask : participationTypeMasks.To,
"[email protected]" : "/accounts(" + accountIdTo2 + ")"
});
//examples of using contacts
// activityParties.push({
// participationtypemask : participationTypeMasks.To,
// "[email protected]" : "/contacts(00000000-0000-0000-0000-000000000000)"
// });
//examples of using the current user as the from address
// var currentUserId = Xrm.Page.context.getUserId().replace("}", "").replace("{", "");
// activityParties.push({
// participationtypemask : participationTypeMasks.From,
// "[email protected]" : "/systemusers("+currentUserId+")"
// });
var email = {
subject: emailTemplate.subject,
description: emailTemplate.description,
email_activity_parties: activityParties,
"[email protected]" : "/incidents(" + incidentId + ")"
};
The return is just the entityId of the record that got created.
I have complete sample of the code available at: https://github.com/rajrao/CRM-Tools/tree/master/JavaScript/CreateEmail
Upvotes: 1