Extension Model: Creating a Static Table Method in Dynamics 365 for Operations

By Brandon Carmichael | June 5, 2017

Creating table static methods for some tables was straight forward prior to the release of Microsoft Dynamics 365 for Operations (AX7), and arguably it still is if you are overlaying. Although this article is not intended to distinguish between over layering and extensions, I feel it is important enough to include some key points in this post.

Creating table methods that are static, while using the extension model are a little different. In this scenario, I will create a new method to validate that there is an entry in the external comment field in the table: TSTimeSheetLineWeek.

static class TSTimesheetLineWeek_Extension
{
    /// <summary>
    /// Don't forget your comments
    /// </summary>
    /// <returns>
    /// Don't forget your comments
    /// </returns>
    /// <remarks>
    /// Don't forget your comments
    /// </remarks>
    public static boolean validateExtNotes(TSTimesheetLineWeek _tstTimeSheetLineWeek)
    {
        int i;
        boolean ret = true;
    
        for(i = 1; i <= 7; i++)
        {
            if (_tstTimeSheetLineWeek.Hours[i] != 0)
            {
                if (_tstTimeSheetLineWeek.ExternalComments[i] == '')
                {
                    ret = checkFailed("External Comments are missing");
                    break;
                }
            }
        }
        return ret;
    }
}

NOTE:  NOTE – I created a class with a name of the table (TsTimeSheetLineWeek) with the mandatory _Extension.

NOTE:  Parameter of static table method must be of the type of table you are creating the method for, in this case - TsTimeSheetLineWeek.

To call the method, you could do something like the following:

class TSTimesheetEntryFormEventHandler
{
    /// <summary>
    /// Don't forget your comments
    /// </summary>
    /// <param name="args"></param>
    [PostHandlerFor(tableStr(TSTimesheetLineWeek), tableMethodStr(TSTimesheetLineWeek, validateRecord))]
    public static void TSTimesheetLineWeek_Post_validateRecord(XppPrePostArgs args)
    {
        boolean                 		validated = true;
        TsTimesheetLineWeek     	timeSheetLineWeek = args.getThis() as TSTimesheetLineWeek;
        TSTimesheetLine         	timeSheetLine;
 
        timeSheetLine = TSTimesheetLine::findRecId(timeSheetLineWeek.TSTimesheetLine);
 
        // check the line property to see if external notes are required.
        if(UtilClass::checkLinePropertyExtNotes(timeSheetLine.LinePropertyId))
        {
           validated = validated & timesheetLineWeek.validateExtNotes();
        }
        // check the line property to see if internal notes are required.
        if(UtilClass::checkLinePropertyIntNotes(timeSheetLine.LinePropertyId))
        {
            validated = validated & timesheetLineWeek.validateIntNotes();
        }
        args.setReturnValue(validated);
    }

Over layering

A few key points to remember about over layering, include:

  • You can customize source code and metadata of model elements that are shipped by Microsoft or third-party Microsoft partners.
  • The overlaying model must belong to the same Package as the source model and belong to a layer that is higher than the source model.
  • Over layering is a powerful tool to perform advanced customizations of metadata and source code, but may increase the cost of upgrading a solution to a new version.

Use of extensions

A few advantages to remember about the use of extensions include:

  • Extension models simplify and improve the performance of deployments, builds, test automation and delivery to customers.
  • Building your model or project doesn’t require you to recompile the entire application.
  • Microsoft can install, patch, upgrade, and change internal APIs without affecting your customizations.
  • Extensions reduce the cost of upgrading to a new version, as this approach eliminates costly code and metadata conflicts.

Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!