Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Recursive Function, Issue with IFs?

582 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Mike_Pechter
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi everyone,

I’m creating a script that can be used in conjunction with the Gantt App. Each record has a duration and a pre-defined antecedent or “Preceding Step”. Sometimes, a given record has more than one antecedent, and this is where I’m having an issue in my code.

I used recursion to capture all the “children” of a given task, thus connecting them on timeline. Their start and end dates link up, while any other set of tasks could be independent.

Here is where I’m stuck: Where the code says “if(!startdate)”, the whole point is to not re-fill in a date with a second antecedent if the date is already complete. (If I can get this to work, I would compare the dates of the two antecedents and use the later one, thus having this new task begin when the latest of its multiple antecedents is completed.) But the IF appears to be letting everything through!

Even more strangely, if I run the script once, and the dates are all full, then run it again, the IF statement does work as expected, and sends me to the else.

What am I doing wrong??

async function filterAndFill(parentName, parentEndDate){

let children = query.records.filter(record => record.getCellValueAsString(“Preceding Step”) === parentName)

if(children.length > 0){

    for(let i = 0; i < children.length; i++){
        
        let child = children[i];
                    
        let duration = child.getCellValue("Duration");
        let childtask = child.getCellValueAsString("Task");

        let newStart = bumpForward(parentEndDate);
        let newEndDate = getEndDate(newStart, duration);

        await table.updateRecordAsync(child, {"Start Date": newStart, "Deadline": newEndDate});
        await filterAndFill(childtask, newEndDate);

    }
}

let childrenWithMore = query.records.filter(record => record.getCellValue("# of Antecedents") > 1);

if(childrenWithMore.length > 0){

    for(let i = 0; i < childrenWithMore.length; i++){
        
        let child = childrenWithMore[i];
                    
        let duration = child.getCellValue("Duration");
        let childtask = child.getCellValueAsString("Task");
        let antecedent = child.getCellValueAsString("Preceding Step");
        let startDate = child.getCellValue("Start Date");

        if(antecedent.includes(parentName)){

            if(!startDate){

                let newStart = returnLaterDate(startDate, parentEndDate);

                newStart = bumpForward(parentEndDate);
                let newEndDate = getEndDate(newStart, duration);  

                await table.updateRecordAsync(child, {"Start Date": newStart, "Deadline": newEndDate});
                await filterAndFill(childtask, newEndDate);

            }else{

                console.log("Already Complete!");
            }
        }
    }   
}

}

0 Replies 0