Creating a module in Microsoft Dynamics 365: Create Number Sequence for a Data Type [Part 7]

This post is part of a series on creating a module within a Microsoft Dynamics 365 for Finance and Operations application. Links to the other parts are available in the first post: Creating a Module in Microsoft Dynamics 365: Laying the Foundation.

A module in Microsoft Dynamics 365 for Finance and Operations is a grouping of similar business processes and functions that allow users to conduct business. Examples include General Ledger, Accounts Receivable, Accounts Payable, and many others. Developers create new modules to group new functionality in an equivalent manner or to produce third-party solutions.

In this series, I am creating a fictitious module called Generic Application Solution (GAS). The assumption will be that the reader has an intermediate knowledge of development in Dynamics 365 as well as best practices. This post will build upon the last post by adding a number sequence for a data type.

Creating the Data Type

Properties of a string data type for the number sequence

The data type for a number sequence is almost always a string. There are no special rules for this data type. The configuration key property should be set to the module’s configuration key.

Adding the Data Type to the Number Sequence Module

Adding the data type to the number sequence module involves adding a block of code (similar to the following) to the loadModule method on the NumberSequenceModule class.

        /* GAS identifier */
        datatype.parmDatatypeId(extendedTypeNum(GASId));
        datatype.parmReferenceHelp("@GAS:GASIdHelp");
        datatype.parmWizardIsContinuous(false);
        datatype.parmWizardIsManual(NoYes::No);
        datatype.parmWizardIsChangeDownAllowed(NoYes::No);
        datatype.parmWizardIsChangeUpAllowed(NoYes::No);
        datatype.parmSortField(1);
        datatype.parmWizardHighest(999999);

        datatype.addParameterType(NumberSeqParameterType::DataArea, true, false);
        this.create(datatype);

In this block of code, developers add the data type to the dataType object with the parmDatatypeId method. The remainder of the lines controls various parameters on the number sequence. The final line adds the data type to the module.

After running the loadModule method, as discussed in a previous post, the data type should be visible in the parameters form.

Parameters for showing the number sequence grid populated

Adding a Reference Method to Parameters Table

The final step in creating the number sequence of the data type is to add a static method that returns a reference to the number sequence.

    /// <summary>
    ///
    /// </summary>
    public static NumberSequenceReference numRefGASId()
    {
        NumberSeqScope scope = NumberSeqScopeFactory::createDataAreaScope(curext());
        return NumberSeqReference::findReference(extendedtypenum(GASId), scope);
    }

Using the Number Sequence

There are a few options in which developers can use a number sequence. This exercise will show how to use it one a form. For the purposes of brevity, I will assume that we already have a form on which we want to use the number sequence. This form should have a table that uses a field with the data type as its primary key and that all of the user interface elements are on it.

Using the number sequence on a form consists of code. Specifically, developers use the NumberSeqFormHandler class. Developers store this object in a member variable on the form and include a method to instantiate the object if it has not already been.

    NumberSeqFormHandler numberSeqFormHandler;

    NumberSeqFormHandler numberSeqFormHandler()
    {
        if (!numberSeqFormHandler)
        {
            numberSeqFormHandler = NumberSeqFormHandler::newForm(GASParameters::numRefGASId().NumberSequenceId,
                                                                 element,
                                                                 GASTable_ds,
                                                                 fieldNum(GASTable, GASId));
        }
        return numberSeqFormHandler;
    }

On the form, developers override the close method.

    void close()
    {
        if (numberSeqFormHandler)
        {
            numberSeqFormHandler.formMethodClose();
        }

        super();
    }

The remaining changes are in the form’s data source. Developers will need to override the linkActive, validateWrite, write, delete, and create methods.

    /// <summary>
    ///
    /// </summary>
    public void linkActive()
    {
        element.numberSeqFormHandler().formMethodDataSourceLinkActive();

        super();
    }
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public boolean validateWrite()
    {
        boolean ret;
    
        ret = super();
    
        ret = element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret) && ret;

        return ret;
    }
    /// <summary>
    ///
    /// </summary>
    public void write()
    {
        ttsbegin;

        super();

        element.numberSeqFormHandler().formMethodDataSourceWrite();

        ttscommit;
    }
    /// <summary>
    ///
    /// </summary>
    public void delete()
    {
        ttsbegin;

        element.numberSeqFormHandler().formMethodDataSourceDelete();

        super();

        ttscommit;
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name = "_append"></param>
    public void create(boolean _append = false)
    {
        element.numberSeqFormHandler().formMethodDataSourceCreatePre();

        super(_append);

        element.numberSeqFormHandler().formMethodDataSourceCreate(true);
    }

After compiling the form and running it, the field that uses the data type should automatically populate when creating a new record.

Details for showing a new record with the primary key field populated from number sequence

In the next post, I will be wrapping up the series. It will finish by adding the module to the module tab in the navigation pane on the dashboard and talking about other considerations.

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s