Change field value based on other fields

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Change field value based on other fields

Viewing 7 reply threads
  • Author
    Posts
  • December 26, 2021 at 4:08 AM #46187

    Guillermo q
    Participant

    Hello, Merry christmas to everyone,
    I tried to write an script to change the field value based on other fields. I created an script in the form but I cant find which is the problem. Can you help me?

    function Script_Precio_Y_Grupo(){

    var company = record.getFieldValue(‘fld-e1c28abdd3b8451fa38b875818503bc6’);
    var grupo = record.getFieldValue(‘fld-6c980123a15646a085389ac4426e07de’);
    var precio

    if (company==”TEST”) {

    //Grupo 1

    if (grupo=1) {
    precio=62
    }
    //Grupo 2

    if (grupo=2) {
    precio=90
    }
    //Grupo 3

    if (grupo=3) {O
    precio=125
    }

    //Grupo 4

    if (grupo=4) {
    precio=157
    }

    //Grupo 5

    if (grupo=5) {
    precio=250
    }

    }

    record.setFieldValue(‘fld-5b32e56f61f34bfb8e9c522f9eb9414a’, precio);

    form.saveAllChanges();

    }

    Script_Precio_Y_Grupo();

    December 26, 2021 at 9:57 AM #46188

    Daniel Leu
    Participant

    As you did in the first condition check, the comparison operator is == and not =. So the code should be

    if (grupo==1) {
       precio=62
    }

    Side note, I think you should initialize your precio variable: eg, var precio = 0.

    Merry Christmas!

    December 26, 2021 at 11:04 AM #46189

    Guillermo q
    Participant

    Thanks for your answer.

    I get an error: reference error cant find variable: record, line (null)

    Any suggestions?

    December 26, 2021 at 11:56 AM #46190

    Sam Moffatt
    Participant

    Make sure you have a record selected before running the form script. On iOS this means you have to navigate into the record before running the script (you can’t run form scripts from the record list view, only record detail). On the other platforms, simply making sure a record is selected and displayed should be sufficient. It’s a little harder on the Mac to not have a record select but still possible.

    December 26, 2021 at 1:06 PM #46191

    Guillermo q
    Participant

    Thank you very much!. Im not getting the error now, but I cant get it to work, I modify the var group and price is not modified. Any suggestions?

    December 26, 2021 at 1:16 PM #46192

    Guillermo q
    Participant

    Ok I did it. Thank you very much.

    function Script_Precio() {

    var grupo = record.getFieldValue(‘fld-6c980123a15646a085389ac4426e07de’);

    var precio

    var company = record.getFieldValue(‘fld-e1c28abdd3b8451fa38b875818503bc6’);

    if (company==”TEST”) {

    //Grupo 1

    if (grupo==1) {
    precio=62
    }

    //Grupo 2

    if (grupo==2) {
    precio=90
    }

    //Grupo 3

    if (grupo==3) {
    precio=125
    }

    //Grupo 4

    if (grupo==4) {
    precio=157
    }

    //Grupo 5

    if (grupo==5) {
    precio=250
    }

    }

    record.setFieldValue(‘fld-5b32e56f61f34bfb8e9c522f9eb9414a’, precio);

    document.saveAllChanges();

    }

    Script_Precio();

    December 27, 2021 at 2:45 PM #46195

    Brendan
    Keymaster

    Hi Guillermo,

    FYI, if you surround your code with the back tick character, then your code will be formatted more nicely and will be easier to read, like in Daniel’s example.

    function Script_Precio() {
    
       var grupo = record.getFieldValue(‘fld-6c980123a15646a085389ac4426e07de’);
    
       var precio
    
       var company = record.getFieldValue(‘fld-e1c28abdd3b8451fa38b875818503bc6’);
    
       if (company==”TEST”) {
    
          // Grupo 1
    
          if (grupo==1) {
             precio=62
          }
    
          //Grupo 2
    
          if (grupo==2) {
             precio=90
          }
    
          // Grupo 3
    
          if (grupo==3) {
             precio=125
          }
    
          // Grupo 4
    
          if (grupo==4) {
             precio=157
          }
    
          // Grupo 5
    
          if (grupo==5) {
             precio=250
          }
    
       }
    
       record.setFieldValue(‘fld-5b32e56f61f34bfb8e9c522f9eb9414a’, precio);
    
       document.saveAllChanges();
    
    }
    
    Script_Precio();

    Also, a Switch statement might be a little nicer:

    function Script_Precio() {
    
       var grupo = record.getFieldValue('fld-6c980123a15646a085389ac4426e07de');
    
       var precio;
    
       var company = record.getFieldValue('fld-e1c28abdd3b8451fa38b875818503bc6');
    
       if (company == "TEST") {
    
          switch(grupo) {
          case 1:
             precio = 62;
             break;
          case 2:
             precio = 90;
             break;
          case 3:
             precio = 125;
             break;
          case 4:
             precio = 157;
             break;
          default:
             precio = 250;
             break;
       }
    
       record.setFieldValue('fld-5b32e56f61f34bfb8e9c522f9eb9414a', precio);
    
       document.saveAllChanges();
    
    }
    
    Script_Precio();
    • This reply was modified 2 years, 3 months ago by Brendan.
    December 27, 2021 at 4:19 PM #46197

    Guillermo q
    Participant

    Thank you very much for your guidance. That switch solution is lot more elegant. In sorry i have not idea of programming.

    Thanks for your time

    December 27, 2021 at 10:16 PM #46198

    Brendan
    Keymaster

    Another idea since it’s a simple lookup table would be to use an array and just use the grupo value to index into the array.

    function Script_Precio() {
       var precio_lookup = [62, 90, 125, 157, 250];
       // array indexes start at 0, so subtract 1 from grupo 
       var grupo = record.getFieldValue('fld-6c980123a15646a085389ac4426e07de') - 1;
       var company = record.getFieldValue('fld-e1c28abdd3b8451fa38b875818503bc6');
    
       if (company == "TEST") {
          if (grupo < precio_lookup.length) {
             var precio = precio_lookup[grupo];
             record.setFieldValue('fld-5b32e56f61f34bfb8e9c522f9eb9414a', precio);
             document.saveAllChanges();
          }
       }
       return;
    }
    
    Script_Precio();

    But I don’t know exactly what your data looks like. Maybe this would work for you, maybe the switch statement is better. Not sure.

    • This reply was modified 2 years, 3 months ago by Brendan.
Viewing 7 reply threads

You must be logged in to reply to this topic.