Wednesday, November 5, 2014

Sample Copy Data from Template Table by using RecordRef



Just trying to create this sample for my reference purpose in future. Refer to CU 8611 (Migration Management) and CU 8612 (Template Management) for more detail....
OnRun()
TemplateTable.RESET;
IF TemplateTable.FINDSET THEN REPEAT
  FromRecRef.GETTABLE(TemplateTable);
  ToRecRef.GETTABLE(DestinationTable);
  ToRecRef.INIT;
  FieldRec.SETRANGE(TableNo, FromRecRef.NUMBER);
  FieldRec.SETRANGE(Enabled, TRUE);
  FieldRec.SETRANGE(Class, FieldRec.Class::Normal);
  IF FieldRec.FINDSET THEN BEGIN
    InsertRecordWithKeyFields(ToRecRef,FromRecRef);
    REPEAT
      IF NOT IsKeyField(FieldRec.TableNo,FieldRec."No.") THEN BEGIN
        FromFieldRef := FromRecRef.FIELD(FieldRec."No.");
        ToFieldRef := ToRecRef.FIELD(FieldRec."No.");
        ValidateFieldValue(ToRecRef,ToFieldRef,FORMAT(FromFieldRef.VALUE));
      END;
    UNTIL FieldRec.NEXT = 0;
    ToRecRef.MODIFY(TRUE);
  END;
  CLEAR(FromFieldRef);
  CLEAR(ToFieldRef);
  FromRecRef.CLOSE;
  ToRecRef.CLOSE;
UNTIL TemplateTable.NEXT = 0;
MESSAGE('Done !!!');



ValidateFieldValue(VAR RecRef : RecordRef;VAR FieldRef : FieldRef;Value : Text[250])
Field.GET(RecRef.NUMBER,FieldRef.NUMBER);

IF Field.Type <> Field.Type::Option THEN BEGIN
  IF Value <> '' THEN
    EVALUATE(FieldRef,Value);
END ELSE
  IF GetOption(Value,FieldRef.OPTIONCAPTION,OptionAsInteger) THEN
    FieldRef.VALUE := OptionAsInteger;

FieldRef.VALIDATE;

GetOption(Value : Text[250];OptionString : Text[1024];VAR OptionInteger : Integer) Valid : Boolean
IF Value = '' THEN
  EXIT;
IF EVALUATE(OptionInteger,Value) THEN BEGIN
  EXIT(TRUE);
END;

ValueInOptionString := FALSE;

CommaPos := STRPOS(OptionString,',');

WHILE NOT ValueInOptionString AND (CommaPos > 0) DO BEGIN
  IF STRPOS(OptionString,',') <> 0 THEN
    Option := COPYSTR(OptionString,1,STRPOS(OptionString,',')-1)
  ELSE
    Option := OptionString;
  IF Option = Value THEN
    ValueInOptionString := TRUE
  ELSE
    OptionPos := OptionPos + 1;
  OptionString := DELSTR(OptionString,1,CommaPos);
  CommaPos := STRPOS(OptionString,',');
END;

IF OptionString = Value THEN
  ValueInOptionString := TRUE;

OptionInteger := OptionPos;
EXIT(ValueInOptionString);

InsertRecordWithKeyFields(VAR RecRef : RecordRef;_RecRef : RecordRef)
_KeyRef := _RecRef.KEYINDEX(1);
KeyRef := RecRef.KEYINDEX(1);
FOR KeyFieldCount := 1 TO KeyRef.FIELDCOUNT DO BEGIN
  _FieldRef := _KeyRef.FIELDINDEX(KeyFieldCount);
  FieldRef := KeyRef.FIELDINDEX(KeyFieldCount);
  ValidateFieldValue(RecRef,FieldRef,FORMAT(_FieldRef.VALUE));
END;

RecRef1 := RecRef.DUPLICATE;

IF RecRef1.FIND('=') THEN BEGIN
  RecRef := RecRef1;
  EXIT
END;

RecRef.INSERT(TRUE);

IsKeyField(TableID : Integer;FieldID : Integer) : Boolean
RecRef.OPEN(TableID);
KeyRef := RecRef.KEYINDEX(1);
FOR KeyFieldCount := 1 TO KeyRef.FIELDCOUNT DO BEGIN
  FieldRef := KeyRef.FIELDINDEX(KeyFieldCount);
  IF FieldRef.NUMBER = FieldID THEN
    EXIT(TRUE);
END;

EXIT(FALSE);

No comments:

Post a Comment