Octov0.11.17
Guides

Sync Contacts and Opportunities with Odoo

Upsert contacts in bulk, close opportunities from outside events, and schedule follow-ups on stale deals.

This guide keeps an Odoo CRM in step with the systems around it: a batch of contacts is upserted by email, an outside event marks an opportunity won or lost, and a weekday sweep plans a follow-up on deals that have stopped moving. It follows samples/odoo-pipeline-sync. For connector setup and the lead-capture basics, start with Odoo CRM and Sales.

You need the Contacts and CRM apps installed and an API key. Run the sample with:

export ODOO_URL=https://mycompany.odoo.com
export ODOO_API_KEY=...
octo run --config samples/odoo-pipeline-sync

Upsert a batch of contacts

POST /contacts/sync takes a list of people from another system and matches each one to a contact by email. foreach in map mode turns the batch into one result per contact. Each iteration runs on its own scope, so one contact's lookup never leaks into the next:

- type: foreach
  name: each-contact
  items: body.contacts
  as: contact
  mode: map
  body:
    process:
      - type: odoo-search-read
        name: find-existing
        settings:
          connector: odoo
          model: '"res.partner"'
          domain: '[["email", "=ilike", vars.contact.email]]'
          fields: [id]
          limit: 1
          resultVar: existing
      - type: if
        name: contact-known
        condition: size(vars.existing) > 0
        then:
          process:
            - type: odoo-write
              name: update-contact
              settings:
                connector: odoo
                model: '"res.partner"'
                ids: vars.existing[0].id
                values: '{"name": vars.contact.name, "phone": vars.contact.phone}'
            - type: set-payload
              settings:
                value: '{"email": vars.contact.email, "id": vars.existing[0].id, "action": "updated"}'
        else:
          process:
            - type: odoo-create
              name: create-contact
              settings:
                connector: odoo
                model: '"res.partner"'
                values: '{"name": vars.contact.name, "email": vars.contact.email, "phone": vars.contact.phone}'
                resultVar: created
            - type: set-payload
              settings:
                value: '{"email": vars.contact.email, "id": vars.created[0], "action": "created"}'

The loop's body is the list of per-contact results, so one set-payload counts them:

- type: set-payload
  name: summarize
  settings:
    value: |
      {"created": body.filter(r, r.action == "created").size(),
       "updated": body.filter(r, r.action == "updated").size(),
       "contacts": body}
curl -s -X POST localhost:8080/contacts/sync -d '{"contacts": [
  {"name": "Ada Lovelace", "email": "ada@example.com", "phone": "+44 20 7946 0001"},
  {"name": "Grace Hopper", "email": "grace@example.com", "phone": "+1 202 555 0143"}]}'
# {"created": 1, "updated": 1, "contacts": [{"action": "updated", "email": "ada@example.com", "id": 12}, ...]}

Close an opportunity from an outside event

POST /opportunities/outcome receives an event such as a signed contract or a declined quote, and closes the opportunity it names. Won and lost are workflow actions on crm.lead, not field writes: action_set_won moves the deal to the won stage at 100% probability, and action_set_lost archives it with an optional reason. Both go through odoo-call.

The sample is a folder in the globals-file layout: octo.yaml declares the service, env and connectors, and each area's flows sit beside it. This endpoint lives in opportunities.yaml, and each outcome is a subflow of its own, so its suite tests it with an event instead of through the router. opportunity-outcome only routes:

- type: switch
  name: by-outcome
  cases:
    - when: body.outcome == "won"
      process:
        - type: enrich
          name: close-won
          body:
            process:
              - type: flow-ref
                name: call-close-won
                settings:
                  flow: close-won
    - when: body.outcome == "lost"
      process:
        - type: enrich
          name: close-lost
          body:
            process:
              - type: flow-ref
                name: call-close-lost
                settings:
                  flow: close-lost

A flow-ref answers with the subflow's body, and the read at the end of the flow still needs the event's opportunityId. The enrich around each call keeps the subflow's answer inside its scope, so the event is still the body afterwards.

Inside a subflow every step is one odoo block, so each keeps its result out of the body with resultVar instead of an enrich of its own:

- name: close-won
  process:
    - type: odoo-write
      name: write-amount
      settings:
        connector: odoo
        model: '"crm.lead"'
        ids: body.opportunityId
        values: '{"expected_revenue": body.amount}'
        resultVar: amountWritten
    - type: odoo-call
      name: set-won
      settings:
        connector: odoo
        model: '"crm.lead"'
        method: '"action_set_won"'
        ids: body.opportunityId
        resultVar: wonResult

