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, January 30, 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();
    }
}

Friday, January 24, 2014

How to open Form through Code in ax 2012

Hi Guys,

      Today we are going to open a form using code..just try following code


 if(inventtable.Family == family::MettalicCore)
   {
   new MenuFunction(MenuItemDisplayStr(Attributes1),MenuItemType::Display).run();
    }
    else
    {
     new MenuFunction(MenuItemDisplayStr(Attributes),MenuItemType::Display).run();
     }
Here we are opening a form of Attributes1 and Attributes form its a simple way.Block Super()

Here is another way take a look


static void OpenFormByCodeB()
{ FormRun formRun;
Args args = new Args();
;
args.name(formstr(CustTable));
args.record(CustTable::find('ABC'));

formRun = ClassFactory.formRunClass(args);
formRun.init();
formRun.run();
formRun.wait();
}

Now if we tweak this a little bit, we can add our code
Like this:

static void OpenFormByCodeB()
{ Object formRun;
Args args = new Args();
;
args.name(formstr(CustTable));
args.record(CustTable::find('ABC'));

formRun = ClassFactory.formRunClass(args);
formRun.init();

formRun.yourmethodgoeshere(); /* !!

formRun.run();
formRun.wait();
}

By changing the type of formRun from class FormRun to class Object, we can implement and execute extra methods on our destination form! This gives us extra possibilities for customizations. You can pass along extra parameters for example.
Only drawback: While programming, your method doesn't show up in the IntelliSense, showing all the available methods. So be carefull of typo's. 

Monday, January 20, 2014

Generate Txt File on clicked in ax 2012

void clicked()
{
     LedgerJournalTrans   ledgerJournalTrans1;
    BinData     binData;
    TextBuffer  textBuffer;
    ;

    textBuffer = new TextBuffer();
    textBuffer.setText('');
        ledgerJournalTrans1 = LedgerJournalTrans_ds.getFirst(1);
    while(ledgerJournalTrans1) //where custTable.AccountNum < '40020'
    {

        textBuffer.appendText(strfmt('%1 %2 %3 %4 %5',ledgerJournalTrans1.LedgerDimension, ledgerJournalTrans1.Txt, ledgerJournalTrans1.AmountCurDebit, ledgerJournalTrans1.OffsetAccountType, ledgerJournalTrans1.OffsetLedgerDimension));
        ledgerJournalTrans1 = LedgerJournalTrans_ds.getNext();
    }

    textBuffer.getText();

    binData = new BinData();
    binData.setStrData(textBuffer.getText());
    binData.saveFile(@"d:\iban.txt");

}

Monday, January 13, 2014

Worker Phone With Name Filter in ax 2012

static void S_WorkerPhone(Args _args)
{
    TreeNode                    treeNode;
    Query                       q;
    QueryBuildDataSource        qbds,qbds1,qbds2,qbds3;
    QueryRun                    qr;
    QueryBuildRange             qbr;
    HcmWorker                   hcmWorker;
    DirPerson                   dirPerson;
    DirPartyTable               dirPartyTable;
    LogisticsElectronicAddress  logisticsElectronicAddress;
    str                         queryname = "S_WorkerPhone";
    str                         projectName = "A_DemoQuery";
    ;
    #AOT
    // Delete the query from the AOT, if the query exists.

    treeNode = TreeNode::findNode(#QueriesPath);
    //treeNode = treeNode::findNode(#ProjectPrivatePath);
    treeNode = treeNode.AOTfindChild(queryname);
    //treeNode = treeNode.AOTfindChild(queryname);
    if (treeNode)
    {
        //treeNode.AOTDuplicate();
        //treeNode.newObjectName(queryname);
        treeNode.AOTdelete();
    }

    treeNode = TreeNode::findNode(#QueriesPath);
    //treeNode = treeNode::findNode(#ProjectPrivatePath);
    //treeNode = treeNode.AOTfindChild(projectName);
    //treeNode = treeNode.AOTfindChild(queryname);
    treeNode.AOTadd(queryname);
    q                   = treeNode.AOTfindChild(queryname);
    //q                   = treeNode.AOTfindChild("S_WorkerPhone");
    qbds                = q.addDataSource(tableNum(DirPerson));
    qbds1               = qbds.addDataSource(tableNum(DirPartyTable));
    qbds2               = qbds1.addDataSource(tableNum(LogisticsElectronicAddress));
    //qbds3               = qbds1.addDataSource(tableNum(LogisticsElectronicAddress));
    //qbr                 = qbds.addRange(fieldNum(HcmWorker,personnelNumber));
    //qbds1.addLink(fieldNum(DirPartyTable,RecId),
    //              fieldNum(DirPerson,RecId));
    qbds1.addLink(fieldNum(DirPerson,RecId),
                  fieldNum(DirPartyTable,RecId));
    qbds2.addLink(fieldNum(LogisticsElectronicAddress,RecId),
                  fieldNum(DirPartyTable,PrimaryContactEmail));
                 /* && fieldNum(DirPartyTable,primaryContactPhone),
                     fieldNum(LogisticsElectronicAddress,RecId));*/
    //qbds2.addDynalink(fieldNum(LogisticsElectronicAddress,RecId),
    //                  fieldNum(DirPartyTable,primaryContactEmail));
    //qbds2.addLink(fieldNum(LogisticsElectronicAddress,RecId),
    //              fieldNum(DirPartyTable,primaryContactemail));
    //            (fieldNum(DirPartyTable,RecId)));
    //qbds3.addLink(fieldNum(DirPartyTable,primaryContactPhone),
    //              fieldNum(LogisticsElectronicAddress,RecId));
    q.AOTcompile(1);
    q.AOTsave();
    qr = new QueryRun("S_WorkerPhone");
    while(qr.next())
    {
        dirPerson                    = qr.GetNo(1);
        dirPartyTable                = qr.getNo(2);
        logisticsElectronicAddress   = qr.getNo(3);
        //logisticsElectronicAddress   = qr.getNo(4);
        //hcmWorker       = qr.get(tableNum(HcmWorker));
        info(strFmt("%1, %2, %3",dirPerson.Name,logisticsElectronicAddress.Locator,dirPartyTable.primaryPhone()));
    }
}

