Script - promises created during execution left in unfulfilled state

Please help with this script.

Use case: I want to create a page in which its paragraphs are entities, and use an automation button to merge these paragraphs in a new entity. The page database is ‘Y Blocks Page’ and the paragraphs database is ‘Y Block’, the relation field is called ‘Y Blocks’, the merged entity database is ‘Y Page’.

Script goal: To fetch child entities from a specific ‘Y Blocks Page’ entity, merge their description fields, and then create a new ‘Y Page’ entity with this combined description.

Actions:

  1. Fetch the current ‘Y Blocks Page’ entity.
  2. Retrieve related ‘Y Block’ entities associated with this ‘Y Blocks Page’.
  3. Merge the ‘Description’ fields of all fetched ‘Y Block’ entities into a single string.
  4. Create a new ‘Y Page’ entity, naming it “Merge [timestamp]” and setting its ‘Description’ to the merged string.
  5. Log the creation of the new ‘Y Page’ entity.

The following script throws the error

Failed to execute Action “Script”: Execution completed but 13 of 17 promises created during execution left in unfulfilled state. Most likely you miss ‘await’ keyword somewhere.

Here is the script:

// Accessing Fibery's API service
const fibery = context.getService('fibery');
const yBlocksPageType = 'Yuri/Y Blocks Page'; // Replace with the actual Type name of your 'Y Blocks Page'

async function fetchAndProcessYBlocksEntities() {
    try {
        // Getting the first entity from the current entities, which should be a 'Y Blocks Page'
        const currentEntity = args.currentEntities[0];

        // Fetching the 'Y Blocks Page' entity and its related 'Y Blocks' entities
        const yBlocksPageEntity = await fibery.getEntityById(yBlocksPageType, currentEntity.id, ['Y Blocks']);
        const yBlocksEntities = yBlocksPageEntity['Y Blocks'] || [];

        // Initializing a variable to hold the merged descriptions
        let mergedDescription = '';

        // Iterating over each 'Y Block' entity to fetch and append its description
        for (const yBlock of yBlocksEntities) {
            const yBlockEntity = await fibery.getEntityById('Y Block', yBlock.Id, ['Description']);
            mergedDescription += yBlockEntity['Description'] + '\n';
        }

        // Generating a timestamp for naming the new entity
        const timestamp = new Date().toISOString();

        // Creating a new 'Y Page' entity with the merged description
        const newYPageEntity = await fibery.createEntity('Y Page', {
            'Name': `Merge ${timestamp}`,
            'Description': mergedDescription
        });

        // Logging the result of the new entity creation
        console.log('New Y Page entity created:', newYPageEntity);

    } catch (error) {
        // Error handling for any issues that occur during the script execution
        console.error('Error in script:', error);
    }
}

// Executing the function to start the process
fetchAndProcessYBlocksEntities();

As the error tells you, you need an await keyword:
await fetchAndProcessYBlocksEntities();

2 Likes

Every fibery call that accepts a “type” argument will need the “full type”, e.g. “SpaceName/DbName”.

In your code above, instead of the types 'Y Page' and 'Y Block' you probably need to include the Space name, e.g. 'Yuri/Y Page'.

1 Like