microsoft-dynamicsdynamics-business-centraldynamics-albusinesscentral

Get SCC code (barcode in "Item Reference" table) with Business Dynamics extension


I have created a custom extension and I need to get the SCC number (Shipping Container Code) from some items. The SCC number is a barcode that goes on the shipping container vs on the individual items. I know that the number is in a related field called Item Reference but I do not know how to fetch the information. In looking at the structure of the table, it appears to me that some of the primary keys would be impossible for me to get and, in fact, one of the PKs is the very data that I am trying to get

"Item No."                       Code[20] (PK1)
"Variant Code"                   Code[10] (PK2)
"Unit of Measure"                Code[10] (PK3)
"Reference Type"                 Enum Microsoft.Inventory.Item.Catalog."Item Reference Type" (PK4)
"Reference Type No."             Code[20] (PK5)
"Reference No."                  Code[50] (PK6)

I believe that the Reference No. is the number that I need but I must admit that I am still quite new to Business Central/Dynamics and the AL language.

How can I get the SCC number that seems to reside in this Item Reference table?


Solution

  • You need to set the relevant filters on Item Reference and fetch the record within that filter.

    I assume you have an Item variable of type Record Item and a ItemReference variable of type Record "Item Reference". The value you are looking for would be found in the Reference No. field.

    To link the ItemReference to your Item together you need to filter on "Item No.":

    ItemReference.SetRange("Item No.", Item."No.");
    

    Secondly you need to find the correct type of reference by filtering on Reference Type and possibly Reference Type No. depending on which Reference Type you filter on. Reference Type can be either " " (blank), Customer, Vendor or Bar Code. Filtering on "Reference Type No." is only relevant for Customer and Vendor.

    Assuming that Shipping Container Code is not a bar code we will filter on " " (blank):

    ItemReference.SetRange("Reference Type", ItemReference."Reference Type"::" ");
    

    Lastly you need to choose if you want the first, last og all matches within your filter. This is done by calling FindFirst, FindLast og FindSet on ItemReference:

    ItemReference.FindFirst(); // First match
    ItemReference.FindLast(); // Last match
    ItemRefrence.FindSet(); // All matches. Must be paired with repeat-statement to loop through results
    

    Be aware that all the Find*-calls above will fail if there are no matches unless guarded by an if-statement.