Thursday, April 28, 2022

Use of Dictionary and List of Text Variable in Business Central

Hello Friends,

We can Lit of [Text] or Dictonaroty Variable for multiple business needs.

Here are example how we can use it practically

Use of Dictionary Variable to save Key and Value and Get from Index (List of text) List of Dictionary


Var
---
FieldValues: Dictionary of [Text, Text];

Add Code to for Key and Value
-----------------------------
Clear(FieldValues);
FieldValues.Add('InvoiceNumber', OriginalInvoiceNumber);

Below code will help to find all key and value and add in code
--------------------------------------------------------------
for i := 1 to FieldValues.Keys().Count() do
 if (FieldValues.ContainsKey(FieldValues.Keys().Get(i))) then
    NewInvoice := Replace(NewInvoice, FieldValues.Keys().Get(i),
FieldValues.Values().Get(i));

-------------------
Same way below code we use for get key and find value

LineHash: Dictionary of [Code[20], Decimal];

if not LineHash.ContainsKey(Rec."No.") then begin

    AvailableToSell := 0;

    if Rec.Type = Rec.Type::Item then begin

    Item.Get(REc."No.");

    If Item.Type = Item.Type::Inventory then

        AvailableToSell := ItemAvailability.GetAvailabletoSell(Item,
Rec."Location Code", Rec."Shipment Date");

    end;

    LineHash.Add(Rec."No.", AvailableToSell);

end;

exit(LineHash.Get(Rec."No."));

Use List of Text variable

field(LookUpCustomer; LookUpCustomer)
                    {
                        ApplicationArea = All;

                        trigger OnLookup(var Text: Text): Boolean
                        var
                            CustomerList_Page: Page "Customer List";
                            Customer_lRec: Record Customer;
                            i: Integer;
                        begin
                            Customer_lRec.Reset();
                            Clear(CustomerList_Page);
                            CustomerList_Page.Editable(false);
                            CustomerList_Page.LookupMode(true);
                            CustomerList_Page.SetTableView(Customer_lRec);
                            IF CustomerList_Page.RunModal = Action::LookupOK Then begin
                                CustomerList_Page.GetSelectionPIPE_gFnc(LookUpCustomer);
                                CustomListOfText_gList := LookUpCustomer.Split('|');

                                For i := 1 to CustomListOfText_gList.Count Do begin
                                    //Message(CustomListOfText_gList.Get(i));
                                    IF CustomListOfText_gList.Contains('CB000004') then
                                        Message('Yes')
                                    Else
                                        Message('No');
                                end;
                            end;
                        end;
                    }


    var
        LookUpCustomer: Text;
        CustomListOfText_gList: List of [Text];


https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/list/list-data-type

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/dictionary/dictionary-data-type

Thank you for reading

I hope this will help someone

Keep Sharing...Keep Growing....

No comments:

Post a Comment