Add character to integer field

What is the easiest way to add the number 3 to an existing Integer field - for instance Phone No. on the Customer table and do it for all Customers?

Example - The Phone Number is 789 but i need to put 3 in front so that it becomes 3789 and do that for all records…

Insert that codeunit in a codeunit and run.

recCust is a Record of type Record.

IF recCust.FIND(‘-’) THEN
REPEAT
recCust.VALIDATE(”Phone No.”, ‘3’ + recCust. ”Phone No.”);
recCust.MODIFY(TRUE);
UNTIL recCust.NEXT = 0;

I would do it a little different (with all good practices in it). (the example of **Nuno Maia** will work correctly)
I know it is just a program to put a 3 before it, but it is an example everyone can see and use for programming, so it is best a state-of-the-art program:

-Always use RESET-SETCURRENTKEY-SETRANGE(or SETFILTER) because programs become easier to read and adapt for others
-If you are looping, use FINDSET
-If you are looping and in the loop, you change the record, always use another buffer (even if it is not necessary) because if later someone has to change your program, at least it won’t create problems.

recCust.RESET;
recCust.SETCURRENTKEY(“No.”);
IF recCust.FINDSET(TRUE,FALSE) THEN
REPEAT
recCust2 := recCust;
recCust2.VALIDATE(”Phone No.”, ‘3’ + recCust2. ”Phone No.”);
recCust2.MODIFY(TRUE);
UNTIL recCust.NEXT = 0;

Why TRUE,FALSE when you’re not modifying the record inside the loop? Is there a reason for that or is it just a typo, when I use a different buffer I use FALSE, FALSE.

To be honest: I am not completely sure it needs to be done. In the manual is written to use the FIRST TRUE if you are changing the record in the loop. I am not sure if they mean with that the record-variable or the record even if in another variable. I just take the safest way.

In general I even use (TRUE,TRUE) even if I don’t change the fields of the index. I do it in case later someone has to add some fields that belongs to the key and forgets to change (TRUE,FALSE) to (TRUE,TRUE).

I have to admit, when I’m in hurry and I have to create temporary codeunit / report to make a change that is only to be done once. I create code such has the first thread. I know that isn’t optimized for performance but rush can kill good coding.[:P]

Yes, both examples works. Thank you for showing me.

Nuno Maias example is straight forward and easy to understand…

I also understand the logic in your example. But could you elaborate on the advantages of using recCust 2? As i read your post its because:

“If you are looping and in the loop, you change the record, always use another buffer (even if it is not necessary) because if later someone has to change your program, at least it won’t create problems.” But why is that easier?

Btw.

What if i want to add the character on place 3? For example phone number is 123456 and i want to add 7 or - so it would be 1273456 or 12-3456? How is that achieved?

recCustomer2 : if you use a certain key and you change the value of a field that is in that key, it is possible Navision skips records or goes into an endless loop.

So to avoid it, your original record keeps all its values and you work on another record.

Always use this way for safety. It is possible that the field you change is NOT in the index, so you DON’t need to use that second buffer. But if in the future you (or worse:someone else) has to change something (change key or add a field to change) so that the field that is changed is in the key, you need a second buffer. If you already have it:no problem. If you don’t:you have to change your program (hopefully you remember to change it;or if it is someone else:hopefully he knows he has to change it).

charactr on place 3:

txtThePhone := COPYSTR( txtThePhone,1,2) + ‘-’ + COPYSTR(txtThePhone,3);

Thx kriki - that works perfectly [Y]