Monday, January 27, 2020

Alert notification pop up and In user message center for custom/standard table in D365FO



///  class delivers the alerts notifications to user for custom tables
class BRUEventNotification_Test extends EventNotification
{
    SysIMailerNonInteractive mailer;
    Email        senderEmailAddress;

    public EventInboxId create()
    {
        SysUserInfo     userInfo;

        super();

        // send mail as well if enabled.
        userInfo = SysUserInfo::find(inbox.UserId);
        if (userInfo && userInfo.EventWorkflowTasksInEmail)
        {
            if (SysEmailDistributor::validateEmail(userInfo.emailDisplay()))
            {
                inbox.SendEmail = true;
                inbox.EmailRecipient = userInfo.emailDisplay();
                this.sendMail();
            }
        }

        inbox.AlertCreatedDateTime = DateTimeUtil::newDateTime(DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()),DateTimeUtil::getTimeNow(DateTimeUtil::getUserPreferredTimeZone()),DateTimeUtil::getUserPreferredTimeZone());
        inbox.update();
        this.addPopupNotification(inbox, SysUserInfo::find(inbox.userId).EventPollFrequency, 30,this.ParmMenuFunction().name(),MenuItemType::Display,this.ParmRecord().RecId);//To get alert in to message center
        return inbox.InboxId;
    }

    private void addPopupNotification(EventInbox _inbox, int _reminderInterval, int _addDays, MenuItemName _menuItemName, MenuItemType _menuItemType, Int64 _recId)
    {
        SystemNotificationDataContract ndc = new SystemNotificationDataContract();
        Array actions = new Array (Types::Class);
        Array userList = new Array(Types::String);

        // create the message to be displayed in the message center
        userList.value(1, _inbox.UserId);

        ndc.Message(_inbox.Message);
        ndc.Title(_inbox.Subject);
        ndc.RuleId(_inbox.RuleId);
        ndc.Type(SystemNotificationType::Alert);
        ndc.State(SystemNotificationState::Active);
        ndc.Severity(SystemNotificationSeverity::Informational);
        ndc.Users(userList);
        ndc.ReminderInterval(_reminderInterval);
        ndc.ExpirationDateTime(DateTimeUtil::addDays(DateTimeUtil::utcNow(), _addDays));

        if (_menuItemName)
        {
            // create the hyperlink to the form
            SystemNotificationActionDataContract adc = new SystemNotificationActionDataContract();
            userList.value(1, _inbox.UserId);
            adc.Message(_inbox.AlertedFor);
            if (_menuItemType == MenuItemType::Action)
            {
                adc.Type(SystemNotificationActionType::AxActionMenuFunction);
            }
            else
            {
                // if it isn't an action menu item, assume display
                adc.Type(SystemNotificationActionType::AxDisplayMenuFunction);
            }

            SystemNotificationMenuFunctionDataContract notificationActionData = new SystemNotificationMenuFunctionDataContract();
            notificationActionData.MenuItemName(_menuItemName);
            notificationActionData.Data(int642Str(_recId));
            adc.Data(FormJsonSerializer::serializeClass(notificationActionData));
            actions.value(1,adc);
            ndc.Actions(actions);
        }

        // add to the Message center
        SystemNotificationsManager::AddNotification(ndc);
    }

    protected void new()
    {
        super();
        inbox.NotificationSource = EventNotificationSource::Alerts;
    }

    protected Email senderEmailAddress()
    {
        SysUserInfo     sysUserInfo;

        if (! senderEmailAddress)
        {
            // get the sender email address
            sysUserInfo = SysUserInfo::find(curuserid());
            senderEmailAddress = sysUserInfo.emailDisplay();

            // set senderEmail = toEmail
            if (! senderEmailAddress)
            {
                sysUserInfo         = SysUserInfo::find(inbox.UserId);
                senderEmailAddress  = sysUserInfo.emailDisplay();
            }
        }

        return senderEmailAddress;
    }

    protected void sendMail()
    {
        #define.mailPriority(1)

        super();

        if (mailer == null)
        {
            mailer = SysMailerFactory::getNonInteractiveMailer();
        }

        if (mailer != null
            && SysEmailDistributor::validateEmail(this.senderEmailAddress()))
        {
            try
            {
                var messageBuilder = new SysMailerMessageBuilder();
                messageBuilder.setFrom(this.senderEmailAddress(), "@SYS115063")
                              .setSubject(inbox.Subject)
                              .addTo(inbox.EmailRecipient)
                              .setPriority(#mailPriority)
                              .setBody(strReplace(inbox.Message, '\r\n', '<br>'));
                mailer.sendNonInteractive(messageBuilder.getMessage());
            }
            catch
            {
                warning(strfmt("@SYS324464",inbox.EmailRecipient));
                mailer = null;
            }
        }
    }

    protected static BRUEventNotification_Test construct()
    {
        return new BRUEventNotification_Test();
    }

    public static BRUEventNotification_Test newStandard(
        userId                  _userId,
        EventSubject            _subject,
        EventMessage            _message,
        EventNotificationType   _eventNotificationType,
        Common                  _record)
    {
        BRUEventNotification_Test  notification = BRUEventNotification_Test::construct();
        MenuFunction    getmenuFunction;
        getmenuFunction = new MenuFunction(menuitemdisplaystr(TestTableDetails),MenuItemType::Display);
        notification.parmUserId(_userId);
        notification.parmSubject(_subject);
        notification.parmMessage(_message);
        notification.parmNotificationType(_eventNotificationType);
        notification.parmRecord(_record);
        notification.parmDataSourceName(tableId2Name(tableNum(TestTable)));
        notification.parmMenuFunction(getmenuFunction);

        return notification;
    }

}

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...