我很难弄清楚如何实例化电子邮件模板,然后使用WebApi创建具有多个收件人地址的电子邮件。
我遇到了许多帖子,其中一些是针对较早版本的CRM的,或者他们使用了C#。此问答向您展示了通向工作代码之旅的顶峰。
这些是我引用的一些帖子: Create an email activity using REST Endpoints in CRM2011-2013
Dynamics 365 Web API Email send(具体答案是:https://stackoverflow.com/a/47455785/44815)
答案 0 :(得分:1)
该操作将如下所示的对象作为输入:
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"
};
}
};
然后可以将其传递给:
Xrm.WebApi.online.execute(instantiateTemplateRequest)
返回的对象具有2个属性:主题和描述。
您需要使用CreateRecord method创建电子邮件记录 它以以下类型的对象作为输入:
var activityParties = [];
activityParties.push({
participationtypemask : participationTypeMasks.From,
"partyid_queue@odata.bind" : "/queues("+ queueId+ ")"
});
//setup 2 send-to addresses
activityParties.push({
participationtypemask : participationTypeMasks.To,
"partyid_account@odata.bind" : "/accounts(" + accountIdTo1 + ")"
});
activityParties.push({
participationtypemask : participationTypeMasks.To,
"partyid_account@odata.bind" : "/accounts(" + accountIdTo2 + ")"
});
//examples of using contacts
// activityParties.push({
// participationtypemask : participationTypeMasks.To,
// "partyid_contact@odata.bind" : "/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,
// "partyid_systemuser@odata.bind" : "/systemusers("+currentUserId+")"
// });
var email = {
subject: emailTemplate.subject,
description: emailTemplate.description,
email_activity_parties: activityParties,
"regardingobjectid_incident@odata.bind" : "/incidents(" + incidentId + ")"
};
返回值只是创建的记录的entityId。
我在https://github.com/rajrao/CRM-Tools/tree/master/JavaScript/CreateEmail
上提供了完整的代码示例。