A lost reason is a record of crm.lost.reason, so close-lost looks up the name the event sends, then passes the reason only when one matched:

- type: odoo-search-read
  name: find-reason
  settings:
    connector: odoo
    model: '"crm.lost.reason"'
    domain: '[["name", "=ilike", body.reason]]'
    fields: [id]
    limit: 1
    resultVar: reasons
- type: odoo-call
  name: set-lost
  settings:
    connector: odoo
    model: '"crm.lead"'
    method: '"action_set_lost"'
    ids: body.opportunityId
    params: 'size(vars.reasons) > 0 ? {"lost_reason_id": vars.reasons[0].id} : {}'
    resultVar: lostResult

Each subflow ends by posting a note to the opportunity's chatter with message_post, and opportunity-outcome answers with the opportunity as it now stands:

curl -s -X POST localhost:8080/opportunities/outcome -d '{"opportunityId": 3, "outcome": "lost", "reason": "too expensive"}'
# [{"id": 3, "active": false, "probability": 0, "lost_reason_id": [1, "Too expensive"], ...}]

Reading a lost opportunity by id works even though it is archived. Reason names match case-insensitively; a name with no matching reason marks the deal lost without one.

Follow up on stale deals

stale-followups runs at 08:00 on weekdays (0 0 8 * * 1-5, six fields with seconds). It finds open opportunities whose stage has not changed in 14 days. Odoo compares datetimes as YYYY-MM-DD HH:MM:SS in UTC, so the cutoff is computed from now in that shape:

- type: odoo-search-read
  name: find-stale
  settings:
    connector: odoo
    model: '"crm.lead"'
    domain: |
      [["type", "=", "opportunity"],
       ["probability", "<", 100],
       ["activity_ids", "=", false],
       ["date_last_stage_update", "<",
        string(now - duration("336h")).substring(0, 19).replace("T", " ")]]
    fields: [id, name, user_id]
    limit: 100
    resultVar: stale

["activity_ids", "=", false] skips deals that already have something planned, so running the sweep twice schedules nothing new. Each stale deal then gets a to-do activity through activity_schedule, a method every record with a chatter has. Without a user_id Odoo leaves the activity unassigned, so the flow passes the salesperson when the opportunity has one:

- type: foreach
  name: each-stale
  items: vars.stale
  as: opportunity
  body:
    process:
      - type: odoo-call
        name: schedule-followup
        settings:
          connector: odoo
          model: '"crm.lead"'
          method: '"activity_schedule"'
          ids: vars.opportunity.id
          params: |
            type(vars.opportunity.user_id) == list
              ? {"act_type_xmlid": "mail.mail_activity_data_todo",
                 "summary": "Follow up: no stage change in 14 days",
                 "date_deadline": string(now + duration("48h")).substring(0, 10),
                 "user_id": vars.opportunity.user_id[0]}
              : {"act_type_xmlid": "mail.mail_activity_data_todo",
                 "summary": "Follow up: no stage change in 14 days",
                 "date_deadline": string(now + duration("48h")).substring(0, 10)}

Run the sweep now instead of waiting for the schedule:

octo invoke --config samples/odoo-pipeline-sync --flow stale-followups

Test it

The sample has one suite per flow, each named after its flow and never after a file, so dolphin runs every one against the whole folder: sync-contacts_test.yaml, opportunity-outcome_test.yaml, close-won_test.yaml, close-lost_test.yaml and stale-followups_test.yaml. Each mocks the odoo-* blocks. In the contact sync, the lookup's mock answers per contact by matching on the loop variable, so one batch exercises both the update and the create path:

mocks:
  sync-contacts.each-contact[body].find-existing:
    cases:
      - when: vars.contact.email == "ada@example.com"
        body: *batch
        vars: { existing: [{ id: 12 }] }
    default:
      body: *batch
      vars: { existing: [] }

opportunity-outcome_test.yaml mocks the two flow-ref calls and checks the routing and that the event survives them. What each outcome does in Odoo is pinned by close-won_test.yaml and close-lost_test.yaml, which drive the subflows with an event directly.

dolphin test samples/odoo-pipeline-sync --env-file samples/.env.test

On this page