Skip to main content

Notifications

Announcements

No record found.

Concatenation of strings — Round 2

This post is the continuation of the previous post of mine on the same topic.
In the last post, I was discussing a requirement which is very specific.
When I shared the post with my colleagues, they want to rework it towards a generic requirement. Feel free to check out here.

A short story: I used Variants, yes a very old datatype in NAV, developers never ever wanted to use it unless someone asked them to use! Thank you Rasitha Chandrasekara for the valuable tips.

Code next…
The first part of the code will have the necessary filtration of the Item master table. Then the function call happens with appropriate parameters.
The second and last part of the code will be the function itself. Let’s jump in.

The first part of the code…


Item.SETRANGE(Item.Critical, TRUE);
IF Item.FINDSET THEN
BEGIN
  MESSAGE(ConcatCU.ConcatStrings(Item,
             Item.FIELDNO(Item."No."), '|'));
END;

ConcatCU is the codeunit, ConcatStrings is the function which takes three parameters A Variant, The Field No., and the Sign to be used to concatenation.

 


ConcatStrings(ConcatVariant : Variant;FieldNo : Integer;
                 SignValue : Code[1]) : Text

CLEAR(FilterText);
IF ConcatVariant.ISRECORD THEN
  ConcatRecRef.GETTABLE(ConcatVariant)
ELSE
  ConcatRecRef := ConcatVariant;

IF ConcatRecRef.FINDSET THEN
BEGIN
  REPEAT
    FieldRef := ConcatRecRef.FIELD(FieldNo);
    FieldValue := FieldRef.VALUE;

    IF FilterText = '' THEN
      FilterText := FieldValue
    ELSE
      FilterText += SignValue + FieldValue;

  UNTIL ConcatRecRef.NEXT = 0
END;

EXIT(FilterText);

The second part of the code actually does the concatenation with the parameters you passed in. Since the first parameter is Variant, you can pretty much pass anything.

I have done it for records only, hence, I used RecordRef.GETTABLE() to retrieve the Item Table. The sign can be a pipeline(|) or an and(&) or even double periods(..) if you use it appropriately.

The final concatenation is 1000|1001|1100|1110|1120|1150


#TIL: You can only pass by value, not pass by reference when you are passing record variables as variants.

When I try to concatenate a Code and a FieldRef, the compilation error message is bit funny.

2

3

Code + Joker

That’s coincidence because my PC wallpaper is featuring Joker!

4

If you like to get this wallpaper, head over to justinmaller.com.

 



This was originally posted here.

Comments

*This post is locked for comments