Action

Add mulitiple tasks to project

Posted by @howarddyer, Last update over 5 years ago

Add multiple tasks to a selected Project in Todoist.
Tasks should be separated by a new line.

Tip: Add the project as the first line to add the tasks to a project automatically, for example:

Shopping list

Apples
Pears

Steps

  • script

    // Add multiple tasks to a selected Project in Todoist
    
    (function () {
        let todoist, prompt, projectLookup = {};
        let projects, projectNames, tasks = [];
        let selectedProjectAsserted;
        let selectedProjectId;
        let selectedProjectName;
    
        function initialise () {
            _getData();
            _assertProject();
            _showPrompt();
            _getSelectedProject();
            _addTasks();
        }
    
        function _getData () {
            todoist = Todoist.create();
            prompt = Prompt.create();
            projects = todoist.getProjects();
            tasks = draft.content.split("\n");
    
            if (!projects) {
                _failAction("Unable to retrieve projects");
            }
            
            projectLookup = _mapProjects(projects, "name", "id");
            projectNames = Object.keys(projectLookup);
        }
        
        function _assertProject() {
            for (let project in projectNames) {
                if (tasks[0] == projectNames[project]) {
                    selectedProjectAsserted = true;
                    selectedProjectName = tasks[0];
                }
            }
        }
        
        function _showPrompt () {
            
            if (!selectedProjectAsserted) {
    
                prompt.title = "Add tasks to Project in Todoist";
                    prompt.addPicker("project", "Project", [projectNames], [0]);
                prompt.addButton("Add tasks");
    
                if (!prompt.show()) {
                    _failAction("Unable to show prompt")
                    return true;
                }
            }
        }
    
        function _getSelectedProject () {
            if (!selectedProjectAsserted) {
                let projectIndex = prompt.fieldValues["project"][0];
                selectedProjectName = projectNames[projectIndex];
            }
    
            selectedProjectID = projectLookup[selectedProjectName];
        }
    
        function _addTasks () {
            for (let i = 0; i < tasks.length; i++) {
                if (tasks[i].length > 0 && !(selectedProjectAsserted && i === 0)) {
                    let taskData = todoist.createTask({
                        "content": tasks[i],
                        "project_id": selectedProjectID
                    });
    
                    if (!taskData) { return false }
                }
            }
        }
    
        function _mapProjects (arr, key, valKey) {
            let result = {};
            
            arr.forEach(o => {
                result[o[key]] = o[valKey];
            })
            return result;
        }
    
        function _failAction (errorMessage) {
            context.fail();
            console.log(errorMessage)
        }
        
        return initialise;
        
    })()();

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.