Scripting Block - choose view based on field value

Full disclosure: I have absolutely zero experience with JavaScript (learning as I go!) which is why I am completely lost here and can’t do much searching in other people’s posts because I won’t understand the code.

I have three templates that I want to make available to my team, all separated into views within my Marketing Tracker table (views: Webinar Template, Content Template, Event Template). The script that I currently have (below) requires me to choose one of those views (in this case, the Webinar Template). Ideally, I’d like to allow my team to 1. pick the record they are creating from (selectedEvent below) and 2. pick the template they want to create from (Webinar, Content, Event). I can’t figure out how to create two selections for the team (one to pick the record, and one to pick the template). See screenshot 1.

The other thing that crossed my mind is, if possible, to have the script reference another field (if field #1 = “event” then create based on Event Template, if field #1 = “webinar” then create based on Webinar Template, otherwise, create based on Content Template). This option would allow me to create a button field and remove the room for user error. See screenshot 2.

// pick tables from your base here
let checklist = base.getTable('Marketing Tracker')
**let templateView = base.getTable('Marketing Tracker').getView('Webinar Template');**
let templateQuery = await templateView.selectRecordsAsync();
let templateRecords = templateQuery.records;
let noChecklist = base.getTable('Campaign Offer Repository').getView('Master Repository');

// Select Event to Create Checklist
let selectedEvent = await input.recordAsync('Choose Event to Create Checklist',noChecklist);

// create the tasks
let createRecords = templateRecords.map( c => ({fields: {

        'Action Item Descriptor': c.getCellValue('Action Item Descriptor'),
        'Campaign Offer': [selectedEvent],
        'Action Item': c.getCellValue('Action Item'),
        'Offer Type': c.getCellValue('Offer Type'),
        'Offer': "new offer - webinar",
        'Action Item Descriptor': c.getCellValue('Action Item Descriptor'),
        'Action Item Descriptor': c.getCellValue('Action Item Descriptor'),

}})
);

if(selectedEvent)await checklist.createRecordsAsync(createRecords)
else output.markdown("# Uh-Oh! You need to select a record.")

output.text('Done!');

If that field in question is associated with the selectedEvent, you could do something like this:

let template 
switch(selectedEvent.getCellValue('Field #1')){
    case "event" : 
        template = "Event Template"
        break;
    case "webinar" : 
        template = "Webinar Template"
        break;
    case "content" : 
        template = "Content Template"
        break;
}
let templateView = base.getTable('Marketing Tracker').getView(template)

This alternative way to set the template variable might also work if your field is a single-select type, assuming adding " Template" to the end of those values will match the name of your views:

let template = selectedEvent.getCellValue('Field #1').name + ' Template'

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.