Dynamics 365 Business Central: How to check whether a value has been assigned to a GUID (Check GUID is Null)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk briefly about how to check whether a value has been assigned to a GUID (check GUID is Null) in AL.

The GUID is a 16-byte binary data type that can be logically grouped into the following subgroups: 4byte-2byte-2byte-2byte-6byte.
The standard textual representation is {12345678-1234-1234-1234-1234567890AB}. The GUID is useful when you want to uniquely identify data so that it can be exchanged with external applications.

Some standard examples:
SystemId field: The SystemId field is a GUID data type field that specifies a unique, immutable (read-only) identifier for records in the table.

User Security Id, also known as User SID, is the unique identifier of the user, it is the primary key of the table 2000000120 User.

When the GUID data type is empty/null, {00000000-0000-0000-0000-000000000000} will be displayed on the page.
For example, when creating a new Microsoft Entra Application:

New custom added field:

A null GUID that consists only of zeros is valid but must never be used for references.

There is a small problem here. When you check whether the GUID field is empty/null in AL, if you write the following, no error will occur during compilation/installation, but an error will occur at runtime.

This is because the GUID value can also be represented as text. You can use the standard AL methods Format and Evaluate to convert from GUID values to Text values. If you do not use the correct format when you edit a GUID value in its textual format, the following error message is displayed:

Invalid format of GUID string. The correct format of the GUID string is: CDEF7890-ABCD-0123-1234-567890ABCDEF where 0-9, A-F symbolizes hexadecimal digits.

More details: Guid Data type Format

At this point you may realize that you need to change the code to the following. Yes, this works, but it’s not the best way to do it.

We can just use the following standard method:
System.IsNullGuid(Guid) Method: Indicates whether a value has been assigned to a GUID.

Syntax: Ok := System.IsNullGuid(Guid: Guid)

This method is a convenient way to check if a value has already been assigned to a GUID. For example,

Test video:

Very simple, give it a try!!!😁

Test code:

tableextension 50112 ZYCustomerExt extends Customer
{
    fields
    {
        field(50100; "User Security ID"; Guid)
        {
            Caption = 'User Security ID';
            DataClassification = CustomerContent;
            TableRelation = User."User Security ID";
        }
    }
}

pageextension 50112 ZYCustomerCardExt extends "Customer Card"
{
    layout
    {
        addafter(Name)
        {
            field("User Security ID"; Rec."User Security ID")
            {
                ApplicationArea = All;
            }
        }
    }

    actions
    {
        addafter(Contact)
        {
            action(CheckGUIDisNull)
            {
                Caption = 'Check GUID is Null';
                Promoted = true;
                PromotedCategory = Process;
                Image = Info;
                ApplicationArea = All;
                trigger OnAction()
                begin
                    if IsNullGuid(Rec."User Security ID") then
                        Message('GUID is Null')
                    else
                        Message('GUID is not Null');
                end;
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL