Understanding action scopes and the context object
Actions have three scopes that determine how they trigger and which records they target. The context object provides access to form values, selected records, and user information.
collection.addAction('Send email', { scope: 'Single', execute: async (context, resultBuilder) => { const user = await context.getRecord(['email', 'name']); // Process single user },});
collection.add_action( 'Send email', BaseAction.new(scope: ActionScope::SINGLE) do |context, result_builder| user = context.get_record(['email', 'name']) # Process single user end)
collection.action 'Send email', scope: :single do execute do user = record(['email', 'name']) # Process single user endend
Use when:
Sending an email to a specific user
Generating an invoice for one order
Viewing details of a single item
Resetting a user’s password
Context methods:
context.getRecord(fieldNames) - Get the selected record
const { Amount, Description } = context.formValues;// With spaces in labelconst firstName = context.formValues['First Name'];// By field id (if specified)const email = context.formValues['email'];
Get data from the records the action is running on:
const user = await context.getRecord(['id', 'email', 'name']);console.log(user.id, user.email, user.name);// Get just the IDconst userId = await context.getRecordId();
collection.addAction('Mark as live', { scope: 'Single', execute: async (context, resultBuilder) => { // Update using the filter await context.collection.update(context.filter, { status: 'live', liveAt: new Date(), }); return resultBuilder.success('Company is now live!'); },});
collection.add_action( 'Mark as live', BaseAction.new(scope: ActionScope::SINGLE) do |context, result_builder| # Update using the filter context.collection.update(context.filter, { 'status' => 'live', 'liveAt' => Time.now }) result_builder.success('Company is now live!') end)