sharepointpowerappspower-platform

How to reference ThisRecord.Value inside of a LookUp function?


I've created this code inside BarcodeReader in an OnScan event. I am trying to first check if the barcode scanned exists in a Microsoft list named barcode_inventory and if it does, it will get added to a list named signed_out.

My problem is that ThisRecord.Value does not work inside of the LookUp function for some reason, is there any way I can reference it within the LookUp function?

ForAll(
    Self.Barcodes,
    If(
        !IsBlank(
            LookUp(barcode_inventory,Barcode = Value(ThisRecord.Value), Barcode)
        ),
        Collect(
            signed_out,
            {
                Barcode: Value(ThisRecord.Value),
                Product: Text(LookUp(barcode_inventory,Barcode = Value(ThisRecord.Barcode)).ProductNameSize)
            }
        ),
        Notify("Could not find record.", NotificationType.Error)
    )
)


Solution

  • The ForAll function creates a scope which has the ThisRecord object that you are trying to reference. But the LookUp function also creates its own scope, and the ThisRecord always refers to the nearest scope.

    If you want to reference the scope created in the ForAll function, you can use the As keyword to give it a name, and reference it with that:

    ForAll(
        Self.Barcodes As TheBarcode,
        If(
            !IsBlank(
                LookUp(barcode_inventory,Barcode = Value(TheBarcode.Value), Barcode)
            ),
            Collect(
                signed_out,
                {
                    Barcode: Value(TheBarcode.Value),
                    Product: Text(LookUp(barcode_inventory,Barcode = Value(TheBarcode.Barcode)).ProductNameSize)
                }
            ),
            Notify("Could not find record.", NotificationType.Error)
        )
    )
    

    By the way, you can make this a little more efficient by caching the LookUp value with the With function, so it doesn't have to be performed twice:

    ForAll(
        Self.Barcodes As TheBarcode,
        With(
            { productFromInventory: LookUp(barcode_inventory,Barcode = Value(TheBarcode.Value) },
            If(
                !IsBlank(productFromInventory.Barcode),
                Collect(
                    signed_out,
                    {
                        Barcode: Value(TheBarcode.Value),
                        Product: Text(productFromInventory.ProductNameSize)
                    }
            ),
            Notify("Could not find record.", NotificationType.Error)
        )
    )