web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

making code better

(1) ShareShare
ReportReport
Posted on by 536
Hi,

If let's say we have a parent class for posting journals
Table1post

and we have other child classes that extend it, like
Table1Post_JournalType1, Table1Post_JournalType2, Table1Post_JournalType3, Table1Post_JournalType4...etc

Now i have a logic that is common between JournalType1 and JournalType2 only -- so what is better to put the logic in each class of those journal types or to put it in parent class and put if conditon for those two types only? or a new class for all of this?

Here's what I did:

 
//ParentClass
[ExtensionOf(classStr(Table1Post))]
final class Table1Post_Extension
{
    private boolean canJournalGenerateId(Table1 _Table1)
    {
        if (!_table1 || _table1.Enum1 == NoYes::Yes)
        {
            return false;
        }     
        switch (_table1.JournalType)
        {
            case JournalType::JournalType1:
                return this.parmCreateOrder();
            case JournalType::JournalType2:
                return this.isPreviousJournalType1WithoutOrder(_table1);
        }
    
        return false;
    }
 
    protected str generatId(Table1 _table1)
    {
        str IdNext;
        
        TableSetup  tableSetup = TableSetup::find(_table1.CustAccount);
        if(tableSetup.IdPrefix)
        {
            if(this.canJournalGenerateId(_table1))
            {
                str numFormat = '####';
                Table1 table1ParmId;
   
                select count(RecId) from table1ParmId
                    where table1ParmId.JournalParmId == _table1.JournalParmId;
                if(table1ParmId.RecId == 1)
                {
                    int     lastNum;
                    str     lastDeliveryNumberSeqFormat;
                    str     dLastCreated  = Table1::IdLastCreated(_table1.CustAccount, _table1.JournalType);

                    //logic
            }
        }
        return IdNext;
    }
 
    
    public boolean isPreviousJournalType1WithoutOrder(Table1 _table1)
    {
         //logic
    }
}

//child class 1
[ExtensionOf(classStr(Table1Post_JournalType1))]
final class Table1Post_JournalType1_Extension
{

    void postPosting(Table1 _table1)
    {
        Table1 table1Update = _table1;
        
        str IdNext = this.generateId(_table1);
        if(IdNext)
        {
            ttsbegin;
            table1Update = Table1::find(_table1.JournalId, true);
            table1Update.Id = IdNext;
            table1Update.update();
            ttscommit;
        }
        next postPosting(journalTableUpdate);
    }
    public void initTableX(Table1 _table1, TableX _tableX)
    {
        next initTableX(_table1, _tableX);
        if (_tableX &&  _tableX.Id == '')
        {
            _tableX.Id = _table1.Id;
        }
    }}

//child class 2
[ExtensionOf(classStr(Table1Post_JournalType2))]
final class Table1Post_JournalType2_Extension
{

    void postPosting(Table1 _table1)
    {
        Table1 table1Update = _table1;
        
        str IdNext = this.generateId(_table1);
        if(IdNext)
        {
            ttsbegin;
            table1Update = Table1::find(_table1.JournalId, true);
            table1Update.Id = IdNext;
            table1Update.update();
            ttscommit;
        }
        next postPosting(journalTableUpdate);
    }
    public void initTableX(Table1 _table1, TableX _tableX)
    {
        next initTableX(_table1, _tableX);
        if (_tableX &&  _tableX.Id == '')
        {
            _tableX.Id = _table1.Id;
        }
    }
}


 
//Table1 method
[ExtensionOf(tableStr(Table1))]
final class Table1_Extension
{

    public static str IdLastCreated(CustAccount _custAccount, JournalType _journalType)
    {
        if(_journalType == JournalType::JournalType1 || _journalType == JournalType::JournalType2)
        {
            str  IdLastCreated = '';
            StartDateTime   todayDate = DateTimeUtil::newDateTime(DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()),0);
            IdLastCreated = (select reverse Id from Table1 order by Id where (Table1.createdDateTime >= todayDate
                        && Table1.createdDateTime < DateTimeUtil::addDays(todayDate, 1))
                        && Table1.Posted == 1
                        && Table1.CustAccount == _custAccount
                        && (Table1.JournalType == JournalType::JournalType2 
                            || Table1.JournalType == JournalType::JournalType1 )).Id;
            return IdLastCreated;
        }
        else
        {
            throw error(/error/);
        }
    }
}


How would I refactor the code to make it better? is the structure i did good from architecture and clean code point of view?
I have the same question (0)
  • Hana Xue Profile Picture
    Microsoft Employee on at
    Hi,
    Regarding this issue, please be patient and wait for developer support. And hope the links below will give you some hints.
    Class extension model in X++ - Finance & Operations | Dynamics 365 | Microsoft Learnrahulmsdax.blogspot.com
    Best Regards,
    Hana

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 583 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 378

#3
Diego Mancassola Profile Picture

Diego Mancassola 255

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans