Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create email in Workflow (but do not send it) using a Template

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I need to Create an Email in a Workflow but do not send it, since I need to add dynamic recipients to that email. The problem is I need to Create that Email using a Template. Do you know how can I accomplish that?

Thank you.

*This post is locked for comments

  • Pawar Pravin  Profile Picture
    Pawar Pravin 5,231 on at
    RE: Create email in Workflow (but do not send it) using a Template

    Please mark as verified if the answer is helpful so that it could be helpful for others as well

  • RE: Create email in Workflow (but do not send it) using a Template

    Thanks Pravin. This helped.

    Thanks once gain for swift response

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,231 on at
    RE: Create email in Workflow (but do not send it) using a Template

    Hi Ashwini,

    Please refer below code for your reference:

    string emailTemplate = "Email template name here";

           FilterExpression filter = new FilterExpression();

           ConditionExpression conditionTitle = new ConditionExpression();

           ConditionExpression conditionType = new ConditionExpression();

           QueryExpression query = new QueryExpression();

           conditionTitle.AttributeName = "title";

           conditionTitle.Operator = ConditionOperator.Equal;

           conditionTitle.Values.Add(emailTemplate);

           conditionType.AttributeName = "templatetypecode";

           conditionType.Operator = ConditionOperator.Equal;

           conditionType.Values.Add("systemuser");

           filter.FilterOperator = LogicalOperator.And;

           filter.Conditions.Add(conditionTitle);

           filter.Conditions.Add(conditionType);

           query.Criteria = filter;

           query.ColumnSet = new ColumnSet();

           query.EntityName = "template";

           query.ColumnSet.Columns.Add("templateid");

           query.ColumnSet.Columns.Add("subject");

           query.ColumnSet.Columns.Add("title");

           EntityCollection templateCollection = service.RetrieveMultiple(query);

           tracingService.Trace("templateCollection retrived ... " + templateCollection.Entities.Count.ToString() + Environment.NewLine);

           Guid templateId = (templateCollection.Entities.Count > 0) ? (Guid)templateCollection[0].Id : Guid.Empty;

           Entity template = new Entity();

           InstantiateTemplateRequest request = new InstantiateTemplateRequest();

           request.RequestName = "InstantiateTemplate";

           request["TemplateId"] = templateId;

           request["ObjectId"] = userId;

           request["ObjectType"] = "systemuser";

           InstantiateTemplateResponse response = (InstantiateTemplateResponse)service.Execute(request);

           EntityCollection templateCollectionnew = (EntityCollection)response["EntityCollection"];

           tracingService.Trace("templateCollectionnew retrived ... " + templateCollectionnew.Entities.Count.ToString() + Environment.NewLine);

           Entity fromParty = new Entity("activityparty");

           fromParty["partyid"] = new EntityReference("systemuser", userId);

           EntityCollection collToParty = new EntityCollection();

           Entity User = service.Retrieve("systemuser", userId, new ColumnSet("internalemailaddress"));

           Entity toParty = new Entity("activityparty");

           string EmailAddress = User.Attributes.Contains("internalemailaddress") ? (string)User.Attributes["internalemailaddress"] : string.Empty;

           if (EmailAddress != string.Empty)

           {

               toParty["addressused"] = EmailAddress;

               toParty["partyid"] = new EntityReference("systemuser", User.Id);

               toPartyArray.Add(toParty);

           }

           collToParty.Entities.Add(toParty);

           Entity Email = new Entity("email");

           Email.Attributes["from"] = new Entity[] { fromParty };

           Email.Attributes.Add("to", collToParty);

           Email.Attributes["subject"] = subject;

           Email.Attributes["regardingobjectid"] = new EntityReference("incident", Complaint.Id);

           Email.Attributes["description"] = description;

           Email.Attributes["ownerid"] = new EntityReference("systemuser", userId);

           Guid NewEmail = service.Create(Email);

  • RE: Create email in Workflow (but do not send it) using a Template

    Thanks Ravi for the response. Yes I did check that link, Alex link is about sending an email with template, I am looking for creating an email with template.

    Many Thanks

    Ashiwni

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Create email in Workflow (but do not send it) using a Template

    Hi AshwiniShilpa,

    Did you check the link shared above by Alex?

  • AshwiniShilpa Profile Picture
    AshwiniShilpa 30 on at
    RE: Create email in Workflow (but do not send it) using a Template

    Hello team,

    Could you please share custom workflow code to create email and associate template and use that in OOB workflow. This is needed urgently.

    Many Thanks

    Ashwini

  • Suggested answer
    RE: Create email in Workflow (but do not send it) using a Template

    This post can help you accomplish this task.

    community.dynamics.com/.../894320

  • RE: Create email in Workflow (but do not send it) using a Template

    Vicent Rico 

    Hi, did you accomplished this? I have the same scenario but having hard time on having my data fields in the template populated with values.

    I just posted this in the community also.

    community.dynamics.com/.../894320

  • Suggested answer
    prt33k Profile Picture
    prt33k 6,907 on at
    RE: Create email in Workflow (but do not send it) using a Template

    This wont be possible using OOB workflow since you dont have to send the email.

    Create a custom workflow which will create email with template and there you can populate the dynamics recipients as well.

    blogs.msdn.microsoft.com/.../send-mail-to-team-using-custom-workflow-activities-in-crm-20132015

    Thanks,

    Prateek

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,477 on at
    RE: Create email in Workflow (but do not send it) using a Template

    Hi,

     I think you'll need a custom workflow activity which will use InstantiateTemplateRequest:

    msdn.microsoft.com/.../hh210212.aspx

     You will have that email in the EntityCollection of the response:

    msdn.microsoft.com/.../microsoft.crm.sdk.messages.instantiatetemplateresponse.aspx

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans