{
  "apiVersion": 63,
  "status": "Active",
  "label": "HubSpot Lead → Contact Outbound Update",
  "description": "Creates or PATCHes the linked HubSpot contact after a NexusCRM Lead save.",
  "runAtomically": false,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "triggerType": "RecordAfterSave",
    "recordTriggerType": "CreateAndUpdate",
    "object": "Lead",
    "filterLogic": "1 AND 2",
    "filters": [
      {
        "field": "HubSpot_Sync_Enabled__c",
        "operator": "EqualTo",
        "value": true
      },
      {
        "field": "IsConverted",
        "operator": "NotEqualTo",
        "value": true
      }
    ],
    "connector": {
      "targetReference": "SkipConnectorInboundEcho"
    }
  },
  "variables": [
    {
      "name": "outboundPayloadSourceFields",
      "dataType": "String"
    }
  ],
  "actionCalls": [
    {
      "name": "PushLeadToHubSpotContact",
      "label": "Push Lead to HubSpot Contact",
      "locationX": 40,
      "locationY": 160,
      "actionName": "PushToConnector",
      "actionType": "PushToConnector",
      "inputParameters": [
        {
          "name": "profileOwnerPack",
          "value": {
            "stringValue": "hubspot-crm-connector"
          }
        },
        {
          "name": "profileName",
          "value": {
            "stringValue": "hubspot-crm-contact-write"
          }
        },
        {
          "name": "sourceObject",
          "value": {
            "stringValue": "Lead"
          }
        },
        {
          "name": "targetObject",
          "value": {
            "stringValue": "contacts"
          }
        },
        {
          "name": "actionType",
          "value": {
            "stringValue": "PushToConnector"
          }
        },
        {
          "name": "bodyRoot",
          "value": {
            "stringValue": "properties"
          }
        },
        {
          "name": "destinationSlotId",
          "value": {
            "stringValue": "hubspot-contact"
          }
        },
        {
          "name": "remoteId",
          "value": {
            "elementReference": "$Record.HubSpot_Id__c"
          }
        },
        {
          "name": "updatePathTemplate",
          "value": {
            "stringValue": "/crm/v3/objects/contacts/{remoteId}"
          }
        },
        {
          "name": "sourceRecord",
          "value": {
            "elementReference": "$Record"
          }
        },
        {
          "name": "payloadSourceFields",
          "value": {
            "elementReference": "outboundPayloadSourceFields"
          }
        }
      ]
    }
  ],
  "assignments": [
    {
      "name": "SelectOwnerOnlyHubSpotPayload",
      "label": "Select owner-only HubSpot payload",
      "locationX": 220,
      "locationY": 160,
      "assignmentItems": [
        {
          "assignToReference": "outboundPayloadSourceFields",
          "operator": "Assign",
          "value": {
            "stringValue": "HubSpot_Owner_Id__c"
          }
        }
      ],
      "connector": {
        "targetReference": "PushLeadToHubSpotContact"
      }
    }
  ],
  "decisions": [
    {
      "name": "SkipConnectorInboundEcho",
      "label": "Skip connector inbound echo",
      "locationX": 40,
      "locationY": 80,
      "defaultConnector": {
        "targetReference": "PushLeadToHubSpotContact"
      },
      "defaultConnectorLabel": "User or non-HubSpot write",
      "rules": [
        {
          "name": "OwnerShadowCorrectedBeforeSave",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$SystemContext.ConnectorSyncOrigin",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "hubspot-crm-connector"
              }
            },
            {
              "leftValueReference": "$Record.HubSpot_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record__BeforeSave.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record__BeforeSave.Id",
              "operator": "EqualTo",
              "rightValue": {
                "elementReference": "$Record.Id"
              }
            },
            {
              "leftValueReference": "$Record__BeforeSave.HubSpot_Owner_Id__c",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record.HubSpot_Owner_Id__c"
              }
            }
          ],
          "connector": {
            "targetReference": "SelectOwnerOnlyHubSpotPayload"
          },
          "label": "Inbound owner shadow corrected before save"
        },
        {
          "name": "OwnerShadowChangedDuringInboundWithRequiredFieldShell",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$SystemContext.ConnectorSyncOrigin",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "hubspot-crm-connector"
              }
            },
            {
              "leftValueReference": "$Record.HubSpot_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record__Old.HubSpot_Owner_Id__c"
              }
            },
            {
              "leftValueReference": "$Record.LastName",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            },
            {
              "leftValueReference": "$Record.Company",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            }
          ],
          "connector": {
            "targetReference": "SelectOwnerOnlyHubSpotPayload"
          },
          "label": "Inbound-derived owner shadow changed with required-field shell"
        },
        {
          "name": "RequiredFieldShell",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$Record.LastName",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            }
          ],
          "label": "Incomplete required-field shell"
        },
        {
          "name": "OwnerShadowChangedDuringInbound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$SystemContext.ConnectorSyncOrigin",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "hubspot-crm-connector"
              }
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record__Prior.HubSpot_Owner_Id__c"
              }
            },
            {
              "leftValueReference": "$Record.LastName",
              "operator": "NotEqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            },
            {
              "leftValueReference": "$Record.Company",
              "operator": "NotEqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            }
          ],
          "connector": {
            "targetReference": "PushLeadToHubSpotContact"
          },
          "label": "Inbound-derived owner shadow changed"
        },
        {
          "name": "OwnerShadowChangedDuringInboundWithExistingCompanyShell",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$SystemContext.ConnectorSyncOrigin",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "hubspot-crm-connector"
              }
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record__Prior.HubSpot_Owner_Id__c"
              }
            },
            {
              "leftValueReference": "$Record.LastName",
              "operator": "NotEqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            },
            {
              "leftValueReference": "$Record.Company",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            },
            {
              "leftValueReference": "$Record.Company",
              "operator": "EqualTo",
              "rightValue": {
                "elementReference": "$Record__Prior.Company"
              }
            }
          ],
          "connector": {
            "targetReference": "SelectOwnerOnlyHubSpotPayload"
          },
          "label": "Inbound-derived owner shadow changed with existing company shell"
        },
        {
          "name": "OwnerShadowChangedDuringInboundWithNewCompanyShell",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$SystemContext.ConnectorSyncOrigin",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "hubspot-crm-connector"
              }
            },
            {
              "leftValueReference": "$Record.HubSpot_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Owner_Id__c",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record__Prior.HubSpot_Owner_Id__c"
              }
            },
            {
              "leftValueReference": "$Record.LastName",
              "operator": "NotEqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            },
            {
              "leftValueReference": "$Record.Company",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "HubSpot Lead"
              }
            },
            {
              "leftValueReference": "$Record__Prior.Id",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "SelectOwnerOnlyHubSpotPayload"
          },
          "label": "Inbound-derived owner shadow changed with new company shell"
        },
        {
          "name": "HubSpotConnectorInbound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$SystemContext.ConnectorSyncOrigin",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "hubspot-crm-connector"
              }
            }
          ],
          "label": "HubSpot connector inbound"
        },
        {
          "name": "LeadWithoutHubSpotId",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$Record.HubSpot_Id__c",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "PushLeadToHubSpotContact"
          },
          "label": "Lead has no HubSpot Contact id"
        },
        {
          "name": "LeadFirstNameChanged",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$Record__Prior.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.FirstName",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record__Prior.FirstName"
              }
            }
          ],
          "connector": {
            "targetReference": "PushLeadToHubSpotContact"
          },
          "label": "Lead FirstName changed"
        },
        {
          "name": "LeadLastNameChanged",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$Record__Prior.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.LastName",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record__Prior.LastName"
              }
            }
          ],
          "connector": {
            "targetReference": "PushLeadToHubSpotContact"
          },
          "label": "Lead LastName changed"
        },
        {
          "name": "LeadTitleChanged",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$Record__Prior.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.HubSpot_Id__c",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$Record.Title",
              "operator": "NotEqualTo",
              "rightValue": {
                "elementReference": "$Record__Prior.Title"
              }
            }
          ],
          "connector": {
            "targetReference": "PushLeadToHubSpotContact"
          },
          "label": "Lead Title changed"
        }
      ]
    }
  ]
}
