---
title: "DynamicTrigger & DynamicSchedule"
sidebarTitle: "Dynamic"
description: "These are advanced features that allows you to attach dynamic triggers to a Job."
---

Sometimes you don't know when you write the code what the trigger or schedule will be.

[DynamicTrigger](/sdk/dynamictrigger) and [DynamicSchedule](/sdk/dynamicschedule) allow you to configure triggers and schedules at runtime.

## DynamicSchedule

```typescript
//1. create a DynamicSchedule
const dynamicSchedule = new DynamicSchedule(client, {
  id: "dynamicinterval",
});

//2. create a Job that is attached to the dynamic schedule
client.defineJob({
  id: "user-dynamicinterval",
  name: "User Dynamic Interval",
  version: "0.1.1",
  //3. set the DynamicSchedule as the Trigger
  trigger: dynamicSchedule,
  run: async (payload, io, ctx) => {
    await io.logger.info("The userId is ", ctx.source.id);
  },
});

//4. Register the DynamicSchedule anywhere in your app
async function registerUserCronJob(userId: string, userSchedule: string) {
  //use the userId as the id for the DynamicSchedule
  //so it comes through to run() in the context source.id
  await dynamicSchedule.register(userId, {
    type: "cron",
    options: {
      cron: userSchedule,
    },
  });
}

//5. Register inside other Jobs
client.defineJob({
  id: "register-dynamicinterval",
  name: "Register Dynamic Interval",
  version: "0.1.1",
  trigger: eventTrigger({
    name: "dynamic.interval",
    schema: z.object({
      userId: z.string(),
      seconds: z.number().int().positive(),
    }),
  }),
  run: async (payload, io, ctx) => {
    //6. Register the DynamicSchedule
    await io.registerInterval("📆", dynamicSchedule, payload.userId, {
      seconds: payload.seconds,
    });

    await io.wait("wait", 60);

    //7. Unregister the DynamicSchedule if you want
    await io.unregisterInterval("❌📆", dynamicSchedule, payload.id);
  },
});
```

## DynamicTrigger

```typescript
//1. create a DynamicTrigger
const dynamicOnIssueOpenedTrigger = new DynamicTrigger(client, {
  id: "github-issue-opened",
  event: events.onIssueOpened,
  source: github.sources.repo,
});

//2. create a Job that is attached to the dynamic trigger
client.defineJob({
  id: "listen-for-dynamic-trigger",
  name: "Listen for dynamic trigger",
  version: "0.1.1",
  trigger: dynamicOnIssueOpenedTrigger,
  integrations: {
    slack,
  },
  run: async (payload, io, ctx) => {
    await io.slack.postMessage("Slack 📝", {
      text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`,
      channel: "C04GWUTDC3W",
    });
  },
});

//3. Register the DynamicTrigger anywhere in your app
async function registerRepo(owner: string, repo: string) {
  //the first param (key) should be unique
  await dynamicOnIssueOpenedTrigger.register(`${owner}/${repo}`, {
    owner,
    repo,
  });
}

//4. Register inside other Jobs
client.defineJob({
  id: "new-repo",
  name: "New repo",
  version: "0.1.1",
  //5. when a new repo is created in your org
  trigger: github.triggers.org({
    event: events.onNewRepository,
    org: "triggerdotdev",
  }),
  run: async (payload, io, ctx) => {
    //6. Register the dynamic trigger so you get notified when an issue is opened
    return await io.registerTrigger(
      "register-repo",
      dynamicOnIssueOpenedTrigger,
      payload.repository.name,
      {
        owner: payload.repository.owner.login,
        repo: payload.repository.name,
      }
    );
  },
});
```
