Feb 14, 2025

Programmatically create a draft Email using Email Template (C#)

Previously we discussed how to send an Email using a Email Template, but we noticed it just sends the email but no chance of create and save as a draft. In some instances we need to create the Draft email to be sent later after checking or/and modifications by the user. 

In such situations we can use below code. One can say its simply possible to use CREATE message of the Org service to achieve this but it is not possible to use a Template which is a constrain. 

By creating the draft first, it allows you to programmatically or manually attach attachments prior to sending the email.

Here InstantiateTemplateRequest  message does the magic.

 public void CreateDraftEmailToPrimaryContactOfAccount(Account account)
 {
     InstantiateTemplateRequest request = new InstantiateTemplateRequest()
     {
         TemplateId = new Guid("bf0b97c7-d5a3-4a3f-8771-a1cd737ab555"),
         ObjectId = account.Id,
         ObjectType = Account.LogicalName
     };
     InstantiateTemplateResponse response = (InstantiateTemplateResponse)OrgService.Execute(request);

     Entity email = response.EntityCollection[0];
     Entity Fromparty = new Entity("activityparty");
     Entity Toparty = new Entity("activityparty");
     Toparty["partyid"] = new EntityReference(Account.EntityLogicalName, account.PrimaryContactId.Id);
     Fromparty["partyid"] = new EntityReference("queue", new Guid("f14a45e9-fac5-4ba0-9a95-c07fe1adabf0"));
     email["from"] = new Entity[] { Fromparty };
     email["to"] = new Entity[] { Toparty };
     email["directioncode"] = true;
     email["regardingobjectid"] = new EntityReference(Account.EntityLogicalName, account.Id);
     Guid emailId = OrgService.Create(email);
}

When ready if you need to send the Email programmatically, below SendEmailRequest message can be used as below.

SendEmailRequest sendEmailRequest = new SendEmailRequest
{
     EmailId = emailId,
     TrackingToken = string.Empty,
     IssueSend = true
};
SendEmailResponse sendEmailResponse = (SendEmailResponse)OrgService.Execute(sendEmailRequest);

Related Posts
Programmatically send Email with Template (c#)

Programmatically send Email with Template (c#)

This is a code snippet that sends Email programmatically (in C#) while using an Email Template

Explained scenario, we send an Email to Primary Contact of the Account. We set Account as the regarding object of the Email so this Email activity will associate with Account and will be displayed in Account's timeline. Also we have to pass Account as the regarding object of Template since we want dynamic fields of Template to be filled with Account field values as necessary.

Actually SendEmailFromTemplateRequest message does the magic.

public void SendEmailToPrimaryContactOfAccount(Account account)
{
    Entity Fromparty = new Entity("activityparty");
    Entity Toparty = new Entity("activityparty");
    Toparty["partyid"] = new EntityReference(Contact.EntityLogicalName, account.PrimaryContactId.Id);
    Fromparty["partyid"] = new EntityReference("queue", new Guid("f14a45e9-fac5-4ba0-9a95-c07fe1adabf0"));
    Entity email = new Entity("email");
    email["from"] = new Entity[] { Fromparty };
    email["to"] = new Entity[] { Toparty };
    email["directioncode"] = true;
    email["regardingobjectid"] = new EntityReference(Account.EntityLogicalName, account.Id);

    SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest
    {
        Target = email,
        TemplateId = new Guid("bf0b97c7-d5a3-4a3f-8771-a1cd737ab555"),
        RegardingId = account.Id,
        RegardingType = Account.EntityLogicalName
    };
    var emailUsingTemplateRes = OrgService.Execute(emailUsingTemplateReq);
} 

Please note I am sending this email from a queue record. Obviously you can send from a system users as well. In such case, from activity party has to be changed accordingly.

One thing to note is, this method just sends the email, so you are not save Email in draft status.

Related Posts
Programmatically create a draft Email using Email Template (C#)

Feb 8, 2025

Very useful Formula field

Formula fields are pretty useful type in my opinion. It helps us define some values in a field using existing values, but defining them as we want. 

How to do:

Start just like you create any other field, and select Formula within the type selection.


Then intelligence will help you build your own formula. Start clicking Ctrl and Space bar to start with existing field.


Example

Suppose I have below status codes in Contact entity. 



If we need to pass instruction based on the values, we can use below Formula field to be used in Email.

Switch('Status Reason',
  Blank(), "",
 'Status Reason (Contacts)'.Inactive,"Redirect to Sales Team",
 'Status Reason (Contacts)'.'Not Verified',"Seek Approval from Manager",
 'Status Reason (Contacts)'.Verified,"Ready send Stater Pack"
)

This is just a one simple example. 

References;