Skip to main content
Answer

Create Action to Preview Email before sending

  • March 30, 2023
  • 3 replies
  • 205 views

Forum|alt.badge.img

Hello,

 

I am working on creating an Action to preview and send an email created from an email template. I have the email template created and now I want to add an action to a page to call the template and show a preview where the user can edit the email if necessary. However, I am not finding anything on this.

 

Any help is appreciated.

Thanks

Best answer by darylbowman

This will open a new window with a blank email. I’m not actually sure how to load a template, but you could probably look through the source code to find somewhere where that is done.

public PXAction<ARInvoice> action;

[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "New Email")]
public void Action()
{
var emailGraph = PXGraph.CreateInstance<CREmailActivityMaint>();
emailGraph.Message.Current = new CRSMEmail();

throw new PXRedirectRequiredException(emailGraph, true, "");

}

 

3 replies

darylbowman
Captain II
Forum|alt.badge.img+15
  • Answer
  • April 3, 2023

This will open a new window with a blank email. I’m not actually sure how to load a template, but you could probably look through the source code to find somewhere where that is done.

public PXAction<ARInvoice> action;

[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "New Email")]
public void Action()
{
var emailGraph = PXGraph.CreateInstance<CREmailActivityMaint>();
emailGraph.Message.Current = new CRSMEmail();

throw new PXRedirectRequiredException(emailGraph, true, "");

}

 


Forum|alt.badge.img
  • Author
  • Freshman I
  • April 3, 2023

@darylbowman 

Thanks!

 

This worked.


  • Freshman I
  • May 29, 2025

Hi, I saw this question and I want to share what I did if someone need it, the only problem that I had was that I had to do a manual merge of the email variables because I was not able to acces to the TemplateNotificationGenerator class:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PX.Data;
using PX.Data.EP; // Para MailAccountManager
using PX.Objects.CR;
using PX.SM;

namespace PX.Objects.CR
{
public class LeadMaint_Extension : PXGraphExtension<LeadMaint>
{

public PXAction<CRLead> sendToARSimple;

[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "BA Request")]
public IEnumerable SendToARSimple(PXAdapter adapter)
{
// ————————————————————————————————————————————
// 0) Validation for MX tenant's
// ————————————————————————————————————————————
var company = PX.Data.Update.PXInstanceHelper.CurrentCompany;
if (company != 6 && company != 7){
throw new PXException("BA Request not available for this tenant");
yield break;
}

CRLead lead = Base.Lead.Current;
if (lead == null)
yield break;

// ————————————————————————————————————————————
// 1) get contact and address related
// ————————————————————————————————————————————
Contact contact = null;
if (lead.ContactID != null)
{
contact = PXSelect<Contact,
Where<Contact.contactID, Equal<Required<Contact.contactID>>>>
.Select(Base, lead.ContactID);
}

Address address = null;
if (contact?.DefAddressID != null)
{
address = PXSelect<Address,
Where<Address.addressID, Equal<Required<Address.addressID>>>>
.Select(Base, contact.DefAddressID);
}

// ————————————————————————————————————————————
// 2) get the template
// ————————————————————————————————————————————
var notif = PXSelect<Notification,
Where<Notification.name, Equal<Required<Notification.name>>>>
.Select(Base, "CR Lead Conversion Request")
.RowCast<Notification>()
.FirstOrDefault();
if (notif == null)
throw new PXException("'CR Lead Conversion Request' not found on Email Templates.");

// ————————————————————————————————————————————
// 3) token map
// ————————————————————————————————————————————
var map = new Dictionary<string, string>
{
["((Lead.FirstName))"] = lead.FirstName ?? string.Empty,
["((Lead.LastName))"] = lead.LastName ?? string.Empty,
["((Lead.FullName))"] = lead.FullName ?? string.Empty,
["((Lead.ClassID))"] = lead.ClassID ?? string.Empty,
["((AddressCurrent.CountryID))"] = address?.CountryID ?? string.Empty,
["((Lead.DisplayName.ContactID))"] = lead.ContactID?.ToString() ?? string.Empty,
["((Lead.DisplayName))"] = lead.DisplayName ?? string.Empty
};

// ————————————————————————————————————————————
// 4) token replace for subject and body
// ————————————————————————————————————————————
string subject = notif.Subject ?? string.Empty;
string body = notif.Body ?? string.Empty;

foreach (var kv in map)
{
subject = subject.Replace(kv.Key, kv.Value);
body = body .Replace(kv.Key, kv.Value);
}

// ————————————————————————————————————————————
// 5) Create email draft
// ————————————————————————————————————————————
var mailGraph = PXGraph.CreateInstance<CREmailActivityMaint>();
var draft = mailGraph.Message.Insert(new CRSMEmail
{
MailAccountID = notif.NFrom ?? MailAccountManager.DefaultMailAccountID,
RefNoteID = lead.NoteID,
BAccountID = lead.BAccountID,
ContactID = lead.ContactID,
MailTo = notif.NTo ?? lead.EMail,
MailCc = notif.NCc,
MailBcc = notif.NBcc,
Subject = subject,
Body = body
});

//Save email
//mailGraph.Actions.PressSave();

// ————————————————————————————————————————————
// 6) Open new window
// ————————————————————————————————————————————
var redirect = new PXRedirectRequiredException(mailGraph, "Enviar Correo")
{
Mode = PXBaseRedirectException.WindowMode.NewWindow
};
throw redirect;
}

// ————————————————————————————————————————————
// RowSelected handler to show or hide the button
// ————————————————————————————————————————————
protected virtual void _(Events.RowSelected<CRLead> e)
{
if (e.Row == null) return;
var company = PX.Data.Update.PXInstanceHelper.CurrentCompany;
bool visible = company != 6 && company != 7;
sendToARSimple.SetVisible(!visible);
}

}
}

I hope it helps someone else 😊