Context

This is the official documentation of Forest Admin Cloud.

The context of an action contains all the context data when the action is executed in Forest Admin.


To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and action names as needed.

import type { Agent } from '@forestadmin/forest-cloud';
import { Schema } from '../typings';

export default function customizeAgent(agent: Agent<Schema>) {
  agent.customizeCollection('user', (collection) => {
    collection.addAction('Ban user', {
      scope: 'Single',
      // Insert the code snippet here.
    });
  });
}

Getting the selected record(s)

To get the selected record(s) on which the action is executed, use the getRecord() or getRecords() methods from the context.

Example:

execute: async (context) => {
  const { id, email } = await context.getRecord(['id', 'email']);
},

Getting the selected record id(s)

Additionally, you can retrieve just the id(s) by using getRecordId() or getRecordIds()

Example:

execute: async (context) => {
  const id = await context.getRecordId();
},

Getting the collection

To access the collection associated with an action, use the collection attribute. To see the capabilities of what collection provides, please see the following section: Query interface and Native Queries

Example:

execute: async (context) => {
  await context.collection.update(context.filter, { isBanned: true });
},

Last updated