Use automation step results in automation scripting

Is it possible to use/call automation step results in scripts?

For example, I have an automation script the triggers when a Material entity is linked to a Box entity. If I create the automation in the Box DB I can get the Box entity info when I use args.currentEntities, but not the Material info. I have the opposite problem if I make the automation in the Materials DB. As a Box entity has many Material entities, I either need to get the Material that triggered the entity or filter all connected entities by most recently added. I’ve attempted filtering with GraphQL queries but haven’t been successful so far. I’ve attached images to show the automation trigger and the entity info.


2 Likes

Something like this maybe?

const entity = args.currentEntities[0];

const linkedEntities = await fibery.getEntityById(entity.type, entity.id, ['Box', 'linked Material'];

const linkedBoxes = linkedEntities['Box'].map(({ id }) => id);
[...]

See this article for a better example (if I understood your context properly).

1 Like

This may or may not work, as it seems to be indeterminate whether previous steps have truly been “completed” when subsequent steps are run:

1 Like

Thanks all! I will try these suggestions and update.

2 Likes

@Chr1sG Thanks for the info! I’m still not sure that I’m able to get the correct entity info from the first step. When I log args.steps[1] it only returns the entity that the automation has triggered for, not the linked entity. Apologies if I’m just totally messing up the syntax.

You’re not messing anything up. The steps[1] argument does indeed only return the triggering entity and not the entity that was linked. Sorry.

1 Like

Alright, after quite a bit of messing around I think I found an ok way to do this. It seems like grabbing the first item in the array when sorting by modification date should always get you the most recently linked entity and therefore the entity that triggered the automation. I would be curious if there is a better way to do this, but this works ok for my purposes. As usual I SUPER appreciate everyone’s help!

const fibery = context.getService('fibery')
//Insert the name of your space below
const SPACE_NAME = ''
const entity = args.currentEntities[0]
//Modify the query for you scenario, the database I'm querying from is called 'Box' and the linked database is 'Material'
const query = `{findBoxes(id: {is: "${entity.id}"}) {materials(orderBy: {modificationDate: DESC}) {id, name, modificationDate}}}`
const result = await fibery.graphql(SPACE_NAME, query)
const firstMaterialId = result.data.findBoxes[0].materials[0].id;
console.log(firstMaterialId);
console.log(result)