{
  "apiVersion": 63,
  "status": "Active",
  "label": "HubSpot Contact owner shadow (before-save)",
  "description": "Stamps or clears HubSpot_Owner_Id__c on Contact before commit so the after-save push reads the correct value.",
  "start": {
    "locationX": 40,
    "locationY": 0,
    "object": "Contact",
    "triggerType": "RecordBeforeSave",
    "recordTriggerType": "CreateAndUpdate",
    "filterLogic": "1",
    "filters": [
      {
        "field": "HubSpot_Sync_Enabled__c",
        "operator": "EqualTo",
        "value": true
      }
    ],
    "connector": {
      "targetReference": "FindContactOwnerBindingOutbound"
    }
  },
  "recordLookups": [
    {
      "name": "FindContactOwnerBindingOutbound",
      "label": "Find owner binding for outbound",
      "locationX": 40,
      "locationY": 120,
      "object": "HubSpotOwnerBinding__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "CRMPrincipalId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$Record.OwnerId"
          }
        },
        {
          "field": "BindingStatus__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "bound"
          }
        },
        {
          "field": "CRMPrincipalType__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "User"
          }
        }
      ],
      "outputReference": "$contactOwnerBinding",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "FindContactOwnerBindingOutboundDup"
      }
    },
    {
      "name": "FindContactOwnerBindingOutboundDup",
      "label": "Find duplicate owner binding for outbound (ambiguity check)",
      "locationX": 40,
      "locationY": 160,
      "object": "HubSpotOwnerBinding__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "CRMPrincipalId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$Record.OwnerId"
          }
        },
        {
          "field": "BindingStatus__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "bound"
          }
        },
        {
          "field": "CRMPrincipalType__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "User"
          }
        },
        {
          "field": "Id",
          "operator": "NotEqualTo",
          "value": {
            "elementReference": "$contactOwnerBinding.Id"
          }
        }
      ],
      "outputReference": "$contactOwnerBindingDup",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "CheckContactOwnerOutboundAmbiguous"
      }
    }
  ],
  "decisions": [
    {
      "name": "CheckContactOwnerOutboundAmbiguous",
      "label": "Duplicate outbound binding found?",
      "locationX": 40,
      "locationY": 200,
      "rules": [
        {
          "name": "ContactOwnerOutboundDupFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$contactOwnerBindingDup.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "ClearContactOwnerShadowOutbound"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "ContactOwnerShadowDecision"
      }
    },
    {
      "name": "ContactOwnerShadowDecision",
      "label": "Owner shadow needs stamp?",
      "locationX": 40,
      "locationY": 240,
      "rules": [
        {
          "name": "BoundOwnerDiffersFromShadow",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$contactOwnerBinding.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$contactOwnerBinding.HubSpotOwnerId__c"
              }
            }
          ],
          "connector": {
            "targetReference": "StampContactOwnerShadowOutbound"
          }
        },
        {
          "name": "UnboundOwnerNeedsClear",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$contactOwnerBinding.Id",
              "operator": "IsBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "ClearContactOwnerShadowOutbound"
          }
        }
      ]
    }
  ],
  "recordUpdates": [
    {
      "name": "StampContactOwnerShadowOutbound",
      "label": "Stamp owner shadow id",
      "locationX": 40,
      "locationY": 360,
      "inputReference": "$Record",
      "inputAssignments": [
        {
          "field": "HubSpot_Owner_Id__c",
          "value": {
            "elementReference": "$contactOwnerBinding.HubSpotOwnerId__c"
          }
        }
      ]
    },
    {
      "name": "ClearContactOwnerShadowOutbound",
      "label": "Clear stale owner shadow (no bound binding)",
      "locationX": 220,
      "locationY": 360,
      "inputReference": "$Record",
      "inputAssignments": [
        {
          "field": "HubSpot_Owner_Id__c",
          "value": null
        }
      ]
    }
  ]
}
