The below event works fine when the UI button is pressed, however when a business event calls it the PressButton() function returns a NullRefernceException. I can not figure out why.
the exception stack is
at PX.Data.PXAction`1.<Press>d__38.MoveNext()
at PX.Data.GraphHelper.PressButton(PXAction action, PXAdapter adapter)
at GPGeneral.PM.PMTaskEntry_Ext.SendPendingNotifications(PXAdapter adapter) in C:\Program Files\Acumatica ERP\ACU-1\App_Data\Projects\GPGeneral\GPGeneral\PM\Task\PMTaskEntry_Ext.cs:line 511
at PX.Data.PXAction`1.RunHandler(PXAdapter adapter)
at PX.Data.PXAction`1.<Press>d__38.MoveNext()
public PXAction<PMTask> sendPendingNotifications;
[PXUIField(DisplayName = "Send Pending Notifications", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Select, Enabled = true)]
[PXButton(CommitChanges = true, Category = "Notify", Connotation = PX.Data.WorkflowAPI.ActionConnotation.None)]//, ImageKey = Sprite.Main.Preview)]
public IEnumerable SendPendingNotifications(PXAdapter adapter)
{
var task = Base.Task.Current;
if (task != null)
{
var task_Ext = task.GetExtension<PMTask_Ext>();
var existingNotifications = Notifications.Select().RowCast<GPTaskNotificationRecord>().ToList();
foreach (var item in existingNotifications)
{
if (item.DelayedSend == true)
{
var emailGraph = createTaskEmailGraph(task, item, item.DelayedSendCrew ?? task_Ext.UsrPrimaryCrewID);
emailGraph.Send.PressButton();
updateNotificationSentStats(item);
}
}
}
return adapter.Get();
}
private CREmailActivityMaint createTaskEmailGraph(PMTask tsk, GPTaskNotificationRecord notRec, int? crewID)
{
var not = GPTaskNotification.PK.Find(Base, notRec.TaskNotificationID);
var noteTemplate = Notification.PK.Find(Base, not.EmailTemplateID);
if (noteTemplate == null)
throw new PXException($"Notification template '{not.EmailTemplateID}' is missing");
CREmailActivityMaint emailMaint = PXGraph.CreateInstance<CREmailActivityMaint>();
var email = emailMaint.Message.Insert(new CRSMEmail()
{
MailTo = emailToMerge(noteTemplate.NTo, crewID),
MailCc = emailToMerge(noteTemplate.NCc, crewID),
MailBcc = emailToMerge(noteTemplate.NBcc, crewID),
Subject = emailMerge(noteTemplate.Subject, crewID),
Body = emailMerge(noteTemplate.Body, crewID),
RefNoteIDType = typeof(PMTask).ToString(),
RefNoteID = tsk.NoteID
//RefNoteID = task.NoteID,
});
if (noteTemplate.NFrom != null)
email.MailAccountID = noteTemplate.NFrom;
PXNoteAttribute.CopyNoteAndFiles(Base.Caches<Notification>(), noteTemplate, emailMaint.Message.Cache, email, false, true);
}
return emailMaint;
}