{
  "apiVersion": "63.0",
  "status": "Active",
  "label": "HubSpot Contact-Company Association to Lead.BN_Account__c",
  "description": "Inbound association sync (#2521): when a HubSpot Contact<->Company association changes, stamp the local Lead's Account reference (BN_Account__c) from the associated Company's local Account. Resolves the Lead by the contact id (event fromObjectId -> sourceID) and the Account by the company id (event toObjectId). If either prerequisite is not local yet, persist a HubSpotPendingContactCompanyAssociation__c row, retry the event, and let the later Lead-side or Account-side inbound materialization path reconcile and delete that pending state. Non-company associations stay a no-op.",
  "runAtomically": true,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": {
      "targetReference": "FindLeadByHubSpotId"
    }
  },
  "variables": [
    {
      "name": "payload",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "record",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "sourceID",
      "dataType": "String",
      "isInput": true
    }
  ],
  "recordLookups": [
    {
      "name": "FindLeadByHubSpotId",
      "label": "Find Lead by HubSpot contact id",
      "locationX": 40,
      "locationY": 120,
      "object": "Lead",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "sourceID"
          }
        }
      ],
      "outputReference": "$lead",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "FindAccountByCompanyId"
      }
    },
    {
      "name": "FindAccountByCompanyId",
      "label": "Find Account by associated HubSpot company id",
      "locationX": 40,
      "locationY": 240,
      "object": "Account",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "payload.toObjectId"
          }
        }
      ],
      "outputReference": "$account",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "FindPendingAssociation"
      }
    },
    {
      "name": "FindPendingAssociation",
      "label": "Find pending contact-company association by HubSpot ids",
      "locationX": 40,
      "locationY": 360,
      "object": "HubSpotPendingContactCompanyAssociation__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "ContactHubSpotId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "sourceID"
          }
        },
        {
          "field": "CompanyHubSpotId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "payload.toObjectId"
          }
        }
      ],
      "outputReference": "$pendingAssociation",
      "getFirstRecordOnly": true,
      "assignNullValuesIfNoRecordsFound": true,
      "connector": {
        "targetReference": "StampAccountWhenBothResolved"
      }
    }
  ],
  "decisions": [
    {
      "name": "StampAccountWhenBothResolved",
      "label": "Apply or clear when both resolved; retry when a contact-company association arrives before its prerequisite records land",
      "locationX": 220,
      "locationY": 480,
      "rules": [
        {
          "name": "RemovalResolved",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$lead.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$account.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "payload.associationRemoved",
              "operator": "EqualTo",
              "rightValue": {
                "booleanValue": true
              }
            },
            {
              "leftValueReference": "$lead.BN_Account__c",
              "operator": "EqualTo",
              "rightValue": {
                "elementReference": "$account.Id"
              }
            }
          ],
          "connector": {
            "targetReference": "ClearAccountOnLead"
          }
        },
        {
          "name": "RemovalPendingOnly",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "payload.associationRemoved",
              "operator": "EqualTo",
              "rightValue": {
                "booleanValue": true
              }
            },
            {
              "leftValueReference": "$pendingAssociation.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "DeletePendingAssociation"
          }
        },
        {
          "name": "BothResolved",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$lead.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$account.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "payload.associationRemoved",
              "operator": "NotEqualTo",
              "rightValue": {
                "booleanValue": true
              }
            }
          ],
          "connector": {
            "targetReference": "StampAccountOnLead"
          }
        },
        {
          "name": "CompanyAssociationPrerequisiteNotYetLocal",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "payload.associationType",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "CONTACT_TO_COMPANY"
              }
            },
            {
              "leftValueReference": "payload.associationRemoved",
              "operator": "EqualTo",
              "rightValue": {
                "booleanValue": false
              }
            }
          ],
          "connector": {
            "targetReference": "CreatePendingAssociationIfMissing"
          }
        }
      ]
    },
    {
      "name": "CreatePendingAssociationIfMissing",
      "label": "Persist unresolved contact-company association before retrying",
      "locationX": 600,
      "locationY": 600,
      "rules": [
        {
          "name": "PendingAssociationMissing",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$pendingAssociation.Id",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "CreatePendingAssociation"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "RetryUntilPrerequisiteLands"
      }
    },
    {
      "name": "DeletePendingAssociationIfPresent",
      "label": "Delete pending association once the live link is reconciled",
      "locationX": 520,
      "locationY": 720,
      "rules": [
        {
          "name": "PendingAssociationFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$pendingAssociation.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "DeletePendingAssociation"
          }
        }
      ]
    }
  ],
  "recordUpdates": [
    {
      "name": "ClearAccountOnLead",
      "label": "Clear Lead.BN_Account__c for removed company association",
      "locationX": 280,
      "locationY": 480,
      "object": "Lead",
      "filterLogic": "and",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$lead.Id"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "BN_Account__c",
          "value": null
        }
      ],
      "connector": {
        "targetReference": "DeletePendingAssociationIfPresent"
      }
    },
    {
      "name": "StampAccountOnLead",
      "label": "Stamp Lead.BN_Account__c from associated company Account",
      "locationX": 400,
      "locationY": 480,
      "object": "Lead",
      "filterLogic": "and",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$lead.Id"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "BN_Account__c",
          "value": {
            "elementReference": "$account.Id"
          }
        }
      ],
      "connector": {
        "targetReference": "DeletePendingAssociationIfPresent"
      }
    }
  ],
  "recordDeletes": [
    {
      "name": "DeletePendingAssociation",
      "label": "Delete pending contact-company association",
      "locationX": 760,
      "locationY": 720,
      "object": "HubSpotPendingContactCompanyAssociation__c",
      "inputReference": "$pendingAssociation"
    }
  ],
  "recordCreates": [
    {
      "name": "CreatePendingAssociation",
      "label": "Persist pending contact-company association",
      "locationX": 760,
      "locationY": 600,
      "object": "HubSpotPendingContactCompanyAssociation__c",
      "inputAssignments": [
        {
          "field": "ContactHubSpotId__c",
          "value": {
            "elementReference": "sourceID"
          }
        },
        {
          "field": "CompanyHubSpotId__c",
          "value": {
            "elementReference": "payload.toObjectId"
          }
        }
      ],
      "connector": {
        "targetReference": "RetryUntilPrerequisiteLands"
      }
    }
  ],
  "actionCalls": [
    {
      "name": "RetryUntilPrerequisiteLands",
      "label": "Retry inbound event: contact-company association arrived before its Lead/Account create landed",
      "locationX": 600,
      "locationY": 480,
      "actionName": "setRetriable",
      "actionType": "setRetriable",
      "inputParameters": [
        {
          "name": "reason",
          "value": {
            "stringValue": "contact-company association processed before its prerequisite Lead/company Account exist locally; retrying until they land"
          }
        }
      ]
    }
  ]
}