Monday, January 6, 2014

Failed to create a session, confirm that the user has the proper privileges to log on to Microsoft Dynamics in ax 2012

While running system Administration > Setup > Workflow > Workflow
infrastructure configuration

Error:- Failed to create a session, confirm that the user has the proper privileges to log on to Microsoft Dynamics.

To resolve this:- Confirm that user is logged in as a administrator.
go to the user relation in system Administration > Common > User relation
and set user admin to a person.
and save this. jst close application and start again.


"or"

A closer look into this learned that this is linked to the new partitions feature.

It seems the UserInfo table is not correctly updated and the partition administrator is not updated correctly when you restore an existing DB or the Demo database.
To fix this, you can do the following.

To fix this, you can do the following.

Stop the AOS
Restore the database again
Start the AOS
Start a client and complete the partition initialiasation checklist
Close the client and execute the script below on the SQL database
Restart the client and reimport your license (if you were restoring the Microsoft demo data, the demo license is back in there)
Then compile / generate CIL / DB sync and you should be on track again!

Worker phone with name by query in ax 2012

static void S_WorkerPhone(Args _args)
{
    TreeNode                    treeNode;
    Query                       q;
    QueryBuildDataSource        qbds,qbds1,qbds2,qbds3;
    QueryRun                    qr;
    QueryBuildRange             qbr;
    HcmWorker                   hcmWorker;
    DirPerson                   dirPerson;
    DirPartyTable               dirPartyTable;
    LogisticsElectronicAddress  logisticsElectronicAddress;
    str                         queryname = "S_WorkerPhone";
    str                         projectName = "A_DemoQuery";
    ;
    #AOT
    // Delete the query from the AOT, if the query exists.

    treeNode = TreeNode::findNode(#QueriesPath);
    //treeNode = treeNode::findNode(#ProjectPrivatePath);
    treeNode = treeNode.AOTfindChild(queryname);
    //treeNode = treeNode.AOTfindChild(queryname);
    if (treeNode)
    {
        //treeNode.AOTDuplicate();
        //treeNode.newObjectName(queryname);
        treeNode.AOTdelete();
    }

    treeNode = TreeNode::findNode(#QueriesPath);
    //treeNode = treeNode::findNode(#ProjectPrivatePath);
    //treeNode = treeNode.AOTfindChild(projectName);
    //treeNode = treeNode.AOTfindChild(queryname);
    treeNode.AOTadd(queryname);
    q                   = treeNode.AOTfindChild(queryname);
    //q                   = treeNode.AOTfindChild("S_WorkerPhone");
    qbds                = q.addDataSource(tableNum(DirPerson));
    qbds1               = qbds.addDataSource(tableNum(DirPartyTable));
    qbds2               = qbds1.addDataSource(tableNum(LogisticsElectronicAddress));
    //qbds3               = qbds1.addDataSource(tableNum(LogisticsElectronicAddress));
    //qbr                 = qbds.addRange(fieldNum(HcmWorker,personnelNumber));
    //qbds1.addLink(fieldNum(DirPartyTable,RecId),
    //              fieldNum(DirPerson,RecId));
    qbds1.addLink(fieldNum(DirPerson,RecId),
                  fieldNum(DirPartyTable,RecId));
    qbds2.addLink(fieldNum(LogisticsElectronicAddress,RecId),
                  fieldNum(DirPartyTable,PrimaryContactEmail));
                 /* && fieldNum(DirPartyTable,primaryContactPhone),
                     fieldNum(LogisticsElectronicAddress,RecId));*/
    //qbds2.addDynalink(fieldNum(LogisticsElectronicAddress,RecId),
    //                  fieldNum(DirPartyTable,primaryContactEmail));
    //qbds2.addLink(fieldNum(LogisticsElectronicAddress,RecId),
    //              fieldNum(DirPartyTable,primaryContactemail));
    //            (fieldNum(DirPartyTable,RecId)));
    //qbds3.addLink(fieldNum(DirPartyTable,primaryContactPhone),
    //              fieldNum(LogisticsElectronicAddress,RecId));
    q.AOTcompile(1);
    q.AOTsave();
    qr = new QueryRun("S_WorkerPhone");
    while(qr.next())
    {
        dirPerson                    = qr.GetNo(1);
        dirPartyTable                = qr.getNo(2);
        logisticsElectronicAddress   = qr.getNo(3);
        //logisticsElectronicAddress   = qr.getNo(4);
        //hcmWorker       = qr.get(tableNum(HcmWorker));
        info(strFmt("%1, %2, %3",dirPerson.Name,logisticsElectronicAddress.Locator,dirPartyTable.primaryPhone()));
    }
}