check if record already exists

I try to check if record already exists in database, like this:

“”.“Chamber of Commerce no.”:=‘3562922’;
IF “”.INSERT THEN
MESSAGE(‘not Exists’,"".“Chamber of Commerce no.” )
ELSE
ERROR(‘already exists’, “”.“Chamber of Commerce no.” );

But this doesnt work.

Thank you

Hi,

your code only works if the customer no. is also filled with the one in the database. The insert throws an error if the primary key is already taken. If you want to check a non primary key field, you must solve this in another way. Like setting filter on it and check if the rec.isEmpty :wink: (BTW than set an Key too, if you want to improve the performance!)

kind regards,
Matthias König

Thank you for your answare,
But if I do this:

“”.SETRANGE(“Chamber of Commerce no.”,"".“Chamber of Commerce no.”);
IF “”.FINDFIRST THEN
ERROR('Number already exists ');

It says every time that the record exists, even if the record doenst exists in the database.

Is that an report? Is that the Validate-Trigger?

In case of an report or another source, where the record is written to the database:
The number exists in this record, that you check.
Use another record variable for this purpose.

CustomerForChamberOfComCheck.SETRANGE(“Chamber of Commerce no.”,"".“Chamber of Commerce no.”);
IF CustomerForChamberOfComCheck.COUNT > 1 THEN
ERROR('Number already exists ');

OR (would be mine solution for this):

CustomerForChamberOfComCheck.SETFILTER(“No.”, ‘<>%1’, “”.“no.”);
CustomerForChamberOfComCheck.SETRANGE(“Chamber of Commerce no.”,"".“Chamber of Commerce no.”);
IF NOT CustomerForChamberOfComCheck.ISEMPTY THEN
ERROR('Number already exists ');

(Code is not testet)

It is with a xml port. So I have xml values, and one of the xml values is a chambre of commerce number. If that number already exists in the database, then no import have to be done

Thank you!! It works now :slight_smile: