Create a Run base batch class in AX 7.0 / D365 F&O X++ code

Recently got a requirement to create a run base batch for enabling customer interest charges flag in credit and collection module. So, I created the 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 Credit and Collection -> Periodic area.


 class BCCustTransInterestUpdate extends RunBaseBatch
 {
    CustAccount     custAccount;
    // Dialog fields     DialogField     dlgCustAccount;
    #define.CurrentVersion(1)     #define.Version1(1)     #localmacro.CurrentList         custAccount     #endmacro
            protected boolean canRunInNewSession()     {         return true;     }
    public boolean canGoBatchJournal()     {         return true;     }
    public Object dialog()     {         DialogRunbase       dialog = super();
        dlgCustAccount = dialog.addFieldValue(extendedTypeStr(CustAccount), custAccount);
        return dialog;     }
    public boolean getFromDialog()     {              custAccount = dlgCustAccount.value();
        return super();     }
    protected void new()     {         super();     }
    public container pack()     {         return [#CurrentVersion,#CurrentList];     }
    public boolean unpack(container packedClass)     {         Version version = RunBase::getVersion(packedClass);         ;
        switch (version)         {             case #CurrentVersion:                 [version,#CurrentList] = packedClass;                 break;             default:                 return false;         }
        return true;     }
    public void updateCustIntrest()     {         Query                   query;         QueryBuildDataSource    qbds;         QueryRun                qr;         CustTrans               custTrans;
        query   = new Query();         qbds    = query.addDataSource(tableNum(CustTrans));                 if (custAccount)         {             qbds.addRange(fieldNum(CustTrans, AccountNum)).value(custAccount);         }
        qr      = new QueryRun(query);
        while (qr.next())         {             custTrans = qr.get(tableNum(CustTrans));
            if (custTrans.DueDate < DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()) &&                 CustTable::find(custTrans.AccountNum).CustExcludeInterestCharges == false)             {                 ttsbegin;
                custTrans.selectForUpdate(true);                 custTrans.Interest = NoYes::Yes;                 custTrans.update();
                ttscommit;             }         }
    }
    /// <summary>     ///    Contains the code that does the actual job of the class.     /// </summary>     public void run()     {         #OCCRetryCount         if (! this.validate())         {             throw error("");         }
        try         {             ttsbegin;
            this.updateCustIntrest();
            ttscommit;         }         catch (Exception::Deadlock)         {             retry;         }         catch (Exception::UpdateConflict)         {             if (appl.ttsLevel() == 0)             {                 if (xSession::currentRetryCount() >= #RetryNum)                 {                     throw Exception::UpdateConflictNotRecovered;                 }                 else                 {                     retry;                 }             }             else             {                 throw Exception::UpdateConflict;             }         }     }
    public boolean runsImpersonated()     {         return true;     }
    server static BCCustTransInterestUpdate construct()     {         return new BCCustTransInterestUpdate();     }
    static ClassDescription description()     {         return "Customer intrest update";     }
    static void main(Args args)     {         BCCustTransInterestUpdate    custTransIntrestUpdate;             custTransIntrestUpdate = BCCustTransInterestUpdate::construct();
        if (custTransIntrestUpdate.prompt())         {             custTransIntrestUpdate.runOperation();         }     }
}

Comments

Popular posts from this blog

D365FO - Data integration by OData

Create or Update Product attributes using X++ in D365 F&O

Layers concept in D365