{
  "apiVersion": 64,
  "status": "Active",
  "label": "HubSpot Claim Salesforce Person Identity",
  "description": "Best-effort provider-scoped Salesforce identity claim after HubSpot remote identity is authoritative. Claim conflicts are quarantined and never roll back the primary HubSpot delivery.",
  "runAtomically": true,
  "formulas": [
    {
      "name": "leadSalesforceIdentityKey",
      "dataType": "String",
      "expression": "\"salesforce:\" & salesforceOrganizationId & \":Lead:\" & CASESAFEID(salesforceLeadId)"
    },
    {
      "name": "contactSalesforceIdentityKey",
      "dataType": "String",
      "expression": "\"salesforce:\" & salesforceOrganizationId & \":Contact:\" & CASESAFEID(salesforceContactId)"
    },
    {
      "name": "ambiguousSalesforceRecordIds",
      "dataType": "String",
      "expression": "salesforceLeadId & \"|\" & salesforceContactId"
    }
  ],
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": {
      "targetReference": "ValidateClaimEnvelope"
    }
  },
  "variables": [
    {
      "name": "portalId",
      "dataType": "String",
      "isInput": true
    },
    {
      "name": "hubspotRemoteId",
      "dataType": "String",
      "isInput": true
    },
    {
      "name": "salesforceLeadId",
      "dataType": "String",
      "isInput": true
    },
    {
      "name": "salesforceContactId",
      "dataType": "String",
      "isInput": true
    },
    {
      "name": "targetObject",
      "dataType": "String",
      "isInput": true
    },
    {
      "name": "targetRecordId",
      "dataType": "String",
      "isInput": true
    },
    {
      "name": "claimOutcome",
      "dataType": "String",
      "isOutput": true
    },
    {
      "name": "salesforceClaimObject",
      "dataType": "String"
    },
    {
      "name": "salesforceClaimRecordId",
      "dataType": "String"
    },
    {
      "name": "salesforceIdentityKey",
      "dataType": "String"
    },
    {
      "name": "salesforceOrganizationId",
      "dataType": "String"
    },
    {
      "name": "quarantineStatus",
      "dataType": "String"
    },
    {
      "name": "quarantineReason",
      "dataType": "String"
    }
  ],
  "recordLookups": [
    {
      "name": "FindActiveSalesforceIdentityBinding",
      "label": "Find admitted Salesforce organization for HubSpot portal",
      "locationX": 220,
      "locationY": 240,
      "object": "HubSpotSalesforceIdentityBinding__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "PortalId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "portalId"
          }
        },
        {
          "field": "BindingStatus__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "active"
          }
        }
      ],
      "outputReference": "$salesforceBinding",
      "getFirstRecordOnly": true,
      "assignNullValuesIfNoRecordsFound": true,
      "connector": {
        "targetReference": "CaptureSalesforceOrganizationId"
      }
    }
  ],
  "decisions": [
    {
      "name": "ValidateClaimEnvelope",
      "label": "Validate primary HubSpot envelope and Salesforce claim shape",
      "locationX": 40,
      "locationY": 60,
      "rules": [
        {
          "name": "PrimaryEnvelopeMissing",
          "conditionLogic": "or",
          "conditions": [
            {
              "leftValueReference": "portalId",
              "operator": "IsBlank"
            },
            {
              "leftValueReference": "hubspotRemoteId",
              "operator": "IsBlank"
            },
            {
              "leftValueReference": "targetObject",
              "operator": "IsBlank"
            },
            {
              "leftValueReference": "targetRecordId",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "CompleteWithoutClaim"
          }
        },
        {
          "name": "NoSalesforceClaim",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceLeadId",
              "operator": "IsBlank"
            },
            {
              "leftValueReference": "salesforceContactId",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "CompleteWithoutClaim"
          }
        },
        {
          "name": "AmbiguousSalesforceClaim",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceLeadId",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "salesforceContactId",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "PrepareAmbiguousQuarantine"
          }
        },
        {
          "name": "MalformedSalesforceLeadClaim",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceLeadId",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "salesforceLeadId",
              "operator": "DoesNotMatchRegex",
              "rightValue": "^00Q[A-Za-z0-9]{12}([A-Za-z0-9]{3})?$"
            }
          ],
          "connector": {
            "targetReference": "PrepareMalformedLeadQuarantine"
          }
        },
        {
          "name": "SalesforceLeadClaimAvailable",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceLeadId",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "PrepareLeadClaim"
          }
        },
        {
          "name": "MalformedSalesforceContactClaim",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceContactId",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "salesforceContactId",
              "operator": "DoesNotMatchRegex",
              "rightValue": "^003[A-Za-z0-9]{12}([A-Za-z0-9]{3})?$"
            }
          ],
          "connector": {
            "targetReference": "PrepareMalformedContactQuarantine"
          }
        },
        {
          "name": "SalesforceContactClaimAvailable",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceContactId",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "PrepareContactClaim"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "CompleteWithoutClaim"
      }
    },
    {
      "name": "CheckSalesforceIdentityBinding",
      "label": "Salesforce organization admitted for this portal?",
      "locationX": 220,
      "locationY": 300,
      "rules": [
        {
          "name": "ActiveBindingFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$salesforceBinding.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$salesforceBinding.SalesforceOrganizationId__c",
              "operator": "MatchesRegex",
              "rightValue": "^00D[A-Za-z0-9]{15}$"
            }
          ],
          "connector": {
            "targetReference": "PrepareAdmittedIdentityKey"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "PrepareUnadmittedQuarantine"
      }
    },
    {
      "name": "ChooseAdmittedIdentityKey",
      "label": "Choose provider-scoped identity key by Salesforce object",
      "locationX": 220,
      "locationY": 390,
      "rules": [
        {
          "name": "UseContactIdentityKey",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceClaimObject",
              "operator": "EqualTo",
              "rightValue": "Contact"
            }
          ],
          "connector": {
            "targetReference": "PrepareContactIdentityKey"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "RouteAdmittedClaim"
      }
    },
    {
      "name": "RouteAdmittedClaim",
      "label": "Claim only on the matching person object",
      "locationX": 220,
      "locationY": 420,
      "rules": [
        {
          "name": "ClaimLeadIdentity",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceClaimObject",
              "operator": "EqualTo",
              "rightValue": "Lead"
            },
            {
              "leftValueReference": "targetObject",
              "operator": "EqualTo",
              "rightValue": "Lead"
            }
          ],
          "connector": {
            "targetReference": "ClaimSalesforceIdentityOnLead"
          }
        },
        {
          "name": "ClaimContactIdentity",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "salesforceClaimObject",
              "operator": "EqualTo",
              "rightValue": "Contact"
            },
            {
              "leftValueReference": "targetObject",
              "operator": "EqualTo",
              "rightValue": "Contact"
            }
          ],
          "connector": {
            "targetReference": "ClaimSalesforceIdentityOnContact"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "PrepareObjectMismatchQuarantine"
      }
    }
  ],
  "assignments": [
    {
      "name": "CaptureSalesforceOrganizationId",
      "label": "Capture admitted Salesforce organization",
      "locationX": 220,
      "locationY": 300,
      "assignmentItems": [
        {
          "assignToReference": "salesforceOrganizationId",
          "operator": "Assign",
          "value": {
            "elementReference": "$salesforceBinding.SalesforceOrganizationId__c"
          }
        }
      ],
      "connector": {
        "targetReference": "CheckSalesforceIdentityBinding"
      }
    },
    {
      "name": "CompleteWithoutClaim",
      "label": "Complete without a secondary claim",
      "locationX": 40,
      "locationY": 720,
      "assignmentItems": [
        {
          "assignToReference": "claimOutcome",
          "operator": "Assign",
          "value": {
            "stringValue": "not_present"
          }
        }
      ]
    },
    {
      "name": "CompleteClaimed",
      "label": "Complete claimed",
      "locationX": 220,
      "locationY": 720,
      "assignmentItems": [
        {
          "assignToReference": "claimOutcome",
          "operator": "Assign",
          "value": {
            "stringValue": "claimed"
          }
        }
      ]
    },
    {
      "name": "CompleteQuarantined",
      "label": "Complete quarantined",
      "locationX": 400,
      "locationY": 720,
      "assignmentItems": [
        {
          "assignToReference": "claimOutcome",
          "operator": "Assign",
          "value": {
            "stringValue": "quarantined"
          }
        }
      ]
    },
    {
      "name": "PrepareLeadClaim",
      "label": "Prepare Lead claim",
      "locationX": 220,
      "locationY": 120,
      "assignmentItems": [
        {
          "assignToReference": "salesforceClaimObject",
          "operator": "Assign",
          "value": {
            "stringValue": "Lead"
          }
        },
        {
          "assignToReference": "salesforceClaimRecordId",
          "operator": "Assign",
          "value": {
            "elementReference": "salesforceLeadId"
          }
        }
      ],
      "connector": {
        "targetReference": "FindActiveSalesforceIdentityBinding"
      }
    },
    {
      "name": "PrepareContactClaim",
      "label": "Prepare Contact claim",
      "locationX": 400,
      "locationY": 120,
      "assignmentItems": [
        {
          "assignToReference": "salesforceClaimObject",
          "operator": "Assign",
          "value": {
            "stringValue": "Contact"
          }
        },
        {
          "assignToReference": "salesforceClaimRecordId",
          "operator": "Assign",
          "value": {
            "elementReference": "salesforceContactId"
          }
        }
      ],
      "connector": {
        "targetReference": "FindActiveSalesforceIdentityBinding"
      }
    },
    {
      "name": "PrepareAdmittedIdentityKey",
      "label": "Prepare admitted provider-scoped identity key",
      "locationX": 220,
      "locationY": 360,
      "assignmentItems": [
        {
          "assignToReference": "salesforceIdentityKey",
          "operator": "Assign",
          "value": {
            "elementReference": "leadSalesforceIdentityKey"
          }
        }
      ],
      "connector": {
        "targetReference": "ChooseAdmittedIdentityKey"
      }
    },
    {
      "name": "PrepareContactIdentityKey",
      "label": "Prepare admitted Contact identity key",
      "locationX": 400,
      "locationY": 390,
      "assignmentItems": [
        {
          "assignToReference": "salesforceIdentityKey",
          "operator": "Assign",
          "value": {
            "elementReference": "contactSalesforceIdentityKey"
          }
        }
      ],
      "connector": {
        "targetReference": "RouteAdmittedClaim"
      }
    },
    {
      "name": "PrepareAmbiguousQuarantine",
      "label": "Quarantine ambiguous Lead and Contact claim",
      "locationX": 580,
      "locationY": 120,
      "assignmentItems": [
        {
          "assignToReference": "salesforceClaimObject",
          "operator": "Assign",
          "value": {
            "stringValue": "Ambiguous"
          }
        },
        {
          "assignToReference": "salesforceClaimRecordId",
          "operator": "Assign",
          "value": {
            "elementReference": "ambiguousSalesforceRecordIds"
          }
        },
        {
          "assignToReference": "quarantineStatus",
          "operator": "Assign",
          "value": {
            "stringValue": "malformed"
          }
        },
        {
          "assignToReference": "quarantineReason",
          "operator": "Assign",
          "value": {
            "stringValue": "HubSpot payload carries both Salesforce Lead and Contact identities"
          }
        }
      ],
      "connector": {
        "targetReference": "CreateClaimQuarantine"
      }
    },
    {
      "name": "PrepareMalformedLeadQuarantine",
      "label": "Quarantine malformed Lead claim",
      "locationX": 580,
      "locationY": 180,
      "assignmentItems": [
        {
          "assignToReference": "salesforceClaimObject",
          "operator": "Assign",
          "value": {
            "stringValue": "Lead"
          }
        },
        {
          "assignToReference": "salesforceClaimRecordId",
          "operator": "Assign",
          "value": {
            "elementReference": "salesforceLeadId"
          }
        },
        {
          "assignToReference": "quarantineStatus",
          "operator": "Assign",
          "value": {
            "stringValue": "malformed"
          }
        },
        {
          "assignToReference": "quarantineReason",
          "operator": "Assign",
          "value": {
            "stringValue": "Salesforce Lead identity is not a canonical 00Q identifier shape"
          }
        }
      ],
      "connector": {
        "targetReference": "CreateClaimQuarantine"
      }
    },
    {
      "name": "PrepareMalformedContactQuarantine",
      "label": "Quarantine malformed Contact claim",
      "locationX": 580,
      "locationY": 240,
      "assignmentItems": [
        {
          "assignToReference": "salesforceClaimObject",
          "operator": "Assign",
          "value": {
            "stringValue": "Contact"
          }
        },
        {
          "assignToReference": "salesforceClaimRecordId",
          "operator": "Assign",
          "value": {
            "elementReference": "salesforceContactId"
          }
        },
        {
          "assignToReference": "quarantineStatus",
          "operator": "Assign",
          "value": {
            "stringValue": "malformed"
          }
        },
        {
          "assignToReference": "quarantineReason",
          "operator": "Assign",
          "value": {
            "stringValue": "Salesforce Contact identity is not a canonical 003 identifier shape"
          }
        }
      ],
      "connector": {
        "targetReference": "CreateClaimQuarantine"
      }
    },
    {
      "name": "PrepareUnadmittedQuarantine",
      "label": "Quarantine claim without exact portal admission",
      "locationX": 580,
      "locationY": 300,
      "assignmentItems": [
        {
          "assignToReference": "quarantineStatus",
          "operator": "Assign",
          "value": {
            "stringValue": "unadmitted"
          }
        },
        {
          "assignToReference": "quarantineReason",
          "operator": "Assign",
          "value": {
            "stringValue": "No active exact HubSpot portal to Salesforce Organization.Id admission exists"
          }
        }
      ],
      "connector": {
        "targetReference": "CreateClaimQuarantine"
      }
    },
    {
      "name": "PrepareObjectMismatchQuarantine",
      "label": "Quarantine claim whose object differs from HubSpot owner",
      "locationX": 580,
      "locationY": 420,
      "assignmentItems": [
        {
          "assignToReference": "quarantineStatus",
          "operator": "Assign",
          "value": {
            "stringValue": "object_mismatch"
          }
        },
        {
          "assignToReference": "quarantineReason",
          "operator": "Assign",
          "value": {
            "stringValue": "Salesforce identity object does not match the authoritative HubSpot owner object"
          }
        }
      ],
      "connector": {
        "targetReference": "CreateClaimQuarantine"
      }
    },
    {
      "name": "PrepareAlreadyOwnedQuarantine",
      "label": "Quarantine already-owned Salesforce identity",
      "locationX": 580,
      "locationY": 540,
      "assignmentItems": [
        {
          "assignToReference": "quarantineStatus",
          "operator": "Assign",
          "value": {
            "stringValue": "already_owned"
          }
        },
        {
          "assignToReference": "quarantineReason",
          "operator": "Assign",
          "value": {
            "stringValue": "Provider-scoped Salesforce identity is already owned by another CRM person"
          }
        }
      ],
      "connector": {
        "targetReference": "CreateClaimQuarantine"
      }
    },
    {
      "name": "PrepareClaimErrorQuarantine",
      "label": "Quarantine non-ownership claim error",
      "locationX": 760,
      "locationY": 540,
      "assignmentItems": [
        {
          "assignToReference": "quarantineStatus",
          "operator": "Assign",
          "value": {
            "stringValue": "claim_error"
          }
        },
        {
          "assignToReference": "quarantineReason",
          "operator": "Assign",
          "value": {
            "stringValue": "Salesforce identity claim failed for a non-ownership reason; primary HubSpot delivery remains authoritative"
          }
        }
      ],
      "connector": {
        "targetReference": "CreateClaimQuarantine"
      }
    }
  ],
  "recordUpdates": [],
  "recordCreates": [
    {
      "name": "ClaimSalesforceIdentityOnLead",
      "label": "Claim Salesforce identity on authoritative HubSpot Lead",
      "locationX": 220,
      "locationY": 480,
      "object": "Lead",
      "upsertByExternalIdField": "HubSpot_Id__c",
      "upsertAdoptionGuardFields": [
        "Salesforce_Identity_Key__c"
      ],
      "adoptionConflictConnector": {
        "targetReference": "PrepareAlreadyOwnedQuarantine"
      },
      "inputAssignments": [
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "hubspotRemoteId"
          }
        },
        {
          "field": "Salesforce_Identity_Key__c",
          "value": {
            "elementReference": "salesforceIdentityKey"
          }
        }
      ],
      "connector": {
        "targetReference": "CompleteClaimed"
      },
      "faultConnector": {
        "targetReference": "PrepareClaimErrorQuarantine"
      }
    },
    {
      "name": "ClaimSalesforceIdentityOnContact",
      "label": "Claim Salesforce identity on authoritative HubSpot Contact",
      "locationX": 400,
      "locationY": 480,
      "object": "Contact",
      "upsertByExternalIdField": "HubSpot_Id__c",
      "upsertAdoptionGuardFields": [
        "Salesforce_Identity_Key__c"
      ],
      "adoptionConflictConnector": {
        "targetReference": "PrepareAlreadyOwnedQuarantine"
      },
      "inputAssignments": [
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "hubspotRemoteId"
          }
        },
        {
          "field": "Salesforce_Identity_Key__c",
          "value": {
            "elementReference": "salesforceIdentityKey"
          }
        }
      ],
      "connector": {
        "targetReference": "CompleteClaimed"
      },
      "faultConnector": {
        "targetReference": "PrepareClaimErrorQuarantine"
      }
    },
    {
      "name": "CreateClaimQuarantine",
      "label": "Append non-blocking Salesforce claim quarantine",
      "locationX": 580,
      "locationY": 600,
      "object": "HubSpotSalesforceIdentityClaimQuarantine__c",
      "inputAssignments": [
        {
          "field": "PortalId__c",
          "value": {
            "elementReference": "portalId"
          }
        },
        {
          "field": "HubSpotRemoteId__c",
          "value": {
            "elementReference": "hubspotRemoteId"
          }
        },
        {
          "field": "SalesforceOrganizationId__c",
          "value": {
            "elementReference": "$salesforceBinding.SalesforceOrganizationId__c"
          }
        },
        {
          "field": "SalesforceObject__c",
          "value": {
            "elementReference": "salesforceClaimObject"
          }
        },
        {
          "field": "SalesforceRecordId__c",
          "value": {
            "elementReference": "salesforceClaimRecordId"
          }
        },
        {
          "field": "SalesforceIdentityKey__c",
          "value": {
            "elementReference": "salesforceIdentityKey"
          }
        },
        {
          "field": "TargetObject__c",
          "value": {
            "elementReference": "targetObject"
          }
        },
        {
          "field": "TargetRecordId__c",
          "value": {
            "elementReference": "targetRecordId"
          }
        },
        {
          "field": "Status__c",
          "value": {
            "elementReference": "quarantineStatus"
          }
        },
        {
          "field": "Reason__c",
          "value": {
            "elementReference": "quarantineReason"
          }
        },
        {
          "field": "Resolved__c",
          "value": false
        }
      ],
      "connector": {
        "targetReference": "CompleteQuarantined"
      }
    }
  ]
}
