{
  "schema_version": "1.0.0",
  "id": "facebook-lead-round-robin",
  "title": "Round-robin a Facebook lead to the next rep",
  "category": "speed-to-lead",
  "difficulty": "advanced",
  "featured": false,
  "description": "Each Facebook Lead Ads lead is assigned to the next rep in turn with a Storage counter, texted a hello, and announced to that rep by Slack DM.",
  "tags": [
    "speed-to-lead",
    "facebook-lead-ads",
    "round-robin",
    "storage",
    "twilio"
  ],
  "apps": [
    "Facebook Lead Ads",
    "Code by Zapier",
    "Storage by Zapier",
    "Formatter by Zapier",
    "Twilio",
    "Slack"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 6 steps after the trigger",
      "Formatter by Zapier"
    ]
  },
  "tasks": {
    "typical": 3,
    "max": 3
  },
  "trigger": {
    "app": "Facebook Lead Ads",
    "event": "New Lead",
    "kind": "instant",
    "title": "New lead from the ad form",
    "why": "Facebook pushes each lead from the form to Zapier.",
    "config": {
      "Page": "YOUR_FACEBOOK_PAGE",
      "Form": "Free consultation"
    },
    "outputs": [
      "lead_id",
      "created_time",
      "full_name",
      "email",
      "phone_number",
      "ad_name",
      "form_name"
    ],
    "sample": "sample-trigger.json"
  },
  "steps": [
    {
      "key": "phone",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Format the phone number",
      "why": "Facebook passes the number as typed; Twilio needs E.164.",
      "code": {
        "language": "javascript",
        "module": "normalize-phone",
        "file": "code/normalize-phone.js"
      },
      "input_data": {
        "phone": "{{trigger.phone_number}}",
        "default_country": "1"
      },
      "outputs": [
        "e164",
        "valid",
        "extension"
      ]
    },
    {
      "key": "counter",
      "kind": "storage",
      "app": "Storage by Zapier",
      "event": "Increment Value",
      "title": "Count this lead",
      "why": "Storage by Zapier keeps one number across runs. Each lead adds one, and the remainder picks the rep.",
      "fields": {
        "Secret Storage Key": "YOUR_STORAGE_SECRET",
        "Key": "facebook_lead_counter",
        "Increment By": "1"
      },
      "outputs": [
        "amount"
      ]
    },
    {
      "key": "slot",
      "kind": "formatter",
      "app": "Formatter by Zapier",
      "event": "Numbers",
      "title": "Turn the count into a rep slot",
      "why": "MOD of the count by the number of reps gives 0, 1, 2, then 0 again.",
      "fields": {
        "Transform": "Spreadsheet-Style Formula",
        "Formula": "MOD({{counter.amount}}, 3)"
      },
      "outputs": [
        "output"
      ]
    },
    {
      "key": "rep",
      "kind": "formatter",
      "app": "Formatter by Zapier",
      "event": "Utilities",
      "title": "Look up the rep for the slot",
      "why": "A lookup table maps the slot to a Slack username. Change the table when the team changes.",
      "fields": {
        "Transform": "Lookup Table",
        "Lookup Key": "{{slot.output}}",
        "Lookup Table": {
          "0": "ayesha.k",
          "1": "bilal.r",
          "2": "chen.w"
        },
        "Fallback Value": "ayesha.k"
      },
      "outputs": [
        "output"
      ]
    },
    {
      "key": "hello",
      "kind": "action",
      "app": "Twilio",
      "event": "Send SMS",
      "title": "Text the lead",
      "why": "A first contact within a minute, from the team number, before the rep has even seen the lead.",
      "fields": {
        "From Number": "YOUR_TWILIO_NUMBER",
        "To Number": "{{phone.e164}}",
        "Message": "Hi {{trigger.full_name}}, thanks for requesting a free consultation. One of our advisors will call you shortly from this number. Reply STOP to opt out.",
        "Send Large Messages?": "No"
      },
      "outputs": [
        "sid",
        "status"
      ]
    },
    {
      "key": "assign",
      "kind": "action",
      "app": "Slack",
      "event": "Send Direct Message",
      "title": "Tell the assigned rep",
      "why": "Only the assigned rep is pinged, so ownership is clear.",
      "fields": {
        "To Username": "{{rep.output}}",
        "Message Text": "New lead for you: {{trigger.full_name}} {{phone.e164}} {{trigger.email}} from ad \"{{trigger.ad_name}}\". They have been texted; please call within 15 minutes.",
        "Send as a bot?": "Yes",
        "Bot Name": "Zap alerts"
      },
      "outputs": [
        "ts"
      ]
    }
  ]
}
