Solved

Action PressButton via Business Event causes NullReferenceException

  • 20 March 2024
  • 5 replies
  • 34 views

Userlevel 3
Badge +1

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;
}

 

icon

Best answer by Leif 22 March 2024, 22:13

View original

5 replies

Userlevel 7
Badge +5

Which line of code is 511?

Is it possible that this line needs some protections against null references?

var existingNotifications = Notifications.Select().RowCast<GPTaskNotificationRecord>().ToList();
Userlevel 3
Badge +1

I am sorry, the exception happens inside the PressButton function call. 

line: emailGraph.Send.PressButton();

I don’t think that would have an impact on this exception.

Userlevel 4
Badge

Anything that references Current would be where I would start looking in scenarios where you are relying on any sort of “automation”.  More than once,  something I have considered to be current was not when the non-ui initiated version was used..

 var task = Base.Task.Current;
Userlevel 3
Badge +1

I ended up replacing the offending function call with the code from the action being called.

replacing

emailGraph.Send.PressButton();

With

                        CRSMEmail current = emailGraph.Message.Current;
emailGraph.ValidateEmailFields(current);
emailGraph.Actions.PressSave();
CRSMEmail cRSMEmail = (CRSMEmail)emailGraph.Message.Cache.CreateCopy(current);
//TryCorrectMailDisplayNames(emailGraph, cRSMEmail);
cRSMEmail.MPStatus = "PP";
cRSMEmail.RetryCount = 0;
cRSMEmail = (CRSMEmail)emailGraph.Message.Cache.Update(cRSMEmail);
emailGraph.SaveClose.Press();

 

Userlevel 7
Badge

Thank you for sharing your solution with the community @Leif!

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved