Function parameters - which types are possible
I've found this old post
And not really any docs on the topic.
Any changes on this?
Being able to call a function with parameters record, records, arrays, JSON - without workarounds is requested.
Thx
Jesper
46 replies
-
The possible types for a function are :
- text
- number
- date
- datetime
- time
- timeinterval
- appointment
- boolean
- html
- color
- icon
- phone
- location
- file
- user
- any
with user type, you could call with single user or array of user :
function myFunction(myUser : user) do userName(myUser) end; myFunction(user())
or
function myFunction(myUsers : user) do join(for u in myUsers do userFullName(u) end, ", ") end; myFunction(users())
With the any type you can call with a JSON variable :
function myFunction(a : any) do capitalize(text(a.firsName)) + " " + upper(text(a.lastName)) end; myFunction({ firsName: "Jacques", lastName: "TUR" })
You can also call with array of JSON variable :
function myFunction(t : any) do "// recover the array from JSON array"; "// and return the array filtred and sorted"; var c := for i in t.table do i end; c[lastName like "T"] order by this.firstName end; "//call with array of JSON"; myFunction({ table: (select Customer).[{ firstName: 'First Name', lastName: 'Last Name' }] })
And, of course, call with table result :
function myFunction(t : any) do "// constructs an array of records identical to the one obtained with the Select function"; "// the ID number is gotten by keeping only the number part in order to call the Record function"; var c := for i in t.table do record(Customer,number(extractx(text(i), "\d.*"))) end; c.('First Name' + c.'Last Name') end; myFunction({ table: select Customer })
-
Wow, thanks Jacques! I think Ninox still has some work to do with their documentation.
-
Jacques TUR In the last example, do you know of a way to substitute the actual table name in the record() function? Otherwise, you might as well just use the select statement in the function definition. Thx
-
So this looks like a bust. It would be nice to be able to either pass the table name as an argument or somehow get "this" to work in a global context. I tried to use a number instead of using the first() function in place of "this", but no joy even though first(select table) = 1 returns true. Using a function like first() commits you to a table just like record().
While you can pass a table or record object to a user defined function, if you are going to use the record() function then you have already committed to using a specific table and there is no point in passing it as an argument. This code produces the same output.
function myFunction() do for rec in select Customer do rec.('First Name' + " " + 'Last Name') end; end; myFunction()
If you don't need to modify any of the data and want the flexibility of a user defined function to generate output it make sense to pass a table or record object as an argument, but you will need to include the fields you want to pass, e.g., table: (select Customer).('First Name' + " " + 'Last Name').
-
I did get eval() to work in a "Global function". It's not perfect, but it works. Even though I assign the result of the eval() in a variable, I can't use dot notation to access the fields. I have to include the dot notated field in the eval string. Thanks Jacques TUR for your help.
function myEvalFunction(table : text,rec : number) do let myEvalString := "record(" + table + "," + rec + ").Name"; let myEval := eval(myEvalString, first(select Truck)); myEvalString + " " + myEval end
Note, the first() function uses a different table than the passed table argument.
-
One more note, I can't use the result of the eval() to assign a new value to the field, e.g., record(Vendor, 4).Name := newName. This is possible otherwise.
-
I thought I had it figured out... I didn't eval the entire string, but then I got this disappointing message after I updated my code which pretty much drops a stinky dump on my project...
function evalFunction(table : text,rec : number,name : text) do let getName := "record(" + table + "," + rec + ").Name"; let curName := eval(getName, first(select Truck)); let setName := "record(" + table + "," + rec + ").Name := """ + name + """"; let newName := eval(setName, first(select Truck)); curName + " " + newName end
-
Sean said:
Maybe something is getting crossed in translation, but yes, you can pass a table object or a record object as an argument to a user defined functionNo, it's not a NID (table) or RID (record) variable but just an Any variable. This is very different. Similarly, the Eval function returns a variable of type Any, not of type NID or RID. That's why you can't use the result as a normal Select.
The Select function returns an NID type (try: debugValueInfo(select Customer) in console )
The Record function returns an RID type (try: debugValueInfo(record(Customer,1)) in console ).
And now try this in the console:var a :={table : select Customer,
rec : record(Customer,1)};
debugValueInfo(a.table);
When calling a function with a JSON parameter, Ninox transforms the NID into Any.
Content aside
-
3
Likes
- 1 yr agoLast active
- 46Replies
- 1723Views
-
10
Following