Help

Similar code, different setting...throwing an error

Topic Labels: Scripting extentions
1762 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Chris_Buehnerke
6 - Interface Innovator
6 - Interface Innovator

I have a script block that prompts the user for some information then creates a bunch or records that link records in two other tables.

I wanted to utilize the automation feature to further automate this process. Anytime a record in the “meeting” table is created, the automation script will trigger and create a record in an “attendance” table for each staff member listed in the “staff” table linked to the “meeting” record.

I’ve had this script work in the script block plenty. However, in migrating it to an automation script, I’m seeing a new error pop up.

I’ve confirmed that the “meeting” being passed from the trigger finds the record and then uses an object of the same format that the script block uses (script block asks the user to choose a record).
The createRecordsAsync function is throwing “Error: Request parameters failed validation.” I have a safeguard built in for the 50 record cap (in this testing instance, I’m only creating one record anyway).

Does anyone know of any additional restrictions on this function in the automation script environment? I’ve noticed some differences but that function seems to be the same.

SCRIPT BLOCK CODE

/* Get groups */
var groups = base.getTable("Staff").getField("Groups").options.choices.map(function(r){ return {
    label:r.name,
    value:r
    } });

/* Pick a pre-defined group of staff (defined in the "Groups" field in "Staff" table as well as a view.) */
let group = await input.buttonsAsync('Which group of staff?', groups);
let TBLstaff = base.getTable("Staff");
let query = await TBLstaff.selectRecordsAsync({ sorts: [ {field: "Name"} ], fields: ["Name","Groups"] });

let filtered = query.records.filter(function(r){
    if(r.getCellValue("Groups")==undefined) {
        return false;
    }
    else {
        return r.getCellValue("Groups").map(function(r){return r.id}).includes(this.id);
    }
    }, group);
// output.table(filtered.map(function(r){return r.name})); // list chosen staff in a table

/* Pick meeting, but first choose how to filter options */
// let TBLmeetings = {};
// let whichMeeting = await input.buttonsAsync('Which meeting list?', ['Meetings w/ no attendance records', 'Any']);
// if (whichMeeting === 'Meetings w/ no attendance records') {
    TBLmeetings = base.getTable("Meetings").getView("Meetings w/ no attendance records");
// } else {
//     TBLmeetings = base.getTable("Meetings");
// }

let recordsList = await TBLmeetings.selectRecordsAsync({sorts: [{field: "Date"}, {field: "Type"}]});
let recordMeeting = await input.recordAsync('Pick a meeting', recordsList, {fields:["Name", "Date", "Type"]});
if (recordMeeting) {
    output.text(`You picked ${recordMeeting.getCellValueAsString("Name")}`);
} else {
    output.text("Opps! Nothing selected. There might not be any meetings without attendance records. Click \"Run\" to start over.");
    return 0;
}

//throw JSON.stringify(recordMeeting);

/* Create links between each selected staff and the chosen meeting. */
var TBLattendance = base.getTable("Attendance");
var count = 0;
var recordsToSend = [];
var recordsCreated = [];
for( var i in filtered) {
    recordsToSend.push(
        {
            fields: {
                "Staff": [filtered[i]],
                "Meeting": [recordMeeting]
            }
        }
    );
    while (count>=50) {
        // Send every 50 records (if it were to ever have more than 50...limit for the multi create function)
        await TBLattendance.createRecordsAsync(recordsToSend);
        recordsCreated.push(recordsToSend);
        recordsToSend = [];
        count = 0;
    }
}
await TBLattendance.createRecordsAsync(recordsToSend);
recordsCreated.push(recordsToSend);
output.table(recordsCreated);

AUTOMATION SCRIPT CODE

let inputConfig = input.config();

let TBLstaff = base.getTable("Staff");
let query = await TBLstaff.selectRecordsAsync({ sorts: [ {field: "Name"} ], fields: ["Name","Groups"] });

let filtered = query.records.filter(function(r){
    if(r.getCellValue("Groups")==undefined) {
        return false;
    }
    else {
        return r.getCellValue("Groups").map(function(r){return r.id}).includes(this.id);
    }
    }, inputConfig.group);


// Get Meeting Record
let meeting = undefined;
let meetingTable = base.getTable("Meetings");
let meetingQuery = await meetingTable.selectRecordsAsync();
for (let record of meetingQuery.records) {
    if(record.id==inputConfig.meeting) {
        meeting = record;
        break;
    }
}

/* Create links between each selected staff and the chosen meeting. */
var TBLattendance = base.getTable("Attendance");
var count = 0;
var recordsToSend = [];
var recordsCreated = [];
for( var i in filtered) {
    recordsToSend.push(
        {
            fields: {
                "Staff": [filtered[i]],
                "Meeting": [meeting],
            }
        }
    );
    while (count>=50) {
        // Send every 50 records (if it were to ever have more than 50...limit for the multi create function)
        await TBLattendance.createRecordsAsync(recordsToSend);
        recordsCreated.push(recordsToSend);
        recordsToSend = [];
        count = 0;
    }
}
let recordsCrtd = await TBLattendance.createRecordsAsync(recordsToSend);
output.set("recordsCrtd",recordsCrtd);

:interrobang: This is where the error is thrown.

let recordsCrtd = await TBLattendance.createRecordsAsync(recordsToSend);
2 Replies 2
Erin_OHern
6 - Interface Innovator
6 - Interface Innovator

Hi @Chris_Buehnerkemper,

I’m not sure exactly what’s going on here based on the code alone, but I do have one tip for debugging: Try outputting the value of recordsToSend right before your createRecordsAsync call in both your scripting block code and your automations script action code, and check if there are any differences.

You can do this by adding output.inspect(recordsToSend); in your scripting block code, and console.log(recordsToSend); in your automations scripting action code.

Let us know if that uncovers any differences in the data that is being sent in your createRecordsAsync call!

One thing I notice is that you’re not doing anything to increase the count variable inside your final for loop. You’re pushing data onto the recordsToSend array, but not incrementing count each time. That means that the while loop will never trigger, and it will try to send the full batch once the for loop has completed. Even though you said you’re only testing one record, just be aware that unless you add a line to increase count by one each time you push a new object into the recordsToSend array, the while loop will never run.

Is there any more to the error message you listed above?