Thursday 13 December 2018

Create a Runbase batch class in AX 7.0 / D365 F&O 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());
             }
            }
        }
    }

}

7 comments:

  1. Tarbiya, genuine Islamic good preparing, should be a basic piece of it. This should be the spirit of our schooling, not a formal husk. All designs for improving our schooling will be absolutely pointless except if they depend on a full comprehension of this key certainty. education

    ReplyDelete
  2. The dictatorial needs the instruction in the motivation behind making compliant supporters. Thus, that is the reason they favor a sort of training whose object is to fabricate submission and compliance. In the other hand, Democracy is not the same as them. pre school Singapore

    ReplyDelete
  3. Aren't we supposed to use the SysOperation framework in D365 now?

    ReplyDelete
  4. All things being equal, I early resigned five years prior subsequent to maintaining my own counseling business for only seven years.coding for kids

    ReplyDelete
  5. So what is expected of a custom curriculum instructor? Board certificate with the National Association of Special Education Teachers is a main advantage. To become board guaranteed with NSAT, an instructor should meet the entirety of their undergrad and postgraduate certification necessities. Preschool in Bangalore

    ReplyDelete
  6. This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it
    on the site pozyczki-24.pl

    ReplyDelete

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...