Skip to main content
Solved

Action PressButton via Business Event causes NullReferenceException


Forum|alt.badge.img+1
  • Varsity I
  • 50 replies

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

 

Best answer by Leif

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();

 

View original
Did this topic help you find an answer to your question?

5 replies

Forum|alt.badge.img+6
  • Captain II
  • 551 replies
  • March 20, 2024

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();

Forum|alt.badge.img+1
  • Author
  • Varsity I
  • 50 replies
  • March 20, 2024

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.


Mike Gifford
Jr Varsity III
Forum|alt.badge.img
  • Jr Varsity III
  • 25 replies
  • March 22, 2024

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;

Forum|alt.badge.img+1
  • Author
  • Varsity I
  • 50 replies
  • Answer
  • March 22, 2024

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();

 


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • 2751 replies
  • March 22, 2024

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings