AX development validation

I am new to AX Development. Can you please help me with a code/procedure that signals a user if " Invoice Code AND Account Number" combination already exists in the database after he has input an already existing combination in a new line.and then sets focus on the textbox for the user to correct the duplication

NB>>>THE ACCOUNT NUMBER IS A DIMENSION(LedgerJournalTrans_AccountNum
LedgerJournalTrans(LedgerJournalTrans).LedgerDimension
AutoIdentification)

It seems that you’re using AX2012, correct? (Please always add a tag with AX version to your questions).

What’s the form in question? Where is the exception thrown from?

yes it is AX 2012, the form name is LedgerJournalTransVendInvoice,

select * from LedgerJournalTrans

if ( record with (Account && Invoice already exists in table)

then msg "Invoice account type combo already exists)

LedgerJournalTrans_Invoice.setfocus

LedgerJournalTrans_Invoice.cleartext

end if

Sorry, I didn’t get what you’re doing and what’s the thing you need a help with.

i want to add a feature that prevents the user from entering an invoice that already exists in the System/database,a message should be seen as soon as he “lostfocus” the invoice field. i had something like this:

LedgerJournalTrans_Invoice

method >>lostfocus

public void lostFocus()
{
//LedgerJournalTrans ledgerJournalTrans;
int64 _count;
;
_count = (select count(RecId) from ledgerJournalTrans where ledgerJournalTrans.Invoice == LedgerJournalTrans_Invoice.valueStr()).RecId;

if (_count > 0)
{
box::info(“An invoice with invoice number:” + LedgerJournalTrans_Invoice.valueStr() + " is already in the system",“Title text”,“Help text”);
LedgerJournalTrans_Invoice.text("") ;
LedgerJournalTrans_Invoice.setFocus();

}
}

lostFocus() is not the right method to use. validate() on the form data source field or validateField() on the table are much more appropriate choices.

You can also place such checks to validateWrite() - it’s safer than running the check of a field change only (you can call the same validation from both places). In this particular case, I would probably use formMethodDataSourceValidateWritePost() in LedgerJournalEngine_VendInvoice class (which is called from the form).

You should exclude the current record from your validation (e.g. transInDB.RecID != validatedTrans.RecId), otherwise users wouldn’t be able to change invoice ID after saving the line.

Instead of count(RecId), you may use firstOnly RecId.

Instead of Box::info(), use warning() or, more often, checkFailed() (which combines warning() and returning false). You’ll find many examples in the standard AX code.

Thank you very much,let me try build code along your suggestions