3

first(select Table...) vs create Table data types

A question earlier this week got me thinking about this structure

let a := first(select table where field = value)
if a != null then
   a.field1 := value1;
   a.field2 := value2
else
   let b := create table;
   b.field1 := value1;
   b.field2 := value2
endif

 

Which repeats a fair part of the code. I'd like it to be more like this

let a := first(select table where field = value)
if a = null then
  a := create table
endif;
a.field1 := value1;
a.field2 := value2

 

But this doesn't work because, oddly, lines 1 and 3 don't return the data data type. After playing in the console for a while I found that this does work

let a := first(select table where field = value)
if a = null then
  a := (create table).Id
endif;
a.field1 := value1;
a.field2 := value2

 

With the addition of .Id I now have a single variable whether the initial record existed or a new one needs to be created.

Regards John

Reply

null