Help with a script with two conditions

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Help with a script with two conditions

Viewing 14 reply threads
  • Author
    Posts
  • November 21, 2023 at 7:55 AM #50150

    Fernando DS
    Participant

    Hi. I want to do an script for autocomplete a note field called “Comentario” with “Buen disco.”, when the valoration field “Valoración” has four stars and, at once, the field “Comentario” is empty. I have not errors in the console, but the script does not work. Any help will be welcome. Thank you.

     

    Attachments:
    You must be logged in to view attached files.
    November 21, 2023 at 10:53 AM #50152

    Brendan
    Keymaster

    Hi Fernando,

    try == instead of ===. But it could also be that you just need to check for 4 instead of "4" because the Rating field returns a number, not a string and you’re checking to see if the value is the string "4" and not the number 4.

    November 21, 2023 at 11:25 AM #50153

    Fernando DS
    Participant

    Hi Brendan, thank you for your answer. The script is steel not working.

    Attachments:
    You must be logged in to view attached files.
    November 21, 2023 at 3:48 PM #50155

    Daniel Leu
    Participant

    There’s a second “===” instead of a “==”.

    November 21, 2023 at 9:35 PM #50156

    Fernando DS
    Participant

    It’s true. I have corrected it, but the result is the same.

    Attachments:
    You must be logged in to view attached files.
    November 22, 2023 at 12:07 AM #50158

    Daniel Leu
    Participant

    It would be easier if you posted your code here instead of the screen capture. But I think that the form.saveAllChanges() should be before the closing bracket above.

    November 22, 2023 at 12:38 AM #50159

    Fernando DS
    Participant
    Sorry for my ignorance, but how do I send the code?
    November 22, 2023 at 5:13 AM #50160

    Fernando DS
    Participant

    Well, I’m going to paste it here.

     

    function modifyFieldsBasedOnCondition() {
    var fieldId1 = 'fld-28b97ce8ab4f4f57a83aefa7e91f17fe';
    var fieldId2 = 'fld-c2fd26ae62c1445692b3a0abc1e89158';
    
    for (var record of form.getRecords()) {
    if (record.getFieldValue(fieldId1) == 4 &&
    record.getFieldValue(fieldId2) == "") {
    record.setFieldValue(fieldId2, "Buen disco.");
    }
    }
    
    form.saveAllChanges();
    }
    
    modifyFieldsBasedOnCondition();
    • This reply was modified 5 months, 1 week ago by Brendan.
    November 22, 2023 at 6:30 AM #50161

    Fernando DS
    Participant

    Hope this is what you said Daniel.

    November 22, 2023 at 10:19 AM #50163

    Brendan
    Keymaster

    Hi Fernando,

    What type of field is fieldId2? Is it a Text field or a Number field? Because you’re comparing it to the empty string "".

    You might want to check to see if it’s undefined

    November 22, 2023 at 10:58 AM #50165

    Fernando DS
    Participant

    It’s a note field. I write my opinions about discs in it and I need enough extension.

    November 22, 2023 at 11:33 AM #50166

    Fernando DS
    Participant

    So it’s a field of text, not numbers.

    November 22, 2023 at 2:22 PM #50167

    Fernando DS
    Participant

    Just another try, without errors in console, but not works.

    function modifyFieldsBasedOnCondition() {
    var fieldId1 = 'fld-28b97ce8ab4f4f57a83aefa7e91f17fe';
    var fieldId2 = 'fld-c2fd26ae62c1445692b3a0abc1e89158';
    
    for (var record of form.getRecords()) {
    if (record.getFieldValue(fieldId1) == 4 &&
    record.getFieldValue(fieldId2) <= (0))
    record.setFieldValue(fieldId2, "Buen disco.");
    }
    }
    
    form.saveAllChanges();
    
    modifyFieldsBasedOnCondition();
    • This reply was modified 5 months, 1 week ago by Brendan.
    November 22, 2023 at 5:11 PM #50169

    Daniel Leu
    Participant

    This works for me:

     function modifyFieldsBasedOnCondition() {
    
    var fieldId1 = 'fld-28b97ce8ab4f4f57a83aefa7e91f17fe';
    var fieldId2 = 'fld-c2fd26ae62c1445692b3a0abc1e89158';
       for (var record of form.getRecords()) {
          console.log(record.getFieldValue(fieldId1));
          console.log(record.getFieldValue(fieldId2));
          if (record.getFieldValue(fieldId1) == 4 &&
             record.getFieldValue(fieldId2) == undefined)
          {
             record.setFieldValue(fieldId2, "Buen disco.");
             console.log("empty record found");
          }
       }
       form.saveAllChanges();
    }
    modifyFieldsBasedOnCondition();
    • This reply was modified 5 months, 1 week ago by Daniel Leu.
    • This reply was modified 5 months, 1 week ago by Daniel Leu.
    • This reply was modified 5 months, 1 week ago by Daniel Leu.
    • This reply was modified 5 months, 1 week ago by Daniel Leu.
    • This reply was modified 5 months, 1 week ago by Daniel Leu.
    November 22, 2023 at 11:29 PM #50176

    Fernando DS
    Participant

    At last this is the script that works:

    function modifyFieldsBasedOnCondition() {
    var fieldId1 = ‘fld-28b97ce8ab4f4f57a83aefa7e91f17fe’;
    var fieldId2 = ‘fld-c2fd26ae62c1445692b3a0abc1e89158’;

    for (var record of form.getRecords()) {
    var valueField1 = record.getFieldValue(fieldId1);
    var valueField2 = record.getFieldValue(fieldId2);

    if (valueField1 === 4 && (valueField2 === null || valueField2 === “”)) {
    record.setFieldValue(fieldId2, “Buen disco.”);
    console.log(“Campo fieldid2 actualizado en el registro con campo fieldid1 igual a 4 y fieldid2 vacío.”);
    }
    }

    form.saveAllChanges();
    }

    modifyFieldsBasedOnCondition();

     

    Thank you Brendan and Daniel for your valuable help.

    Bye.

     

Viewing 14 reply threads

You must be logged in to reply to this topic.