createScheduledAction

The createScheduledAction method creates a new scheduled event in the content source.

Return Value
Promise<{ newScheduledActionId: string }>

When scheduled actions are enabled for a content source, this method handles creating a new scheduled action. It is fired when a user chooses to schedule a publish event, rather than immediately publishing.

See getScheduledActions for more information on the feature.

Parameters

name

string

Name for the action being scheduled.

action

ScheduledActionActionType

The type of action. Currently only publish is supported.

documentIds

string[]

A list of document ID values that are to be scheduled. See Document for details.

executeAt

string

ISO formatted string used to determine the time at which the event should be triggered.

userContext

User<UserContext>

See UserContext for details.

Example

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
import type {
  ContentSourceInterface,
  ScheduledAction,
  ScheduledActionActionType,
} from '@stackbit/types'

interface UserContext {
  accessToken: string
}

export class MyContentSource implements ContentSourceInterface<UserContext> {
  async createScheduledAction({
    documentIds,
    name,
    action,
    executeAt,
    userContext,
  }: {
    documentIds: string[]
    name: string
    action: ScheduledActionActionType
    executeAt: string
    userContext?: UserContext
  }): Promise<{ newScheduledActionId: string }> {
    // optionally use userContext.accessToken to create the schedule on the user's behalf
    const cmsSchedule = await this.apiClient.createScheduledAction({
      documents: documentIds,
      executeAt: executeAt,
      name: name,
    })

    // convert the cmsSchedule to stackbit's ScheduledAction type
    const schedule = convertScheduledAction(cmsSchedule)
    return { newScheduledActionId: schedule.id }
  }

  // other methods ...
}

Logging

When this method fails, the error is rendered in the UI so that editors receive feedback on the failure.

This method should be implemented alongside the other scheduled action methods: