0

Formula returning a record causes type mismatch

I have a formula that returns a record. This formula works fine. But whenever I try to use the returned record in a then or else branch, I get a type mismatch.

The formula field is OrderItems.Package:

let pkgName := PackageName;
first(select Packages where Name = pkgName)

 

Then I have another table Orders and there is a 1:n relation on field Orders.OrderItems (left-pointing arrow). Reading the formula from Orders works alright. For example, I can successfully popup the Package of the first record in OrderItems in an onClick trigger:

popupRecord(first(OrderItems.Package))

 

However, Ninox does not accept the following formula:

if PackageOverride != null then
    PackageOverride
else
    first(OrderItems).Package
end

 

Error message: type mismatch for then and else

What I am trying to do is return the Package field of the first record in OrderItems, unless the field PackageOverride is not null. In that case I want to return PackageOverride (which is also pointing to a package record, i.e. right-facing arrow in Orders).

Both the then-clause and the else-clause return the same type (a Package record) but somehow the Ninox parser doesn't realize that. I tried to remove the ) before the dot and put it at the end of the line - same problem.

One work-around I found is: return the Id of the Package record in both the then and the else clause, then after the end, do another select Package where Id = ... but this seems unnecessary overhead. The above code would be more elegant (and probably more efficient), but how do I get Ninox to understand that there is in fact no type mismatch?

2 replies

null
    • Fred
    • 1 mth ago
    • Reported - view

    Ninox has two ways to reference records, one through the table and record Id (called nid), and one with just the record Id (called rid).

    When you do a select, Ninox will return a nid. You can see this if you take your first bit of code and wrap the results in debugValueInfo() in a new formula field.

    let pkgName := PackageName;
    debugValueInfo(first(select Packages where Name = pkgName))
    

    You will see it returns:

    nid(data)
    

    Now if you create another formula field and put:

    debugValueInfo(first(OrderItems.Package))
    

    You will see:

    rid(data)
    

    So that is why Ninox is returning an error in the if statement. PackageOverride is nid while the else is rid.

    One work-around I found is: return the Id of the Package record in both the then and the else clause, then after the end, do another select Package where Id = ... but this seems unnecessary overhead.

    This works because now you have told Ninox to return just the record Id.

    I don't know why you need to do another select at the end. You have the record Id that you need with just adding Id.

    • Alain_Fontaine
    • 1 mth ago
    • Reported - view

    In such a case, it seems that adding ".Id" on the result of the dererencing of the reference field , but not on the result of the "select" - which already returns a nid, gives a record reference that can be used directly.

Content aside

  • 1 mth agoLast active
  • 2Replies
  • 46Views
  • 3 Following