Help

Re: Script has no errors but nothing happens

4763 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Edward1976
6 - Interface Innovator
6 - Interface Innovator

I have written the following script which attempts to check if a field has the value "Definition" and if so make selection on specific single selection fields on a random basis. The reason for this is that the values will be used in a series of image mockups.

I am completely new to both airtable and scripting so have absolutely no idea why this script doesn't do anything, but also doesn't throw any errors. I also don't understand (despite being on the pro trial) why I can't seem to add scripting as an extension - it just says 0/10 added. However, when I go to the automation tab in the table, I can see the script I added (along with another one that works). 

base.getTable("Pre Mock Prod (YP-PP)");
let query = await table.selectRecordsAsync();
let records = query.records;
for (let i = 0; i < records.length; i++) {
    let currentRecord = records[i];
    let posterType = currentRecord.getCellValue("Poster Type");
    if (posterType.name === "Definition") {
      let options = ["_p_design", "_p_layout", "_p_color_style", "_headline_border", "_p_orientation"];
      let randomIndex = Math.floor(Math.random() * options.length);
      let randomOption = options[randomIndex];
      let selectedOption = currentRecord.getCellValue(randomOption);
      let recordId = currentRecord.id;
      await table.updateRecordAsync(recordId, { [randomOption]: selectedOption });
    }
}

I am completely and utterly stuck...help!!

 

 

 

 

5 Replies 5

Can you share screen shots of your data, including column headings? You have a lot of different fields, and the field type are important.

My initial guess is that either the {Poster Type} isn't a single select, or there is a typo in the "Definition" choice. You can try changing .getCellValue to .getCellValueAsString. However, even if this fixes this issue, I suspect that the script has additional issues.

I recommend learning how to use console.log() to see the values of your variables. For example,
console.log(posterType)

Thanks so much for the response Kuovonne, see attached column headers. I just don't get it, there are no errors being thrown and the tests all run successfully but the execution log is blank.

Screenshot 2023-01-18 at 15.42.15.png

Edward1976
6 - Interface Innovator
6 - Interface Innovator

OK so I have made a little more progress but still haven't worked it out...I have modified the code to this:

let table = base.getTable("Pre Mock Prod (YP-PP)");
let query = await table.selectRecordsAsync();
let records = query.records;

for (let i = 0; i < records.length; i++) {
    let currentRecord = records[i];
    let posterType = currentRecord.getCellValue("Poster Type");
    if (posterType.name === "Definition") {
      let options = ["_p_design", "_p_layout", "_p_color_style", "_headline_border", "_p_orientation"];
      let randomIndex = Math.floor(Math.random() * options.length);
      let randomOption = options[randomIndex];
      let recordId = currentRecord.id;
      let optionValues = {
        _p_design: ["definition_1", "definition_2", "definition_3"],
        _p_layout: ["layout-1", "layout-2", "layout-3"],
        _p_color_style: ["style-1", "style-7", "style-45", "style-54", "style-8", "style-11", "style-2", "style-3", "style-4", "style-5", "style-6", "style-64", "style-65", "style-66", "style-67", "style-68", "style-69", "style-70", "style-71", "style-72", "style-73", "style-74", "style-57", "style-69", "style-52"],
        _headline_border: ["1","0"],
        _p_orientation: ["portrait", "landscape"]
      };
      let randomOptionValue = optionValues[randomOption][Math.floor(Math.random() * optionValues[randomOption].length)];
      console.log(randomOptionValue);
            await table.updateRecordAsync(recordId, { [randomOption]: randomOptionValue });
    }
}

I first checked that all the values were properly set and that there were no typos. But it seems from the console.log output that the script only returns 1 random value, regardless of whether it belongs to the field or not. It therefore not accepted as a value by the field. 

What should happen is that only 1 random value per field should be inputed. HOw would I modify the script to do this? If this is indeed the problem?

 

Are you using Scripting Extension or an automation script? You should be able to have unlimited Scripting Extensions in the fly-out dashboard to the right of the data. All plans allow for unlimited instances of Scripting Extension. With a Pro-Trial, your ability to have an automation script is limited.

 

Since you say that you are new to scripting and Airtable, I think that you are trying to do too many things at once. It sounds like you want to populate multiple fields with random values. It also looks like some of your fields are text fields and other fields are single select fields. 

I recommend taking a step back. First write a script that can update each of fields with a hardcoded value. Different field types have different "write" formats. Then update the script so that it updates all of the fields together. Then, after you are able to update all the field types all at once, you can layer on setting random values.

If you haven't seen it yet, the scripting reference is very useful.

Thanks again for getting back..."It also looks like some of your fields are text fields and other fields are single select fields." ....I checked this and they are all singles select and all should accept the values I have passed. I think cracking this will help a lot particularly as I've been trying to do so for 9 hours now!