procedure RenameSalesOrder10000()
var
SalesHeader: Record "Sales Header";
begin
//Get Order '10000'
SalesHeader.Get(SalesHeader."Document Type"::Order, '10000');
//Change value of "No." to '42' (part of Primary Key)
SalesHeader.Rename(SalesHeader."Document Type"::Order, '42');
end;
//Error due to type mismatch
//System will try change enum(integer) "Document Type" to '42'(text) and "No." to empty value
SalesHeader.Rename('42');
//Error because you can't change value to initial e.g. int/enum/option 0 or empty text
//System will try change enum(integer) "Document Type" to Quote(0) and "No." to '42'
SalesHeader.Rename(SalesHeader."Document Type"::Quote, '42');
//Error due because you try to pass empty value to "No."
//System will try change enum(integer) "Document Type" to Order and "No." to empty value
SalesHeader.Rename(SalesHeader."Document Type"::Order);
local procedure GetRecRefPKFieldNosInOrder(RecRef: RecordRef) ListOfPKFieldNo: List of [Integer]
var
FieldRefVar: FieldRef;
KeyRefVar: KeyRef;
KeyCount: Integer;
begin
//Usually Primary Key is first key of table, so we just get key №1 and assign it to KeyRef variable
KeyRefVar := RecRef.KeyIndex(1);
//Read each field index from key in loop
for KeyCount := 1 to KeyRefVar.FieldCount() do begin
//Get each field to FieldRef variable by field index
FieldRefVar := KeyRefVar.FieldIndex(KeyCount);
//Add each field number to list
//We can't use values just because values can have different data types
//List doesn't support variant data type, so we just store numbers for now
ListOfPKFieldNo.Add(FieldRefVar.Number());
end;
end;
procedure RenamePKValueOfRecRef(var RecRef: RecordRef; FieldNoToChange: Integer; NewValueAsVariant: Variant)
var
ListOfPKFieldNo: List of [Integer];
ListOfPkValuesAsTxt: List of [text];
begin
//Get list of PK field numbers from RecordRef variable
ListOfPKFieldNo := GetRecRefPKFieldNosInOrder(RecRef);
//Check if field to change is part of Primary Key from RecordRef variable
if not ListOfPKFieldNo.Contains(FieldNoToChange) then
exit;
//Let's say we have function to get all values as txt
//Also, we get NEW value for required field
//Attention! It's wrong approach because our fields can have any type
//If you would like to see how avoid this problem, just take look for my Data Editor github project
//https://github.com/Drakonian/data-editor-for-bc
ListOfPkValuesAsTxt := GetListOfValuesFromFieldNumberListWithNew(RecRef, ListOfPKFieldNo, FieldNoToChange, NewValueAsVariant);
//Now we have to call Rename() method to update required field from PK
//But how? You need to pass all data in same order per one field LOL
case ListOfPKFieldNo.Count() of
1:
RecRef.Rename(ListOfPkValuesAsTxt.Get(1));
2:
RecRef.Rename(ListOfPkValuesAsTxt.Get(1), ListOfPkValuesAsTxt.Get(2));
3:
RecRef.Rename(ListOfPkValuesAsTxt.Get(1), ListOfPkValuesAsTxt.Get(2), ListOfPkValuesAsTxt.Get(3));
//...
//keep doing it for maximum number of fields in PK xD
20:
RecRef.Rename(ListOfPkValuesAsTxt.Get(1), ListOfPkValuesAsTxt.Get(2), ListOfPkValuesAsTxt.Get(3),
ListOfPkValuesAsTxt.Get(4), ListOfPkValuesAsTxt.Get(5), ListOfPkValuesAsTxt.Get(6),
...
ListOfPkValuesAsTxt.Get(20));
end;
end;
RecRef.Rename(ListOfValues);
var
ListOfVariant: list of [Variant];
RecRef.Rename(ListOfVariant);