Create a Runbase batch class in AX / D365FFO X++ code

Recently got a requirement to create a run base batch for posting inventory journals.So, I created below class.

Please change the code according to your requirement.

Step 1 – Copy and paste the below code in a new class
Step 2 – Create a new action button and attach the class to it.
Step 3 –  Add the action button to a module. In my case I added it to the Inventory Management -> Periodic area.

/// Created this class for posting open inventory journals

class InventoryJournalsPostings extends RunBaseBatch
{
    QueryRun gQueryRun;
    #define.CurrentVersion(1)

    /// <summary>
    /// Construct the class
    /// </summary>
    /// <returns>Return the class object</returns>
    public server static InventoryJournalsPostings construct()
    {
        return new InventoryJournalsPostings();
    }

    /// <summary>
    /// </summary>
    /// <returns>Return the class desc</returns>
    public client server static ClassDescription description()
    {
        return “Inventory Journals Posting”;
    }

    /// <summary>
    /// </summary>
    /// <returns>Whether the class can be used in a batch task</returns>
    protected boolean canGoBatchJournal()
    {
        return true;
    }

    /// <summary>
    /// <returns></returns>
    public container pack()
    {
        container pack = conNull();

        if (gQueryRun)
        {
            pack = gQueryRun.pack();
        }
        return [#CurrentVersion] + [pack];
    }

    /// <summary>
    /// </summary>
    /// <param name = “packedClass”></param>
    /// <returns></returns>
    public boolean unpack(container _packedClass)
    {
        boolean     ret         = false;
        int         version     = RunBase::getVersion(_packedClass);
        container   packedQuery = conNull();

        switch (version)
        {
            case #CurrentVersion:
                [version, packedQuery] = _packedClass;

                if (SysQuery::isPackedOk(packedQuery))
                {
                    gQueryRun   = new QueryRun(packedQuery);
                    ret         = true;
                }
                break;

            default:
                ret = false;
        }
        return ret;
    }

    /// <summary>
    /// Allows the class go batch
    /// </summary>
    /// <returns>Return the result</returns>
    public boolean canGoBatch()
    {
        return true;
    }

    /// <summary>
    /// </summary>
    /// <returns>Return the true result</returns>
    public boolean runsImpersonated()
    {
        return true;
    }

    /// <summary>
    /// Doesn’t allows the class run in the new session
    /// </summary>
    /// <returns>Return the result</returns>
    protected boolean canRunInNewSession()
    {
        return true;
    }

    /// <summary>
    /// </summary>
    /// <returns></returns>
    public boolean showQueryValues()
    {
        return true;
    }

    /// <summary>
    /// </summary>
    /// <returns></returns>
    public QueryRun queryRun()
    {
        return gQueryRun;
    }

    /// <summary>
    /// </summary>
    public void initQueryRun()
    {
        Query                               query = new Query();
        QueryBuildDataSource                inventJournalTableDS;
        QueryBuildRange                     inventJournalStatus;
        QueryBuildRange                     inventJournalStatusRange;

        inventJournalTableDS                      = query.addDataSource(tableNum(InventJournalTable));
        inventJournalStatus                       = inventJournalTableDS.addRange(fieldNum(InventJournalTable, Posted));

        inventJournalStatus                       = inventJournalTableDS.addRange(fieldNum(InventJournalTable, JournalId));

        gQueryRun = new QueryRun(query);
        gQueryRun.saveUserSetup(true);
    }

    /// <summary>
    /// </summary>
    /// <param name = “_args”>The specified arguments</param>
    public static void main(Args _args)
    {
        InventoryJournalsPostings    InventoryJournalsPostings = InventoryJournalsPostings::construct();

        InventoryJournalsPostings.getLast();
        InventoryJournalsPostings.caption();
        InventoryJournalsPostings.initQueryRun();

        if (InventoryJournalsPostings.prompt())
        {
            InventoryJournalsPostings.run();
        }
    }

    /// <summary>
    /// Post open inventory journals
    /// </summary>
    public void run()
    {
        InventJournalTable              inventJournalTable;

        //Query query = gQueryRun.query();
 while (gQueryRun.next())
        {
            inventJournalTable    = gQueryRun.get(tableNum(inventJournalTable));

            if (inventJournalTable.Posted == NoYes::No)
            {
                JournalCheckPost                journalCheckPost;
                journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);
                try
                {
                    ttsbegin;
                    if(journalCheckPost.validate())
                    {
                        journalCheckPost.run();
                    }
                    ttscommit;
                }
                catch
             {
                   info(infolog.text());
             }
            }
        }
    }

}

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...