NAV 2015 - Copy a specific field of RecordRef

Dear all,

I had a function to copy data from 1 recordref to another.

I’m trying to copy a specific field but it’s not ok.

Here’s my code, it does not show error but it copy all the other fields (with the default value!). Please share your ideas about this?

FOR i := 1 TO FromTableRef.FIELDCOUNT DO BEGIN

FromFieldRef := FromTableRef.FIELDINDEX(i);

IF FromFieldRef.NUMBER = ‘specific Field ID here’ then

BEGIN

ToFieldRef := ToTableRef.FIELDINDEX(i);

ToFieldRef.VALUE := FromFieldRef.VALUE;

END;

END;

Thanks and best regards,

Hi Hai,

Why do you have a FOR-loop if you only need it to hand the specific field?

Also if you create a new record, then the remaining fields will always have the initial value. So what did you expect the values of the other fields to be? NAV doesn’t support NULL as such.

Dear Mr. Erik,

Thanks for your reply.

The loop is from my other function (to copy all data).
What i expect is that:

  • when create a new record: values of the other fields will have the initial value
  • when modify a record: values of the other fields remain the same

Thanks,

I need to see more of your code, like how do you handle update/insert of the ToTableRef variable.

Dear Mr. Erik,

Thanks for your reply, here is my full code to copy.

FromTableRef.OPEN(TableNo, FALSE, FromCompanyName);
ToTableRef.OPEN(TableNo, FALSE, ToCompanyName);

IF FromTableRef.FINDFIRST THEN
    REPEAT
        ToTableRef.INIT;
        FOR i := 1 TO FromTableRef.FIELDCOUNT DO
            BEGIN
                FromFieldRef := FromTableRef.FIELDINDEX(i);
				
				// filter here
				IF FromFieldRef.NUMBER = 'specific Field ID here' then
					BEGIN
						ToFieldRef := ToTableRef.FIELDINDEX(i);
						ToFieldRef.VALUE := FromFieldRef.VALUE;
					END;
            END;
    UNTIL FromTableRef.NEXT = 0;

ToTableRef.CLOSE;
FromTableRef.CLOSE;

Thanks,

What I mean is you don’t need to loop all the fields just to get one field.

FromTableRef.OPEN(TableNo, FALSE, FromCompanyName);
ToTableRef.OPEN(TableNo, FALSE, ToCompanyName);

IF FromTableRef.FINDFIRST THEN
    REPEAT
        ToTableRef.INIT;
        FromFieldRef := FromTableRef.FIELDINDEX(SpecificField.FieldNo);
		ToFieldRef := ToTableRef.FIELDINDEX(i);
		ToFieldRef.VALUE := FromFieldRef.VALUE;
		ToTableRef.MODIFY();
    UNTIL FromTableRef.NEXT = 0;

ToTableRef.CLOSE;