代码之家  ›  专栏  ›  技术社区  ›  Raj Rao

如何使用WebAPI在Dynamics Online中创建多个收件人地址的电子邮件

  •  1
  • Raj Rao  · 技术社区  · 6 年前

    我很难理解如何实例化电子邮件模板,然后使用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 )

    1 回复  |  直到 6 年前
        1
  •  1
  •   Raj Rao    6 年前
    1. 要使用电子邮件模板自动生成电子邮件内容,需要使用“ InstantiateTemplate “行动。

    该操作将如下所示的对象作为输入:

    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(实例化模板请求)

    返回的对象有两个属性:主题和说明。

    1. 要从模板创建电子邮件:

    您需要使用创建电子邮件记录 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