Blog

Name is Anant Dubey and the intent to create this blog is to discuss the problems and issues that developer face in the dynamics AX development and to share the new things that come up with the new version of AX.

Thursday, 30 January 2014

how to add another user in Alert in ax 2012

public void execute(
    EventRule   eventRule,
    EventType   eventType,
    Common      buffer,
    EventInbox inbox,
    // note: for due date events, the time and date on which the alert
    // is considered to be created is when the due date batch
    // started to run
    EventAlertCreatedDateTime alertCreatedDateTime = DateTimeUtil::newDateTime(systemDateGet(),timeNow(),DateTimeUtil::getUserPreferredTimeZone())
    )
{
    EventInboxId            inboxId;
    EventInboxData          inboxData;
    EventAlertField         eventAlertField;
    WorkflowRecordCaptionGenerator recordCaptionGenerator;

    container c;

    List                    list;
    SysUserInfo userInfo1;
    SysUserInfo userInfo = SysUserInfo::find(eventRule.UserId);
    if(userInfo)
    {

        inboxId = EventInbox::nextEventId();

        inbox.initValue();
        inbox.initFromEventRule(eventRule);

        inbox.InboxId           = inboxId;

        inbox.AlertCreatedDateTime = alertCreatedDateTime;

        recordCaptionGenerator = WorkflowRecordCaptionGenerator::construct(buffer);
        inbox.AlertedFor        = recordCaptionGenerator.caption();

        if (userInfo && ((userInfo.EventPopUpDisplayWhen == EventPopupShowDefineMode::AllRules) ||
            (userInfo.EventPopUpDisplayWhen == EventPopupShowDefineMode::DefinedOnRule && eventRule.ShowPopup == true)))
            inbox.Visible = false;

        list                = SysDictTable::getUniqueIndexFields(buffer.TableId);
        if (list)
        {
            inbox.keyFieldList(list.pack());
            inbox.keyFieldData(SysDictTable::mapFieldIds2Values(list,buffer).pack());
        }

        if (eventRule.alertField())
        {
            eventAlertField = eventRule.alertField();
            if (eventAlertField != null && eventAlertField.parmAlertField().FieldRelationPath)
            {
                inbox.ExtendedDataType = eventAlertField.parmAlertField().FieldRelationPathExtendedDataType;
                inbox.EnumType = eventAlertField.parmAlertField().FieldRelationPathEnumType;
            }
        }


        if (userInfo)
        {
            // if email send settings are forced
            if (userInfo.EventEmailAlertsWhen == EventEmailSendDefineMode::AllRules)
                inbox.SendEmail = true;
            else if (userInfo.EventEmailAlertsWhen == EventEmailSendDefineMode::NoRules)
                inbox.SendEmail = false;

            if (inbox.SendEmail)
                inbox.EmailRecipient = eventRule.emailRecipient();
        }

        inbox.insert();

        // insert packed EventType class
        c = eventType.pack();
        inboxData.InboxId = inboxId;
        inboxData.DataType = EventInboxDataType::TypeData;
        inboxData.Data = c;
        inboxData.insert();

        // insert packed context information for drill down
        inboxData.InboxId   = inboxId;
        inboxData.DataType  = EventInboxDataType::Context;
        inboxData.Data      = eventRule.contextInfo();
        inboxData.insert();
    }

    eventRule.UserId    = eventRule.UserId1;
    userInfo1 = SysUserInfo::find(eventRule.UserId1);
    if(userInfo1)
    {
        userInfo    =   userInfo1;
        inboxId = EventInbox::nextEventId();

        inbox.initValue();
        inbox.initFromEventRule(eventRule);

        inbox.InboxId           = inboxId;

        inbox.AlertCreatedDateTime = alertCreatedDateTime;

        recordCaptionGenerator = WorkflowRecordCaptionGenerator::construct(buffer);
        inbox.AlertedFor        = recordCaptionGenerator.caption();

        if (userInfo && ((userInfo.EventPopUpDisplayWhen == EventPopupShowDefineMode::AllRules) ||
            (userInfo.EventPopUpDisplayWhen == EventPopupShowDefineMode::DefinedOnRule && eventRule.ShowPopup == true)))
            inbox.Visible = false;

        list                = SysDictTable::getUniqueIndexFields(buffer.TableId);
        if (list)
        {
            inbox.keyFieldList(list.pack());
            inbox.keyFieldData(SysDictTable::mapFieldIds2Values(list,buffer).pack());
        }

        if (eventRule.alertField())
        {
            eventAlertField = eventRule.alertField();
            if (eventAlertField != null && eventAlertField.parmAlertField().FieldRelationPath)
        {
            inbox.ExtendedDataType = eventAlertField.parmAlertField().FieldRelationPathExtendedDataType;
            inbox.EnumType = eventAlertField.parmAlertField().FieldRelationPathEnumType;
        }
        }

        if (userInfo)
        {
            // if email send settings are forced
            if (userInfo.EventEmailAlertsWhen == EventEmailSendDefineMode::AllRules)
                inbox.SendEmail = true;
            else if (userInfo.EventEmailAlertsWhen == EventEmailSendDefineMode::NoRules)
                inbox.SendEmail = false;

            if (inbox.SendEmail)
                inbox.EmailRecipient = eventRule.emailRecipient();
            }

            inbox.insert();

            // insert packed EventType class
            c = eventType.pack();
            inboxData.InboxId = inboxId;
            inboxData.DataType = EventInboxDataType::TypeData;
            inboxData.Data = c;
            inboxData.insert();

            // insert packed context information for drill down
            inboxData.InboxId   = inboxId;
            inboxData.DataType  = EventInboxDataType::Context;
            inboxData.Data      = eventRule.contextInfo();
            inboxData.insert();
    }
}