Reading from rich text field of child entities in action button

I have a type that contains a collection of child entities (“Revisions”). Each revision has a rich text field called “Response”. I’m trying to make an action button that creates a document containing the contents of the “Response” field for all of the child “Revision” entities.

The following is my best attempt at making this work, but it fails on the second call to fibery.getEntityById with a 400 error. Does anyone have a solution?

<%
    const fibery = context.getService('fibery');
    const entityWithRevisions = await fibery.getEntityById(Entity.Type, Entity.Id, ['Revisions']);
    entityWithRevisions.Revisions.forEach((rev) => {
    const revWithExtraFields = await fibery.getEntityById("Papers/Revision", rev.Id, ['Response']);
   var desc = await fibery.getDocumentContent(revWithExtraFields.Response.Secret);
%>
**<%= rev.Name %>**:  <%= desc %>
<%  });%>

Never mind, figured it out! The problem was using await inside the forEach function. Writing it in a script action was a helpful way to debug.

In case it’s helpful to anyone else, the following code works:

<%
    const fibery = context.getService('fibery');
    const entityWithExtraFields = await fibery.getEntityById(Entity.type, Entity.id, ['Revisions']);
    for (var i = 0; i < entityWithExtraFields.Revisions.length; i++) {
        var rev = entityWithExtraFields.Revisions[i];
        var revWithExtraFields = await fibery.getEntityById("Papers/Revision", rev.Id, ['Response']);
        var desc = await fibery.getDocumentContent(revWithExtraFields.Response.Secret);
%>

**<%= rev.Name%>**: <%= desc %>

<% } %>
3 Likes