# Generated tasks for an entity

**URL:** <https://community.fibery.io/t/generated-tasks-for-an-entity/754>\
**Category:** Get Help\
**Created:** [June 8, 2020, 1:08pm UTC](https://community.fibery.io/t/generated-tasks-for-an-entity/754 "2020-06-08T13:08:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haslien](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/haslien/32/493_2.png) [@Haslien](https://community.fibery.io/u/Haslien)\
**Post date:** [June 8, 2020, 1:08pm UTC](https://community.fibery.io/t/generated-tasks-for-an-entity/754/1 "2020-06-08T13:08:51Z")

</div>

I’m checking if it will be worth generating tasks based on picked groups of tasks, sort of like templates for a set of tasks. An entity may make use of multiple templates.

You then click on a button of the entity once you have selected your templates, and it generates them under a different Type in the same App, and reference the order ID in a different text field of the task.

**Wondering if there’s a better way to go about it; either the setup as a whole, or the way of generating.** Any ideas or feedback?

A concern I have is if you’re generating the set of tasks more than once by accident.

It works, but could probably better with for instance referencing the Customer type instead of a text field with ID.

The fields of concern here are highlighted with green, with `Bestilling` as the entrypoint.  
`Bestilling` has multiple `Oppgave Template`, which is made up of a set of `Oppgave`.

Select the template(s) in `Bestilling`, click button, and the `Oppgave`’s are “copied” over to `Oppgave` (purple) with a text field referencing an order/customer ID in `Bestilling`.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/7/7ee59aa90c3c6281493288e065acb17e4c800daa.png)

# `Bestilling` button

```js
const api = context.getService("fibery");
const e = args.currentEntities[0];

// Get full schema for current entity
const t = await api.getEntityById(e.type, e.id, ["Oppgave maler"]);

if (!t["Oppgave maler"].length) return; // Quit early if no task templates assigned.

// Access the map for tasks and merge all templates in to array of their ID's
const templateIds = [].concat(...t["Oppgave maler"]).map(o => o.id);

// Get the tasks of these ID's, return arrays of 'Oppgave' entity, which is referred to as plural 'Oppgaves' in type 'Ressurser/Oppgave Template'.
let allTasks = await api.getEntitiesByIds("Ressurser/Oppgave Template", templateIds, ["Oppgaves"]);

// [{id: "x", Oppgaves:[{id: "x"},{id: "x"}]}, {id: "x", Oppgaves:[{id: "x"},{id: "x"}]}]
allTasks = [].concat(...allTasks.map(template => template["Oppgaves"].map(oppgave => oppgave.id)));

// allTasks are now ID's of specific tasks ["x","x"]. Ignore editor's warning on 'allTasks'
let tasks = await api.getEntitiesByIds("Ressurser/Oppgave", allTasks, ["Name"]);

// 'tasks' is now array of objects: {id: "x", "Name":"Some task"}
// Create tasks in current app
await asyncForEach(tasks, async _t=> {
    await api.createEntity(e.type.split("/")[0] + "/Oppgave", { "Name": _t.Name, "Kundenummer": e.Kundenummer })
});

// Async await forEach to avoid pending promise error
async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
        await callback(array[index], index, array);
    }
}

```

## Bestilling, picking and generating tasks

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/0/03d9c4e05f21248c478e70c393f26218722dbc57.png)

## Tasks generated from clicking button

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/0/0fa7e92776616a510466a543cf5674462f44182e.png)

## A task template, with a set of tasks

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/5/52d90e04a540e3add956a5bc5e6c8beb92a54bf0.png)
