0

Passing a field name (string) as a function parameter

I'm searching the forum but I can't find an answer to this question.

I've defined a function like this very simple example:

function updateField(iField:text, iValue:text,  iRedord: Products) do
     iRecord.iField:=iValue;
end;

let myRecord:= first(select Products where id="123A");
updateField("description","my new description", myRecord) 

As you can see, I'm trying to pass a field name as a parameter, and the use the field name to update a field from the record (that I can also pass as a parameter). It seems Ninox accepts the iRecord as a real record (because I can force a field update if I use a proper "field notation", but the field name does not work as intended.

I'm expecting that the record with "id" = "123A" updates the content of the field "description", but for Ninox, the following code:

iRecord.iField

returns "description" instead of the "real" description field. So, it's clear that Ninox does not recognize the string iField as a field name.

Any references to the function parameters or if this is possible?

3 replies

null
    • Ninox partner
    • RoSoft_Steven.1
    • 21 hrs ago
    • Reported - view

    Try the eval() function:

    function updateField(iField:text, iValue:text,  iRecord: number) do
        let me := record(Products,iRecord);
        let code := "record(Products,"+iRecord+")."+iField + ":="+iValue;
         eval(code,me)
    end;
    • Fred
    • 20 hrs ago
    • Reported - view

    Have you tried the set() command? It is made for using text reference of fieldnames.

    function updateField(iField:text, iValue:text,  iRecord: number) do
        let me := record(Products,iRecord);
        set(me, iField, iValue)
    end;

    There is also the companion get() command as well.

    • Consultant and developer
    • Javier
    • 20 hrs ago
    • Reported - view

    Awesome! get() and set() functions are what I was looking for to implement my functions!!! Thank your very much, 

Content aside

  • Status Answered
  • 20 hrs agoLast active
  • 3Replies
  • 19Views
  • 3 Following