Unmarking CustTrans Record

Occasionally our AR department will contact us with a customer transaction that is marked for payment, but they are unable to do anything with it. I wrote the following job to find to find the transaction and unmark it by clearing the SpecTrans record.


static void unmarkCustTrans(Args _args)
{
    SpecTrans           spec;
    CustTrans           custTrans;
    CustTransOpen       cto;
    CustAccount         custAcct = "834423";    // customer account number
    InvoiceId           inv = "5098723";        // invoice number
    PaymReference       pmr = "5169";           // payment reference
    ;

   select forupdate custTrans
        where custTrans.AccountNum == custAcct
        && custTrans.PaymReference == pmr
        && custTrans.Invoice == inv;

    if (custTrans.RecId)
    {
        select firstonly forupdate cto where cto.AccountNum == custTrans.AccountNum && cto.RefRecId == custTrans.RecId;

        if (cto.RecId)
        {
            spec = SpecTrans::findByRef("RDX", tablenum(CustTransOpen), cto.RecId, true);


            if (spec.RecId)
            {
                ttsbegin;
                spec.delete();
                ttscommit;

                info(strfmt("Transaction for invoice %1 unmarked.", inv));
            }
            else
            {
                info(strfmt("Marking for invoice %1 not found.", inv));
            }
        }
        else
        {
            info(strfmt("Open marking for invoice %1 not found.", inv));
        }
    }
    else
    {
        info(strfmt("Transaction for invoice %1 not found.", inv));
    }
}