Forest Admin Cloud
  • What is Forest Admin?
  • How to Read This Doc?
  • Quick Start
  • Core Concepts
    • Agent
    • Data Sources
    • Collections
    • Views
    • Fields
      • Types
    • Relationships
    • Actions
    • Segments
    • Dashboards
    • Workspaces
    • Layouts
    • Roles & Permissions
    • Projects
    • Organizations
  • Native Modules
    • CRUD
    • Approval Workflows
    • Activity Logs
    • Notes
    • Inbox
  • UI Customization
    • Actions
    • Workspaces
    • Analytics
      • Metabase Integration
  • Code Customization
    • Getting Started
    • Actions
      • Context
      • Response
        • Data refresh
      • Form
        • Widgets
    • Fields
      • Filtering
      • Sorting
      • Validation
    • Relationships
    • Collections
      • Search
    • Charts
  • Security Modules
    • Roles & Permissions
    • SCIM User Provisioning
    • Single Sign On
    • Two-Factor Authentication
    • Auto-logout
    • IP Whitelisting
  • Data sources settings
    • SQL
    • MongoDB
    • Multiple Data Sources
    • Database Credentials
    • Schema Synchronization
  • Best Practices
    • Performance
Powered by GitBook
On this page
  • Getting the selected record(s)
  • Getting the selected record id(s)
  • Getting the collection

Was this helpful?

  1. Code Customization
  2. Actions

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']);
},
execute: async (context) => {
  const { id, email } = await context.getRecords(['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();
},
execute: async (context) => {
  const ids = await context.getRecordIds();
},

Getting the collection

Example:

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

Last updated 1 year ago

Was this helpful?

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