AX / D365FO – Get Next Sequence number even if Sequence mode is set to manual #d365fo #ax #msdyn365fo

There are cases in which we need to be able to use number sequences even if the mode is set to manual.

I am referring in particular to those automatic creation procedures that require the automatic entry of a number sequence.

If the sequence encoding mode is set to manual then we will not be able to take advantage of the automatic numbering.

To use the automation it is necessary to make a small change in the code.

Here’s how to do it:

/// <summary>
/// NumberSeq class extension
/// </summary>
[ExtensionOf(classStr(NumberSeq))]
final class NumberSeq_Extension
{
    /// <summary>
    /// Get Next number even if number sequence is set to manual
    /// </summary>
    /// <returns>Next Sequence Number</returns>
    public Num getNextNumBypassManual()
    {
        if (!numberId || voucherId)
            throw error("@SYS26051");

        this.runNumber();   //This method gets the next sequence number

        return lastNumGenerated;
    }
}

First we need to extend the NumberSeq class adding a new custom method (getNextNumBypassManual()).

This method is essentially the same as the standard num() method but without controlling how the sequences are set. The standard method checks the input mode and if it is set to Manual it prevents the numerator from being advanced. We must remove this control so that the numerator is advanced even if the setting is manual.

class RunnableClass1
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        NumberSeq   numberSeq;
        Num         nextSeqNum;


        selectableDataArea selectableDataArea = 'DAT'; //Set Legal Entity
        NumberSeqScope numberSeqScope = NumberSeqScopeFactory::createDataAreaScope(selectableDataArea); //Get Scope of the legal legal entity

        NumberSequenceReference numberSequenceReference = NumberSeqReference::findReference(extendedTypeNum(CustAccount), numberSeqScope); //Get Customer Number sequence reference for the given legal entity 

        numberSeq   = NumberSeq::newGetNum(numberSequenceReference);
        nextSeqNum   = numberSeq.GetNextNumBypassManual(); //Get next Customer number sequence by calling the extended method
        
        info(nextSeqNum);   //Show number sequence
    }
}

In the above code we created a Runnable class to get the Next Customers sequence number for a given legal entity even if mode is set to manual.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s