Collaborator

Documentation for the Collaborator object in NocoDB Scripts

The Collaborator object represents a user who has access to a NocoDB base. It contains key identity attributes such as the user's ID, name, and email address.

Overview

Collaborator objects appear in several contexts within NocoDB Scripts:

  • session.currentUser returns a Collaborator object representing the current user running the script
  • User fields in records return Collaborator objects
  • base.activeCollaborators returns an array of Collaborator objects for the current base
  • Field types such as CreatedBy and LastModifiedBy return Collaborator objects

This object enables user identification, tracking ownership, assigning responsibilities, and implementing user-specific logic in scripts.

Properties

PropertyTypeDescription
idstringThe unique identifier of the collaborator (read-only)
namestring | nullThe display name of the collaborator (read-only, may be null if not set)
emailstringThe email address of the collaborator (read-only)

Note: All Collaborator properties are read-only and cannot be modified.

Contexts Where Collaborator Objects Appear

Session Object

The session.currentUser property returns a Collaborator object representing the current user running the script:

// Get the current user
const currentUser = session.currentUser;

output.text(`Current user: ${currentUser.name || currentUser.email}`);
output.text(`User ID: ${currentUser.id}`);
output.text(`Email: ${currentUser.email}`);

Base Object

The base.activeCollaborators property returns an array of all Collaborator objects for the current base:

// Get all collaborators for this base
const collaborators = base.activeCollaborators;

output.markdown('# Base Collaborators');
output.text(`Number of collaborators: ${collaborators.length}`);

// List all collaborators
for (const collaborator of collaborators) {
  output.text(`- ${collaborator.name || 'Unnamed'} (${collaborator.email})`);
}

The base.getCollaborator() method allows you to find a specific Collaborator by ID, name, or email:

// Find a collaborator by email
const collaborator = base.getCollaborator('john.doe@example.com');

if (collaborator) {
  output.text(`Found collaborator: ${collaborator.name || 'Unnamed'}`);
  output.text(`ID: ${collaborator.id}`);
} else {
  output.text('Collaborator not found.');
}

User Field

When a table has a User field type, getCellValue() returns a Collaborator object (or an array of Collaborator objects if multiple selection is enabled):

// Get a record from the Tasks table
const tasksTable = base.getTable('Tasks');
const taskRecord = await tasksTable.selectRecordAsync('rec123456');

// Get the assigned user (assuming 'Assigned To' is a User field)
const assignedTo = taskRecord.getCellValue('Assigned To');

if (assignedTo) {
  // This is a Collaborator object
  output.text(`Task assigned to: ${assignedTo.name || assignedTo.email}`);
} else {
  output.text('Task is unassigned.');
}

// For a User field that allows multiple users
const reviewers = taskRecord.getCellValue('Reviewers') || [];

if (reviewers.length > 0) {
  output.text('Reviewers:');
  for (const reviewer of reviewers) {
    // Each reviewer is a Collaborator object
    output.text(`- ${reviewer.name || reviewer.email}`);
  }
} else {
  output.text('No reviewers assigned.');
}

Created By and Last Modified By Fields

The CreatedBy and LastModifiedBy field types return Collaborator objects:

// Get a record with system user fields
const recordsTable = base.getTable('Documents');
const document = await recordsTable.selectRecordAsync('rec123456');

// Get the user who created the record
const createdBy = document.getCellValue('Created By');
if (createdBy) {
  output.text(`Created by: ${createdBy.name || createdBy.email}`);
  output.text(`Creation date: ${document.getCellValueAsString('Created Time')}`);
}

// Get the user who last modified the record
const lastModifiedBy = document.getCellValue('Last Modified By');
if (lastModifiedBy) {
  output.text(`Last modified by: ${lastModifiedBy.name || lastModifiedBy.email}`);
  output.text(`Last modified: ${document.getCellValueAsString('Last Modified Time')}`);
}

Working with Collaborator Objects

Finding a Specific Collaborator

// Get all collaborators
const collaborators = base.activeCollaborators;

// Find a collaborator by email (case-insensitive)
function findCollaboratorByEmail(email) {
  const lowerEmail = email.toLowerCase();
  return collaborators.find(collaborator => 
    collaborator.email.toLowerCase() === lowerEmail
  );
}

// Find a collaborator by name (case-insensitive)
function findCollaboratorByName(name) {
  const lowerName = name.toLowerCase();
  return collaborators.find(collaborator => 
    collaborator.name && collaborator.name.toLowerCase().includes(lowerName)
  );
}

// Example usage
const collaborator1 = findCollaboratorByEmail('jane.smith@example.com');
const collaborator2 = findCollaboratorByName('john');

if (collaborator1) {
  output.text(`Found by email: ${collaborator1.name || collaborator1.email}`);
}

if (collaborator2) {
  output.text(`Found by name: ${collaborator2.name || collaborator2.email}`);
}

Checking if Current User Matches a Specific Collaborator

// Get the current user
const currentUser = session.currentUser;

// Check if the current user is a specific person
function isUser(emailOrId) {
  if (currentUser.email.toLowerCase() === emailOrId.toLowerCase()) {
    return true;
  }
  
  if (currentUser.id === emailOrId) {
    return true;
  }
  
  return false;
}

// Example usage
if (isUser('admin@example.com')) {
  output.text('You are the admin user.');
  // Show admin-specific content
} else {
  output.text('You are not the admin user.');
  // Show regular user content
}

Best Practices

  1. Check for null names - Always check if collaborator.name is null before using it, as not all users have names set. Use collaborator.name || collaborator.email as a fallback.

  2. Be cautious with personal information - Be mindful about how you display and use collaborator information, particularly when creating outputs that might be shared.

  3. Handle missing collaborators - When working with User fields, always check if the value is null before trying to access Collaborator properties.

  4. Consider multi-user fields - Remember that User fields can be configured to allow multiple selections, so the value might be an array of Collaborator objects.

  5. Use IDs for references - When updating records with User fields, use the collaborator's ID as the value, not the entire Collaborator object.