Script for find and modify records

Viewing 36 reply threads
  • Author
    Posts
  • October 1, 2021 at 11:13 AM #45360

    Fernando DS
    Participant

    Hi, I would like to have and script to find and modify my records, but i don’t know nothing about programming and it’s very dificult to me to do the script. I use and iPhone and iPad, I have not got a Mac. Could anybody help me? Thank you very much.

    Attachments:
    You must be logged in to view attached files.
    October 1, 2021 at 11:51 AM #45362

    Sam Moffatt
    Participant

    Could you can expand on what you want to find and modify with a use case?

    October 1, 2021 at 11:58 AM #45363

    Fernando DS
    Participant

    Yes. For example, i want to change every record with “rock duro” in Estilo, for “heavy metal”.

    October 3, 2021 at 11:08 PM #45366

    Sam Moffatt
    Participant

    For something like that, a new form script like the following should do it:

    function Replace_Metal() {
      var fieldId = 'fld-fieldid';
      for (var record of form.getRecords()) {
        var fieldValue = record.getFieldValue(fieldId);
        if (fieldValue.includes("rock duro")) {
          record.setFieldValue(fieldId, fieldValue.replace("rock duro", "heavy metal"));
        }
      }
      form.saveAllChanges();
    }
    
    Replace_Metal();

    Replace fld-fieldid with a field ID from either the script editor or find it in the field editor underneath the description field.

    October 4, 2021 at 4:27 AM #45368

    Fernando DS
    Participant

    I’m afraid it doesn’t work. This is what appear in activity console.

    Attachments:
    You must be logged in to view attached files.
    October 4, 2021 at 4:29 AM #45370

    Fernando DS
    Participant

    This is how the script is redacted:

    function Replace_Metal() {
    var fieldId = ‘fld-fieldid’;
    for (var record of form.getRecords()) {
    var fieldValue = record.getFieldValue(fieldId);
    if (fieldValue.includes(“rock duro”)) {
    record.setFieldValue(fieldId, fieldValue.replace(“rock duro”,
    “heavy metal”));
    }
    }
    form.saveAllChanges();
    }

    Replace_Metal();

    Replace fld-fieldid with a field ID from either the script editor or
    find it in the field editor underneath the description field.

    October 6, 2021 at 10:27 AM #45385

    Brendan
    Keymaster

    Can you upload a screenshot of your script as you have it?

    Your fieldId is wrong. Also, make sure you’re not using curly quotes for any of the values. They must be straight quotes. The example that Sam posted uses straight quotes, but your code you posted is using curly quotes.

    October 6, 2021 at 10:42 AM #45387

    Fernando DS
    Participant

    Hi Brendan,

    The script is as follows:

    function Buscar_Y_Modificar() {

    function Replace_Metal() {
    var fieldId = ‘fld-fieldid’;
    for (var record of form.getRecords()) {
    var fieldValue = record.getFieldValue(fieldId);
    if (fieldValue.includes(“rock duro”)) {
    record.setFieldValue(fieldId, fieldValue.replace(“rock duro”,
    “heavy metal”));
    }
    }
    form.saveAllChanges();
    }

    Replace_Metal();

    Replace fld-fieldid with a field ID from either the script editor or
    find it in the field editor underneath the description field.

    Sorry, my english is very poor. I don’t understand exactly what you mean with curly and straight quotes.

    October 6, 2021 at 10:50 AM #45388

    Fernando DS
    Participant

    And why is wrong my fileid?

    October 6, 2021 at 10:53 AM #45389

    Fernando DS
    Participant

    Sorry, i wanted to say fieldid

    October 6, 2021 at 12:47 PM #45390

    Brendan
    Keymaster

    var fieldId = ‘fld-fieldid’;

    Because first of all, you’re using curly quotes. They need to be straight quotes.

    Plus Tap Forms generates random field IDs. fld-fieldid will never be the field ID that Tap Forms generates. That was just a sample value Sam put in there. You need to change it to be your actual Field ID which you can get from the Script Editor or just beneath the Fields list on the field property panel.

    October 6, 2021 at 12:48 PM #45391

    Brendan
    Keymaster

    Curly quotes are like this: where a straight quote is like this '.

    Can you see the difference?

    October 6, 2021 at 3:09 PM #45393

    Fernando DS
    Participant

    Ok. I understand the curly and straight quotes. But I can’t find the field id. Neither the script editor, nor beneath the field lost on the property panel. Sorry, I must be very incompetent. I’m using an iphone, I have not got a Mac. Perhaps the problem is this? Thank you very much for your time and patience.

    October 6, 2021 at 5:05 PM #45395

    Daniel Leu
    Participant

    The easiest way to get the field ID is to double click on the field name in the script editor. Or you can click on the field name and the select the “ID” field.

    October 6, 2021 at 5:11 PM #45396

    Daniel Leu
    Participant

    Have a look at Sam’s videos such like this one: https://www.youtube.com/watch?v=xB1XZyBYNd4&ab_channel=pasamio. You will see the field id about 4 minutes into the video. He has many more on TapForms and scripting on his channel. It is worthwhile to check them out.

    T.L. Ford wrote a scripting 101 as well: http://www.cattail.nu/tap_forms/tap_forms_scripting_101/index_tap_forms_interface.html

    October 6, 2021 at 9:09 PM #45397

    Sam Moffatt
    Participant

    On iOS/iPadOS there is a button in the action bar that will open up a pop over with a list of fields, see the screenshot highlighting the button. Set it to “field ID” on the top of the pop over and it’ll generate a variable place holder for you.

    I should do a similar video for iPad and iPhone as well.

    Attachments:
    You must be logged in to view attached files.
    October 6, 2021 at 11:02 PM #45399

    Fernando DS
    Participant

    Ok. Thank you everybody for your kind answers. This is what I have done in the script and the error that appears in the console.

    Attachments:
    You must be logged in to view attached files.
    October 6, 2021 at 11:49 PM #45401

    Sam Moffatt
    Participant

    The var fieldId line should look like this:

    var fieldId = ‘fld-abc029a2589545909e4018d9f5870059‘;
    

    You might need to fix the quotes not to use smart quotes. At the moment there is too much on the line, it just needs the field ID.

    October 7, 2021 at 2:14 AM #45404

    Fernando DS
    Participant

    I have fixed the quotes and have changed some more. Please send me your coments.

    Attachments:
    You must be logged in to view attached files.
    October 7, 2021 at 8:08 AM #45407

    Daniel Leu
    Participant
    October 7, 2021 at 8:26 AM #45408

    Fernando DS
    Participant

    The problem is that I don’t understand what he say. I have fix the quotes, but I dont understand the rest of the message. What means “there is too much on the line”. Must I to remove something?. And “just need the field id”. But the field id is not fld-abc029a2589545909e4018d9f5870059?.

    Sorry, I need more assistance. As I said, I have no idea of programming, and I am a little old to learn it now. Thank you for your interest.

    October 7, 2021 at 9:47 AM #45409

    Daniel Leu
    Participant

    He says the code should be
    var fieldId = 'fld-abc029a2589545909e4018d9f5870059';
    but your line 4 is
    var fieldId = 'var estilo_id = 'fld-abc029a2589545909e4018d9f5870059';

    October 7, 2021 at 11:43 AM #45410

    Fernando DS
    Participant

    Hi Daniel, thank you for your support. I have done it and I sent the script as it is now. But there is an error, that I also sent.

    Attachments:
    You must be logged in to view attached files.
    October 7, 2021 at 11:47 AM #45412

    Fernando DS
    Participant

    The script photo is going on now.

    Attachments:
    You must be logged in to view attached files.
    October 7, 2021 at 1:01 PM #45415

    Sam Moffatt
    Participant

    You probably just need to delete line one entirely (the function Buscar_Y_Modificar one) . That looks like the old placeholder which you don’t need since you’ve got a function (Replace_Metal). Assuming that’s the full script then nothing else jumps out at me as wrong.

    If that works, you could also change Replace_Metal with Buscar_Y_Modificar on the function line and also at the bottom of the script (e.g. change Replace_Metal(); with Buscar_Y_Modificar();).

    October 7, 2021 at 1:27 PM #45416

    Fernando DS
    Participant

    It works!!! Thank you very much Sam, Daniel and Brendan for your kind support. Greetings.

    October 7, 2021 at 7:34 PM #45420

    Sam Moffatt
    Participant

    Good to hear it works! I remembered I did a video on shortcuts integration which features code development on an iPad but the interface is the same as on iPhone, just a little more squished.

    October 8, 2021 at 9:13 AM #45421

    Fernando DS
    Participant

    I will look for that video. In the meantime i have been doing some tryings with the script. I want search in any other field. I though it be easy. But not at all. I have changed the fileid, and the words to be changed. The rest of the script exact the same. But I have errors. How is possible?.

    October 8, 2021 at 11:20 AM #45424

    Fernando DS
    Participant

    I send both scripts. They are the same, just change the fieldid and the words to be changed. Where is the mistake?

    Attachments:
    You must be logged in to view attached files.
    October 8, 2021 at 11:28 AM #45426

    Fernando DS
    Participant

    The second script works. The first don’t. But why?.

    October 8, 2021 at 3:09 PM #45427

    Daniel Leu
    Participant

    If you delete the second function (script), does the first one work?

    October 8, 2021 at 3:14 PM #45428

    Fernando DS
    Participant

    No, these are two scripts. I have put it in the same page to compare. The second one works, it’s the one done last days. The first one is a copy of the second, changing the fieldid and the words to be changed. The first does not work.

    October 8, 2021 at 4:26 PM #45429

    Daniel Leu
    Participant

    Hmmm… do you get any errors in the console? With doesn't work I assume you mean it doesn’t perform the search and replace operation.

    October 8, 2021 at 4:36 PM #45430

    Fernando DS
    Participant

    I sent the script and the error. The script does nothing in the database.

    Attachments:
    You must be logged in to view attached files.
    October 8, 2021 at 4:39 PM #45432

    Fernando DS
    Participant

    This is the error.

    Attachments:
    You must be logged in to view attached files.
    October 8, 2021 at 4:43 PM #45434

    Sam Moffatt
    Participant

    You have a record that doesn’t have a value for that field, try changing the line that starts with this:

    if (fieldValue.includes
    

    To add be start with this:

    if (fieldValue && fieldValue.includes
    

    That should validate we got something in fieldValue and that it includes your target string.

    October 8, 2021 at 4:53 PM #45436

    Fernando DS
    Participant

    Perfect. Sam, you’re a genius. Thank you very much again to everybody.

Viewing 36 reply threads

You must be logged in to reply to this topic.