How To Use the SysPackExtension Class to Pack Additional Fields in Dynamics 365 Finance and Operations

By Elliot Thompson | December 21, 2023

If you are working with x++ development language in Dynamics 365 Finance and Operations and need to extend a class that implements packing to pack some additional fields, you might be wondering how to do it safely.

In this blog post, I will show you how to use the SysPackExtension class to achieve this.

The SysPackExtension class is a helper class that allows you to add fields to a packed class without modifying the original class. It works by utilizing the chain of command on the pack and unpack methods to add to the packed container a container of your fields where the first elements in the container are the version and the name of your extension class.

For example, the original container could look like [1, 2, 3] and you want to add 4 to that. The result would be something like [1, 2, 3, [1, “SomeBaseClass_Extension”, 4]].

Here is an example:

[ExtensionOf(classStr(SalesOrderCopyingContract))] 

internal final class SSISalesOrderCopyingContract_Extension 

{ 

    public NoYes createNewOrder; 

    public int newOrdersToCreate; 

 

    #define.CurrentVersion(1) // same as base class 

 

    #localMacro.CurrentList 

        createNewOrder, 

        newOrdersToCreate 

    #endMacro 

 

    public container pack() 

    { 

        container retContainer = next pack(); 

        container conValues = [#CurrentVersion, #CurrentList]; 

 

        return SysPackExtensions::appendExtension(retContainer, classStr(SSISalesOrderCopyingContract_Extension), conValues); 

    } 

 

    public boolean unpack(container _packedClass) 

    { 

        boolean retValue = next unpack(_packedClass); 

 

        if(retValue) 

        { 

            container packedExtension = SysPackExtensions::findExtension(_packedClass, classStr(SSISalesOrderCopyingContract_Extension)); 

 

            if(conLen(packedExtension) > 0) 

            { 

                Integer version = RunBase::getVersion(packedExtension); 

                switch(version) 

                { 

                    case #CurrentVersion: 

                        [version, #CurrentList] = packedExtension; 

Default: 

    retValue = false; 

                } 

            } 

        } 

 

        return retValue; 

    } 

} 

Need Assistance With Your Dynamics 365 Finance and Operations Environment?

Please reach out to us or visit our support page to learn more about our services. Our team can assist you with this and many other aspects of your environment to ensure you are optimizing it.

You can also visit our YouTube page for video resources.

Related Posts


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!