How to recover a number sequence just used ?

3.3k Views Asked by At

I have a simple question, I don't know if It's possible.

In my class I have a some methods, In one in particular I create a record in LedgerJournalTable and save the table reference in global variable _createdLedgerJournalTable.

So, in another method called after there is the possibility that I delete my createdLedgerJournalTable just created.

LedgerJournalTable deletedLedgerJournalTable ;
ttsBegin;
select forUpdate LedgerJournalTable 
where LedgerJournalTable .RecId == _createdLedgerJournalTable;

LedgerJournalTable .delete();
ttsCommit;

I know it's strange I can retrieve the Part Number of deleted records ?

Begin I delete teh record I can save the JournalNum and mark the number sequence is NOT used?

I have seen some examples of that is used mark the number sequence (example seen) , I can unlock the number sequence ?

Thanks in advice,

enjoy!

2

There are 2 best solutions below

0
On

If at all possible, avoid creating the journal header if you do not need it. For example create it just before the first insert of the journal line. Test if (!journalTable).

You can achieve what specify by calling newGetNum with the second parameter _makeDecisionLater set to true. Then call numberSeq.abort() to abort the number generation or numberSeq.use() to mark used.

This only works if in the same transaction scope as newGetNum and if the number sequence is continuous. if not, both calls are no-operations.

0
On

Here is my solution for a non-continuous sequence. Overwritting the delete method of the datasource. This could also be implemented in the delete method of the table:

public void delete()
{
    NumberSequenceTable numSeqTable;
    CMT_RutaId          tmpRutaId;
    ;

    tmpRutaId = CMT_RutaJour.RutaId;
    numSeqTable = NumberSequenceTable::find(SalesParameters::numRefCMT_RutaId().NumberSequence);

    super();

    if (numSeqTable.Continuous)
    {
        // Por si se decide marcar la secuencia como contínua, liberamos el número
        NumberSeq::release(SalesParameters::numRefCMT_RutaId().NumberSequence, tmpRutaId);
    }
    else
    {
        // Si el registro que estamos eliminando es el último que hemos insertado, recuperamos el número
        if (NumberSeq::numInsertFormat(numSeqTable.NextRec - 1, numSeqTable.Format) == tmpRutaId)
        {
            ttsbegin;
            numSeqTable = NumberSequenceTable::find(numSeqTable.NumberSequence, true);
            numSeqTable.NextRec--;
            numSeqTable.doUpdate();
            ttscommit;
        }
    }
}

And overwritting the close method of the form, to cover the case if the user exits the form without saving the record:

public void close()
{;
    if (CMT_RutaJour.RutaId && !CMT_RutaJour.RecId)
        CMT_RutaJour_ds.delete();

    super();
}