Friday, May 24, 2019

SDK: Create Email Using Template

        private static Guid CreateEmailMessageFromTemplate(IOrganizationService service, EntityReference contact, Guid templateId, EntityReference fromQueue)
        {
            InstantiateTemplateRequest request = new InstantiateTemplateRequest()

            {
                TemplateId = templateId,
                ObjectId = contact.Id,
                ObjectType = contact.LogicalName
            };
            InstantiateTemplateResponse response = (InstantiateTemplateResponse)service.Execute(request);
            Entity email = response.EntityCollection[0];
            var toActivityParty = new Entity("activityparty");
            toActivityParty["partyid"] = new EntityReference("contact", contact.Id);
            EntityCollection toParty = new EntityCollection() { EntityName = "activityparty" };
            toParty.Entities.Add(toActivityParty);

            var fromActivityParty = new Entity("activityparty");
            fromActivityParty["partyid"] = fromQueue;
            EntityCollection fromParty = new EntityCollection() { EntityName = "activityparty" };
            fromParty.Entities.Add(fromActivityParty);

            email.Attributes["to"] = toParty;
            email.Attributes["from"] = fromParty;
            return service.Create(email);
        }

No comments:

Post a Comment