Tree Hierarchy in Tabidoo

Hi, Jason again…
I have created the table Tasks. For every Task it is possible to set superior Task (link to table Task:Task).
I can now see Tasks in hierarchical view (GRID).
But I don’t know how to limit number of levels in hierarchy. In my case I need to limit to 3 levels.
thanks for the tip
Jason

Hello Jason,

thank you for an interesting question. It is good to know that you are using the hierarchy view.
There is a way how to control the level of nesting, it can be set by JavaScript:

const ticketTableId = ‘tasks’;

doo.model.isValid = true;
await parentTicketConstraint();

async function parentTicketConstraint() {
    debugger;
    const parentTicketCountPromise = calculateParentTicketsCount(doo.model.linkedField?.value?.id);
    const childTicketCountPromise = calculateChildTicketsCount(doo.model.id);

    const parentTicketCount = await parentTicketCountPromise;
    const childTicketCount = await childTicketCountPromise;

    if (parentTicketCount + childTicketCount >= 2) {
        doo.model.isValid = false;
        doo.toast.error('Tasks cannot have a nesting level greater than 2.', 'Error');
    }
}

// returns count of Parent tickets of the current ticket
async function calculateParentTicketsCount(parentTicketId?: string): Promise<number> {
  debugger;
    if (!parentTicketId)
        return 0;

    const parentTicket = await doo.table.getRecord(ticketTableId, parentTicketId);
    let parentsCount = await calculateParentTicketsCount(parentTicket.data?.fields?.linkedField?.id);

    return parentsCount + 1;
}

// returns count of child tickets of the current ticket
async function calculateChildTicketsCount(ticketId?: string): Promise<number> {
   debugger;
    if (!ticketId)
        return 0;

    const childTickets = await doo.table.getLinkedRecordsV2(ticketTableId, ticketId, 'linkedField');

    if (childTickets.data.length === 0)
        return 0;

    let promises = [];

    for (let childTicket of childTickets.data) {
        const promise = calculateChildTicketsCount(childTicket.id);
        promises.push(promise);
    }

    let maxDepth = 0;

    for (let promise of promises) {
        let currentDepth = await promise;

        if (maxDepth < currentDepth)
            maxDepth = currentDepth;
    }

    return maxDepth + 1;
}

We would most likely use it in the section Before save as it is bound to the validity of the model.
I hope this helps you to manage your table even better.

Anna

I will give it a try. Thanks Anna for fast answer.
Jason