Solved

BQL Fluent statement not selecting a record when there is a null in one of SubItemID field

  • 21 August 2023
  • 5 replies
  • 150 views

Userlevel 6
Badge +3

In the following BQL Fluent statement, I am selecting a record from the POVendorInventory table.

In the table, the record has a NULL in the SubItemID field.  When there is a NULL in that field, this returns no record even though my parameter “reg.SubItemID’ is passing a NULL.

That field is part of a unique index.  My select statement is making sure that if a record already exists with the key fields, it does not add a duplicate.

POVendorInventory test = SelectFrom<POVendorInventory>
                    .Where<POVendorInventory.vendorID.IsEqual<@P.AsInt>
                        .And<POVendorInventory.inventoryID.IsEqual<@P.AsInt>>
                        .And<POVendorInventory.purchaseUnit.IsEqual<@P.AsString>>
                        .And<POVendorInventory.vendorLocationID.IsEqual<@P.AsInt>>
                        .And<POVendorInventory.subItemID.IsEqual<@P.AsInt>>>
                    .View.Select(graph, bAccountTo.BAccountID, reg.InventoryID, reg.PurchaseUnit, locationIDToUse, reg.SubItemID);

In debug, test is null.

If I do a select statement in SQL on the table using those parameter values, it retrieves the record.

SELECT * FROM POVendorInventory
WHERE 
    VendorID = 7117
    AND InventoryID = 10102
    AND VendorLocationID = 8161
    AND PurchaseUnit = 'LS'
    AND SubItemID IS NULL
 

If I edit the table and put in an actual integer in the SubItemID field and pass it as the SubItemID parameter, I am able to retrieve the record in my BQL statement.

In my select statement, is there something I need to add to it in the case that the field is NULL?

 

icon

Best answer by davidnavasardyan09 22 August 2023, 01:11

View original

5 replies

Userlevel 7
Badge +9

Hi @Joe Schmucker,

 

Try using the FBQL   IfNullThen<> instead of the traditional BQL IsNull

 

Screenshot from the developer guide - https://www.acumatica.com/media/2019/06/2019-R1-Framework-Dev-Guide.pdf

 

 

Thanks

Chandra

 

Userlevel 5
Badge +1

Hello @Joe Schmucker 

The issue you're facing is due to the behavior of the .IsEqual method when dealing with null values in BQL Fluent. Instead of generating an IS NULL condition in the SQL query, it generates an = NULL condition, which will not return the expected results.

You can handle null values in your BQL Fluent statement using the .IsNull and .Else methods to generate the appropriate IS NULL condition when the parameter is null.

Here's an example of how you can modify your BQL Fluent statement to handle null values in the SubItemID field:

 

POVendorInventory test = SelectFrom<POVendorInventory>
.Where<POVendorInventory.vendorID.IsEqual<@P.AsInt>
.And<POVendorInventory.inventoryID.IsEqual<@P.AsInt>>
.And<POVendorInventory.purchaseUnit.IsEqual<@P.AsString>>
.And<POVendorInventory.vendorLocationID.IsEqual<@P.AsInt>>
.And<Brackets<POVendorInventory.subItemID.IsNull.And<@P.AsInt.IsNull>
.OrElse<POVendorInventory.subItemID.IsEqual<@P.AsInt>>>>>
.View.Select(graph, bAccountTo.BAccountID, reg.InventoryID, reg.PurchaseUnit, locationIDToUse, reg.SubItemID);

In this modified BQL Fluent statement, we first check if both POVendorInventory.subItemID and the passed parameter are null using the .IsNull method. If both are null, the condition is satisfied. Otherwise, if the parameter is not null, we use the .Else method to compare the subItemID field to the parameter using the .IsEqual method.

This should allow you to handle both null and non-null values in the SubItemID field in your BQL Fluent statement.

Userlevel 6
Badge +3

@ChandraM  Thank you for your effort to assist me!

@davidnavasardyan09  This is great.   I will be copying/pasting this again someday in the future!

Userlevel 6
Badge +3

One little change… I had to change the OrElse to Or.  VS didn’t like OrElse.  

 

Userlevel 7
Badge +9

Hi @Joe Schmucker Thanks for the update on the workaround for OrElse.

 

Thanks

Chandra

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved