Records, RecordIDs, and RecordRefs OH MY!

bhalpinbhalpin Member Posts: 309
Arg.

Ok, I grew up on MS C/Side documentation examples like:

Sally works in finance, and likes cats. Not all cats, just fluffy ones. Sally wants to know how often a customer has ordered a particular item. So she asks Todd in I.T., but he' been unable to overcome his breakup with Sally and is off sick from severe allergy reactions to her cat(s). So she turns to Todd's boss Anne. She escalates her request to management with a proposal that includes new servers, a company car, and a corner office. When in that office she turns to Mibuso and within five minutes has his answer. Copy - Paste - and Viola!

:-)

Ok (again) - I'm Todd - sort-of. (I'm not allergic to cats - just crummy documentation.) All I need is a nudge here. (And maybe win Sally back)

I've read about and played with RecordRefs, etc, and have a headache. This is all I want to do:


Function1
Customer.GET('C1');
or
Item.GET(I1);
// Whatever

Now I want to call Function2 with some sort of variable that Function2 can determine what table number / record type (Customer or Item) the caller is sending, and what the key field(s) are for that record.





Function 2
// Takes a parameter that - like specified above - allows me to determine what table number (Customer or Item) I'm called with, and the primary key(s) of that record. I then want to retrieve that record using a 'normal' record variable using a CASE statement like

CASE TableNo OF
## - Get a ## record using primary key value(s)
### - Get a ### record using primary key value(s)


I can't promise a company car for help here - only my sincere thank!

Bob


Comments

  • AluanAluan Member Posts: 158
    Hello bhalpin,
    if you pass the recordid you can work with recref and settable.

    Example:
    You are at the Item Card and press a button. Inside OnAction() you pass the recordid.

    TestButton - OnAction()
    GetMyRecord(Rec.RECORDID);
    

    GetMyRecord(recid : RecordID)
    LOCAL GetMyRecord(recid : RecordID)
    
    IF recref.GET(recid) THEN BEGIN
      recref.SETTABLE(recItem);
      MESSAGE(recItem."No.");
    END;
    

    Parameter:
    Var	Name	DataType	Subtype	Length
    No	recid	RecordID
    

    Variables:
    Name	DataType	Subtype	Length
    recref	RecordRef		
    recItem	Record	Item
    

    In your case you can simple expand it with the case function. You just have to use recid.TABLENO.
  • bhalpinbhalpin Member Posts: 309
    Just what I wanted. Thanks!

    But, no joy. I tried a simple example (NAV 2015) and get this error:

    6tgtwgjpfwsw.png


  • AluanAluan Member Posts: 158
    edited 2022-01-07
    Oh - that's unfortunate. Sorry.
    I guess it was just added later.

    Anyway,
    in that version you should be able to use the recref as parameter.
    Example:

    TestButton - OnAction()
    recref.GETTABLE(Rec);
    GetMyRecord(recref);
    

    Variable:
    Name	DataType	Subtype	Length
    recref	RecordRef
    

    GetMyRecord(recid : RecordID)
    LOCAL GetMyRecord(recref : RecordRef)
    
    recref.SETTABLE(recItem);
    MESSAGE(recItem."No.");
    

    Parameter:
    Var	Name	DataType	Subtype	Length
    No	recref	RecordRef
    

    Variable:
    Name	DataType	Subtype	Length
    recItem	Record	Item
    

    With recref.NUMBER you can get the Table ID.
  • AlexDenAlexDen Member Posts: 85
    Hi,

    You can create a function with Variant variable as parameter and pass any record/recordref/recordid to it.
    Inside the function you can convert variant to RecordRef;
    GetRecordRef(Variant: Variant)
    CASE TRUE OF
      Variant.ISRECORD:
        RecRef.GETTABLE(Variant);
      Variant.ISRECORDREF:
        RecRef := Variant;
      Variant.ISRECORDID:
        RecRef.GET(Variant);
      ELSE
        ERROR('Invalid parameter');
    END;
    Message('%1', RecRef.RECORDID);
    
  • bhalpinbhalpin Member Posts: 309
    Thanks to both! I have a solution now.

    I'm still curious why Record.RECORDID doesn't compile though.

    Life is too short to dwell on it though.

    Thanks again!
Sign In or Register to comment.