Send xRec and Rec

DarkSideDarkSide Member Posts: 46
I would like to create a generic function in a table that will be called from multiple tables. However, part of the parameters sent to the function will include the table (both xRec and Rec) that is calling it. So it would look something like this:

MyTable.MyGenericFunction(xRec,Rec);

Except this won't work as above or I'll need a function for every table. Is there a way to send a record variable without defining a table for it? If I send the table number and primary key to find the record I'm dealing with I'll loose the xRec, which is important for my needs.

I hope this makes sense. Thanks in advance.
My mommy says I'm special

Comments

  • ara3nara3n Member Posts: 9,256
    Hello DarkSide

    You can use Variant, and xVariant, and Integer

    The integer will be the Table ID.

    In your function

    You'll have a case statement for integer

    and do assignment to the table variable

    If TableID = 32 then begin
    ItemLedger := MyVariant;
    xItemLedger := xMyVariant;

    end;

    You would call the function

    Myfunction(Rec,xRec,database::"Item LEdger Entry");
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • cnicolacnicola Member Posts: 181
    Assuming you are using 3.60 and up I would recommend using RecordRef and FieldRef.

    Your generic function will have 2 parameters:

    CrtRecRef of type RecordRef
    xCrtRecRef of type RecordRef

    When you call this say from a specific table you just say:

    CrtRecRef.GETTABLE(Rec);
    xCrtRecRef.GETTABLE(xRec);

    and then you pass them to your generic function.

    Assuming you do want to know what the table no. is then recordref has a property called Number that will give you that.

    And just to venture a guess why you want to know both: if you want to see what changed then this solution is more elegant since you don't have to use a case and just use fieldref to traverse and compare the records.
    If that is the case then I would recommend looking at codeunit 423 - Change Log Management functions LogInsertion, LogModification, LogDeletion.

    Hope this helps.
    Apathy is on the rise but nobody seems to care.
  • DarkSideDarkSide Member Posts: 46
    Thanks guys this has gotten me a lot farther.

    I'm working on a way to "synchronzie dimensions" that are directly associated with fields in a table. For example, if the item table has an Item dimension, and an Item Category dimension I want them to be created, changed, renamed, deleted as I edit the item.

    I think this is possible by adding a call to a function in a table (yes more appropriate to use a codeunit but I'm trying to keep this small). The table will contain a list of Tables, Fields and Dimension codes. So, In my example above I would setup the following records in my table:
    27 1 ITEMNO
    27 5702 ITEMCATEGORY
    If a change were made to the Item Category field on the item card, the function will be called to change the ITEMCATEGORY dimension for the item being edited.

    Of course other parameters will be needed but I'm still having a problem. I've successfully sent RecRef and xRecRef to my function. I've started my loop to spin through the table 27 dimensions to "synchronize" but I'm trying to do the following code:

    IF RecRef.FIELD(DimSync."Field No.") <> xRecRef.FIELD(DimSync."Field No.") THEN BEGIN

    I get a type conversion error but I'm not sure why. Sorry, this is my first go with recordref and fieldref.

    Thanks again for any help.
    My mommy says I'm special
  • cnicolacnicola Member Posts: 181
    Hi Darkside,

    You need to use format on the values since the direct compare does not work. For a full example though, as I said in first post, look at Codeunit 423 funtion LogModification to see how it is done in a clean way (no matter how special some of us are we all can learn more :P ).

    I am still a little confused as to what exactly you are trying to achieve. How will you know what table you need to update if Item Category is changed? I mean you can hardcode the fields and what you need to do for each but then you might as well just put the code on OnValidate of the field.

    Plus now that I think more about it: Item Category field on Item table has a tablerelation to table Item Category. If I change that field on an item from CatA to CatB what exactly are you trying to change on the Item Category table?

    Also do you plan to have this happening only when user do manual changes or when any code is run?
    Apathy is on the rise but nobody seems to care.
Sign In or Register to comment.