0

For loop for accessing different fields

Hi there.

I'm developing a solution that has, in some tables, multiple options for same kind of data. For example: 'option 1', 'option 2', 'option 3' ... 'option 10', and their corresponding companions, like 'prince 1', 'price 2', 'price 3' ... 'price 10'. What I want/wish: to use a simple for loop for certain operations, like zeroing multiple fields. Something like this:

function initializeField(fieldName:text) do
    for i from 1 to 10 do
        let myField := fieldName +" "+i;
        this.myField := 0;
    end
end;
initializeField("price");

I know this part of the code returns a String:

let myField := fieldName +" "+i;

So I was wondering how can I evaluate it no a field name (or if this is possible).

Reading other posts, it seems Ninox does not support this feature (evaluating a string as a field name) but maybe I haven't used the proper search terms...

2 replies

null
    • Fred
    • 1 yr ago
    • Reported - view

    Maybe Jacques TUR has a trick up his sleeve that can find the Ninox 'real name' for each field name.

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Fred Yes, there are two undocumented functions in Ninox for manipulating fields:

      get(record, fieldName): returns the value of the fieldName passed for the record ;

      var rec := first(select Customer);
      var fieldName := "First Name";
      get(rec, fieldName);

      typeof(field) : returns the type of the field

      var rec := first(select Customer);
      typeof(rec.'First Name')

      Javier Gómez for your need, get seem be the best way to read field value, but not to write inside.

      To do this, there are three ways: 

      1 - use the Ninox API to read and write field values:

      get record : https://docs.ninox.com/en/api/public-cloud-apis#get-a-single-record

      update record : https://docs.ninox.com/en/api/public-cloud-apis#update-a-single-record-with-put

      This method is fully native to Ninox and allows you to do exactly what you want, but the number of API calls per month is limited and depends on your Ninox account type: 1500, 30000 or unlimited.

      2 - use NativeJS (see here)  : 

      This code is very easy and short, but NatvieJS is part of my Ninext project and for the moment I don't have an agreement with Ninox and if they decide to stop it or deeply modify the Ninox code preventing Ninxet to work, then its functions would not be available anymore. That's why I don't recommend it for commercial use.

      var rec := this;
      var fieldName := "User value";
      var value := rec.'User date';
      #{
      var field = database.schema.typeOf(rec.Id).findElement(fieldName);
      database.update(rec.id, field.id, value, (e)=>{e && alert(e)});
      }#

      ...
      But before using complex functions for reading and updating fields, it is always good to verify that there is no other simpler and more native way to do it.
      Why don't you use a sub table with the values?
      If you want it to be transparent to the user, you can create a field in memory (not stored in the principle table) that reads and updates the correct record in the subtable.

Content aside

  • 1 yr agoLast active
  • 2Replies
  • 138Views
  • 3 Following