{
  "id": "fix-4236-owner-echo-persistence",
  "title": "Post-create Lead owner transition keeps the HubSpot owner shadow, provider echo, and field audit coherent",
  "kind": "hubspot_sync",
  "executionMode": "serial",
  "executionModeReason": "Creates a unique connector lane, then changes one Lead owner after the inbound create has settled. Serial execution keeps the exact old/new audit assertion isolated and avoids concurrent owner writeback load.",
  "caseTimeoutMs": 1020000,
  "linkedIssues": [
    4236
  ],
  "provenance": {
    "businessScenario": "FIX-4236 - HubSpot_Owner_Id__c can be silently cleared after a Flow update and later re-applied by an unrelated update. Verify a post-create owner transition preserves a single, auditable old-to-new shadow update and echoes its final bound owner to HubSpot.",
    "reportedBy": "jiwei2021",
    "capturedAt": "2026-07-23",
    "calibrated": "Audit v2 emits field diffs for updates, not initial creation. This case settles an inbound Lead, resolves its assigned active CRM User's unique bound HubSpot owner, and proves that initial shadow and provider owner before one post-create owner PATCH. The provider Contact starts at sandbox-validated owner 44888233; assignment may choose a different owner, so its binding is read by the assigned CRM principal rather than pinned to the preset provider owner. The separately bound target remains sandbox-validated owner 6036357 and must differ from both the preset and assigned owners. PASS requires exactly one actual old-to-new HubSpot_Owner_Id__c audit row, a succeeded durable outbound command for the Lead and exact remote Contact, matching final Lead owner/shadow, and matching HubSpot Contact owner. Entrypoint must be present: the API PATCH is the root entrypoint while before-save Flow calculates the shadow. Missing country metadata, an inactive assigned User, missing/ambiguous bindings, or a non-distinct transition is BLOCKED, never PASS; missing shadow/provider/audit evidence is FAIL. The 1,020-second outer timeout exceeds the 930-second sequential poll budget. This remains a required full-suite case."
  },
  "steps": [
    {
      "id": "dict",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Name FROM Country_Dictionary__c WHERE Full_Name__c='Germany' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 30000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records[0].Name",
          "exists": true
        }
      ]
    },
    {
      "id": "region",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Region_Name__c FROM Countries_Region__c WHERE Name='{{steps.dict.response.body.records[0].Name}}' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 30000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records[0].Region_Name__c",
          "exists": true
        }
      ]
    },
    {
      "id": "create-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "fix4236-{{__runId}}@example.com",
          "firstname": "Fix4236",
          "lastname": "OwnerTransition{{__runId}}",
          "country_picklist__sfdc_": "Germany",
          "hubspot_owner_id": "44888233"
        }
      },
      "expectStatusRange": [
        200,
        299
      ],
      "assertions": [
        {
          "path": "response.body.id",
          "exists": true
        }
      ]
    },
    {
      "id": "wait-assigned-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,Country,Region__c,OwnerId FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 240000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.create-contact.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\.local)"
        }
      },
      "assertions": [
        {
          "path": "response.body.records[0].Country",
          "equals": "Germany"
        },
        {
          "path": "response.body.records[0].Region__c",
          "equals": "{{steps.region.response.body.records[0].Region_Name__c}}"
        },
        {
          "path": "response.body.records[0].OwnerId",
          "exists": true
        },
        {
          "path": "response.body.records[0].OwnerId",
          "notEquals": "system"
        }
      ]
    },
    {
      "id": "assigned-owner-user",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,IsActive FROM User WHERE Id='{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}' AND IsActive=true LIMIT 2",
      "expectStatus": 200,
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].Id",
          "equals": "{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}"
        },
        {
          "path": "response.body.records[0].IsActive",
          "equals": true
        }
      ]
    },
    {
      "id": "initial-owner-binding",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT CRMPrincipalId__c,HubSpotOwnerId__c FROM HubSpotOwnerBinding__c WHERE CRMPrincipalId__c='{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}' AND BindingStatus__c='bound' AND CRMPrincipalType__c='User' LIMIT 2",
      "expectStatus": 200,
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].CRMPrincipalId__c",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpotOwnerId__c",
          "matches": "^[1-9][0-9]*$"
        }
      ]
    },
    {
      "id": "assert-initial-owner-shadow",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,OwnerId,HubSpot_Owner_Id__c FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 120000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].OwnerId",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpot_Owner_Id__c",
          "equals": "{{steps.initial-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        }
      ]
    },
    {
      "id": "assert-initial-hubspot-owner",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.create-contact.response.body.id}}?properties=hubspot_owner_id",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 90000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.properties.hubspot_owner_id",
          "equals": "{{steps.initial-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        }
      ]
    },
    {
      "id": "post-create-owner-binding",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT CRMPrincipalId__c,HubSpotOwnerId__c FROM HubSpotOwnerBinding__c WHERE BindingStatus__c='bound' AND CRMPrincipalType__c='User' AND CRMPrincipalId__c!='{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}' AND HubSpotOwnerId__c='6036357' LIMIT 2",
      "expectStatus": 200,
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].CRMPrincipalId__c",
          "exists": true
        },
        {
          "path": "response.body.records[0].HubSpotOwnerId__c",
          "equals": "6036357"
        },
        {
          "path": "response.body.records[0].CRMPrincipalId__c",
          "notEquals": "{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}"
        },
        {
          "path": "response.body.records[0].HubSpotOwnerId__c",
          "notEquals": "{{steps.initial-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        }
      ]
    },
    {
      "id": "change-lead-owner-post-create",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "PATCH",
      "path": "/services/data/v63.0/sobjects/Lead/{{steps.wait-assigned-lead.response.body.records[0].Id}}",
      "body": {
        "OwnerId": "{{steps.post-create-owner-binding.response.body.records[0].CRMPrincipalId__c}}"
      },
      "expectStatus": 204
    },
    {
      "id": "assert-post-create-owner-shadow",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT OwnerId,HubSpot_Owner_Id__c FROM Lead WHERE Id='{{steps.wait-assigned-lead.response.body.records[0].Id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 120000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].OwnerId",
          "equals": "{{steps.post-create-owner-binding.response.body.records[0].CRMPrincipalId__c}}"
        },
        {
          "path": "response.body.records[0].HubSpot_Owner_Id__c",
          "equals": "{{steps.post-create-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        }
      ]
    },
    {
      "id": "assert-post-create-owner-command-finalized",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT State, ResponseStatus, RemoteId, CASE WHEN FailureCode IS NULL OR FailureCode = '' THEN 1 ELSE 0 END AS FailureCodeAbsent FROM `_System_IntegrationOutboundCommand` WHERE SourceObject = 'Lead' AND SourceRecordId = '{{steps.wait-assigned-lead.response.body.records[0].Id}}' AND FlowActionName = 'PushLeadToHubSpotContact' ORDER BY CreatedDate DESC LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 90000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].State",
          "equals": "succeeded"
        },
        {
          "path": "response.body.records[0].ResponseStatus",
          "numericEquals": 200
        },
        {
          "path": "response.body.records[0].RemoteId",
          "equals": "{{steps.create-contact.response.body.id}}"
        },
        {
          "path": "response.body.records[0].FailureCodeAbsent",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assert-post-create-hubspot-owner-writeback",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.create-contact.response.body.id}}?properties=hubspot_owner_id",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 90000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.properties.hubspot_owner_id",
          "equals": "{{steps.post-create-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        }
      ]
    },
    {
      "id": "assert-one-post-create-owner-shadow-audit",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT COUNT() FROM _System_AuditLog WHERE ObjectApiName='Lead' AND RecordId='{{steps.wait-assigned-lead.response.body.records[0].Id}}' AND FieldName='HubSpot_Owner_Id__c' AND OldValue='{{steps.initial-owner-binding.response.body.records[0].HubSpotOwnerId__c}}' AND NewValue='{{steps.post-create-owner-binding.response.body.records[0].HubSpotOwnerId__c}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 60000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.totalSize",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assert-post-create-owner-shadow-audit-entrypoint",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Entrypoint FROM _System_AuditLog WHERE ObjectApiName='Lead' AND RecordId='{{steps.wait-assigned-lead.response.body.records[0].Id}}' AND FieldName='HubSpot_Owner_Id__c' AND OldValue='{{steps.initial-owner-binding.response.body.records[0].HubSpotOwnerId__c}}' AND NewValue='{{steps.post-create-owner-binding.response.body.records[0].HubSpotOwnerId__c}}' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 60000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records",
          "lengthAtLeast": 1
        },
        {
          "path": "response.body.records[0].Entrypoint",
          "exists": true
        }
      ]
    }
  ]
}
