Help

If / else / else if Statement in Scripting App

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

I have 2 tables that I am working with, one called Projects and the other called Tracking (which are essentially tasks). I created a button in the Projects table that will run the script below when pressed. I have 3 separate template views in the Tracking table: 1) Residential Diagnostic 2) Development Planning and 3) Internal. Whenever I run the script, it enters the tasks associated with the Internal view regardless of the Work Type I select. Can anyone tell me where I am going wrong?

// pick tables & views from your base here
let checklistTable = base.getTable(“Tracking”);
let projectsTable = base.getTable(“Projects”);
let allProjects = projectsTable.getView(“All Projects, all fields”);

// select an event to create a checklist for
let selectedEventRecord = await input.recordAsync(
“Choose a project to create checklist for”,
allProjects,
);

let workType = selectedEventRecord.getCellValue(‘Work Type Text’)

if (workType == “Residential Diagnostic”) {
let templateView = checklistTable.getView(“Residential Diagnostic”)
} else if (workType == “Development Planning”) {
let templateView = checklistTable.getView(‘Development Planning’)
} else (workType == “Internal”)
let templateView = checklistTable.getView(“Internal”)

output.text(${workType}.);

if (selectedEventRecord) {
// load in all of of the tasks that our in our template
let templateQuery = await templateView.selectRecordsAsync();
let templateRecords = templateQuery.records;
// create new tasks based on the template
let recordsToCreate = templateRecords.map((templateRecord) => ({
fields: {
Task: templateRecord.getCellValue(“Task”),
Project: [selectedEventRecord], },
}));
await checklistTable.createRecordsAsync(recordsToCreate);
} else {
output.markdown("#You didn’t select a project!");
}
output.text(“Done!”);

2 Replies 2

The forum frontend wrecked your code and I gave up trying to get it to run after about five minutes, not including the time to set up mock tables.

You can use triple grave ``` accents to post code without forcing it on a ride through overprocessingville and combine that with a single four-space indent for optimal results:

which will look like this

As for the code, I believe the issue is with your polling logic and scoping variable instances. Could you please try replacing the let keyword with the var one? Variables you ‘let’ into existence are scoped to block statements. Writing code in a one statement = one line fashion isn’t always feasible, but it would help you catch these issues moving forward, this new insight in tow.

Hi @Jeremy_Taylor - I think it may be this part that is causing the issue:

let workType = selectedEventRecord.getCellValue(‘Work Type Text’)

What type of field is Work Type Text? Single select, link? The code you have would work if the field was just text I think, but if it is a single select, for example, the cell value returned will be an object:

{
id: aaa,
name: bbb,
colour: ccc
}

So, to make your script work you will need:

let workType = selectedEventRecord.getCellValue(‘Work Type Text’).name

(for other field types the value returned might be something different, e.g. array of objects, so the answer to this depends upon the specific field type).

But, because workType is set to the whole thing (object) that is returned rather than the name you don’t get a match on the first two conditions and always end up on the 3rd, “Internal”. Arguably you could have 4 conditions here - type1, type2, type3 and any other value and any other value would lead to the script halting at that point.