Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 1,036 through 1,050 (of 2,951 total)
  • Author
    Search Results
  • #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 3 years, 10 months ago by Brendan.
    #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 3 years, 10 months ago by Brendan.
    #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();

    #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.

    #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();

    #46175
    Sam Moffatt
    Participant

    The other thing that makes this increasingly difficult is that out of the box MacOS is removing support for the scripting languages that it used to ship by default so getting back PHP on the Mac is increasingly hard.

    #46170
    Brendan
    Keymaster

    I haven’t written any write to disk API for the scripting engine, but I should definitely do that.

    #46164
    Victor Warner
    Participant

    I would like to create a CSV file through a form script, as it will be much quicker/more automated than going through the File/Export/Export option.

    I have been able to create a script which uses Utils.copyTextToClipboard() to get the data out (and then using the clipboard to insert it into an AppleScript). It appears to work, but was wondering whether it is also possible to write the data to a file rather than the clipboard?

    The script as far as I have got with it:

    var client_name = record.getFieldValue('fld-ffa7651ff5bd48a9aa8184a69be8550e');
    var email = record.getFieldValue('fld-152d99920304499f8f89c3868eb144d1');
    var details = record.getFieldValue('fld-23c8168c1deb45bfbcabc862ad6e5199');
    var date_prescribed_information_provided = record.getFieldValue('fld-0bf58a5d40b3430f8745b86b2a49775a');
    var document_details = record.getFieldValue('fld-b8a4b0731dc94f46abf28eab828abc4e');
    var number_of_documents = record.getFieldValue('fld-7ba7df82977945988aac2598dc528a75');
    var amount_of_notary_charge = record.getFieldValue('fld-648fee2d0eaa4e7d973fe86bde1d504f');
    var what_will_third_parties_be_providing = record.getFieldValue('fld-abfb6ffd2c7c4695972ed54ec463fc08');
    var third_party_charge_plus_charge_to_arrange = record.getFieldValue('fld-8cc4031614d7402aa5e131d33ae1c039');
    var final_charge = record.getFieldValue('fld-6b86691517dc4b8b8efa63b7da98185d');
    var second_individual_client_name = record.getFieldValue('fld-772ee93b5eb747d294e5a9a7b548ebda');
    var second_individual_email = record.getFieldValue('fld-d09998df75864a0e8f82b6f61188b537');
    
    // set email for prescribed information document
    
    if (second_individual_client_name){
    var email_for_prescribed_information = email + " and " + second_individual_email;
    } else {
    var email_for_prescribed_information = email;
    }
    
    var date_formatted = date_prescribed_information_provided.toLocaleDateString("en-GB", { year: 'numeric', month: 'long', day: 'numeric' });
    
    var lines = client_name + ";" + email_for_prescribed_information + ";" + details  + ";" + date_formatted + ";" + document_details + ";" + number_of_documents + ";" + amount_of_notary_charge + ";" + what_will_third_parties_be_providing + ";" + third_party_charge_plus_charge_to_arrange + ";" + final_charge + ";" + second_individual_client_name + ";" + second_individual_email;
    
    //	Utils.copyTextToClipboard(JSON.stringify(record.first_name_id));
    	
    Utils.copyTextToClipboard(lines);
    #46162
    Daniel Leu
    Participant

    No, the variable form revers to the current form where you call the script from. If you want to add a record to a different form, then you first have to do a

    var myForm = document.getFormNamed("My Form Name");

    And then you would use myForm instead of form.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #46159
    Brendan
    Keymaster

    The Apple Watch version of Tap Forms is read-only. So that’s why you can’t add a record from the watch. The scripting idea is good. Something like this:

    function addNewRecord() {
       var new_record = form.addNewRecord();
       // do something to the new record if you want, such as calling new_record.setFieldValue(field_id, "some value");
       form.saveAllChanges();
    }
    
    addNewRecord();
    #46156
    Sam Moffatt
    Participant

    I think for this one, if the defaults make sense the form script can be as simple as “form.addNewRecord();” and then you need to enable your script for Siri (there should be a button in the editor on your phone). I think you might also need to enable the document for Siri (it’s been a while) and then you should be able to use Siri to create a new record automatically.

    #46155
    Karan Shah
    Participant

    Thank you. Any guide to scripting? I’m not a coder, but if it’s simple I’m happy to look at setting up a script for this.

    #46154
    Sam Moffatt
    Participant

    I think what might work for this is to enable Siri for your document and then create a script that creates a new record. Then you can use either a Shortcut or Siri directly to create a new record with the current date and time.

    #46150

    In reply to: Help with if statement

    Sam Moffatt
    Participant

    To expand a little, second_individual_client_name.length===0 is failing because if the field isn’t set to a string value, it’s an undefined value or null. The .length only works if second_individual_client_name is set to a string but if the field isn’t entered, Tap Forms returns a null/undefined value to signify the difference between a field with an empty string and a completely unset field.

    Javascript has the concept of “falsy” values that are values that evaluate to “false” when used in an if test situation. In this case not only is null and undefined a “falsy” value, but an empty string is also a falsy value. This means that for your purposes, that is to say to check if the field has a value, this simple if test does what you need without validating the length.

    [1] falsy: https://developer.mozilla.org/en-US/docs/Glossary/Falsy

    #46148
    Victor Warner
    Participant

    I wish to create a script to deal with the following where the document contains two email fields:

    1. if only one email field (“email”) has an email address the script assigns to a variable just that email fields contact; but
    2. 2. if both email fields (“email” and “econd_individual_email”) have email addresses then the script assigns to a variable the following: “email_field_1 = ” and ” + email_field_2″
    using an if statement testing whether another field (second_individual_client_name) contains anything.

    It works fine when the other field (“second_individual_client_name”) contains data but generates an error when it does not.

    The script is as follows:

    var client_name = record.getFieldValue('fld-ffa7651ff5bd48a9aa8184a69be8550e');
    var email = record.getFieldValue('fld-152d99920304499f8f89c3868eb144d1');
    var second_individual_client_name = record.getFieldValue('fld-772ee93b5eb747d294e5a9a7b548ebda');
    var second_individual_email = record.getFieldValue('fld-d09998df75864a0e8f82b6f61188b537');
    
    // set email for prescribed information document
    
    if (second_individual_client_name.length===0){
    var email_for_prescribed_information = email;
    } else {
    var email_for_prescribed_information = email + " and " + second_individual_email;
    }
    
    console.log(client_name + "\n" + email_for_prescribed_information);
    

    The error message generated is:

    New Script: TypeError: undefined is not an object (evaluating 'second_individual_client_name.length'), line:(null)

    Any help would be gratefully received.

Viewing 15 results - 1,036 through 1,050 (of 2,951 total)