Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

Trying to delete invoice journals that haven't been posted - AP/invoice journal - need help

(0) ShareShare
ReportReport
Posted on by 155

HI ,

We use AX2012 RTM version. I need to delete some open invoice journals and here is the simple code that I have in a job

static void deleteJournalNonService(Args _args)
{
    LedgerJournalTable  journalTable;
    
    select forUpdate firstonly journalTable
            index TypeNumIdx
            where journalTable.JournalNum == 'XXXXX';
    
    if(journalTable)
    {
        ttsBegin;
            journalTable.delete();
        ttsCommit;
    }
    
   info(strFmt('%1 has been deleted', journalTable.JournalNum));
}

It is failing at the line of code journaltable.delete();

When I debugged it is not finding the record in the LedgerJournalTable here (I see there is a record in the LedgerJournalTable) but for some reason it can't find the record in the below method

pastedimage1612215485390v1.png

pastedimage1612215630617v2.png

As I can't find the record then it is failing at the below line of code

pastedimage1612215755942v4.png

and it finally throwing the error

pastedimage1612215782032v5.png

Number sequence 0 doesn't exist. I am not sure what I am missing here. It finds the record my job when it executes select forupdate line of code but it is not when it is looking 

in the below code during the debugging (which I already posted) 

static LedgerJournalTable find(LedgerJournalId  _ledgerJournalId,
                               boolean          _forUpdate = false)
{
    LedgerJournalTable ledgerJournalTable;

    if (_ledgerJournalId)
    {
        ledgerJournalTable.selectForUpdate(_forUpdate);

        select firstonly ledgerJournalTable
            index TypeNumIdx
            where ledgerJournalTable.JournalNum == _ledgerJournalId;
    }
    return ledgerJournalTable;
}

Am I missing somethin here or anyone ran into this scenario before?

Thanks. 

  • André Arnaud de Calavon Profile Picture
    293,027 Super User 2025 Season 1 on at
    RE: Trying to delete invoice journals that haven't been posted - AP/invoice journal - need help

    Hi Daxing,

    Has your issue been solved using the code you provided in the last reply or do you still need some help here?

    Indeed, the lines need to be deleted first.

  • Suggested answer
    Daxing Profile Picture
    155 on at
    RE: Trying to delete invoice journals that haven't been posted - AP/invoice journal - need help

    The LedgerJournallines need to be deleted first and then the LedgerJournalTable header record. Here is the code

    while select forupdate ledgerJournalTable

               where ledgerJournalTable.<SomeIndexColumn> == <value>

                   && !ledgerJournalTable.Posted

           {

                   ttsBegin;

                   delete_from ledgerJournalTrans

                       where ledgerJournalTrans.journalNum == ledgerJournalTable.journalNum;

                   ledgerJournalTable.delete();

                   ttsCommit;

          }

  • Daxing Profile Picture
    155 on at
    RE: Trying to delete invoice journals that haven't been posted - AP/invoice journal - need help

    HI Andre, Yes, when I tried to delete the Journal on the header, then it asked to delete the lines first. it successfully deleted the lines and then  it let me the header after that. During the debugging process while deleting the header, I see that it finds the journal in the LedgerJournalTable find() method as expected and deletes the lines and header separately manually just fine and errors were thrown. But it can't find the journal record in the LedgerJournalTable find() method when I execute the code in the job to delete the journal(s). 

  • André Arnaud de Calavon Profile Picture
    293,027 Super User 2025 Season 1 on at
    RE: Trying to delete invoice journals that haven't been posted - AP/invoice journal - need help

    Hi Daxing,

    Do you get the same error when you manually delete the journal from the user interface?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,027 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,852 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans