{
  "apiVersion": "63.0",
  "status": "Active",
  "label": "HubSpot Deal-Company Association to Opportunity.AccountId",
  "description": "Inbound association sync (#2521): when a HubSpot Deal<->Company association changes, stamp the local Opportunity.AccountId from the associated Company's local Account. Resolves the Opportunity by the deal id (event fromObjectId -> sourceID) and the Account by the company id (event toObjectId); only stamps when both already exist locally, so a non-company association (toObjectId not matching any Account) is a no-op.",
  "runAtomically": true,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": {
      "targetReference": "FindOpportunityByHubSpotId"
    }
  },
  "variables": [
    {
      "name": "payload",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "record",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "sourceID",
      "dataType": "String",
      "isInput": true
    }
  ],
  "recordLookups": [
    {
      "name": "FindOpportunityByHubSpotId",
      "label": "Find Opportunity by HubSpot deal id",
      "locationX": 40,
      "locationY": 120,
      "object": "Opportunity",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "sourceID"
          }
        }
      ],
      "outputReference": "$opportunity",
      "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": "StampAccountWhenBothResolved"
      }
    }
  ],
  "decisions": [
    {
      "name": "StampAccountWhenBothResolved",
      "label": "Apply or clear when both resolved; retry when a deal-company association arrives before its prerequisite records land",
      "locationX": 220,
      "locationY": 360,
      "rules": [
        {
          "name": "RemovalResolved",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$opportunity.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$account.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "payload.associationRemoved",
              "operator": "EqualTo",
              "rightValue": {
                "booleanValue": true
              }
            },
            {
              "leftValueReference": "$opportunity.AccountId",
              "operator": "EqualTo",
              "rightValue": {
                "elementReference": "$account.Id"
              }
            }
          ],
          "connector": {
            "targetReference": "ClearAccountIdOnOpportunity"
          }
        },
        {
          "name": "BothResolved",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$opportunity.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$account.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "payload.associationRemoved",
              "operator": "NotEqualTo",
              "rightValue": {
                "booleanValue": true
              }
            }
          ],
          "connector": {
            "targetReference": "StampAccountIdOnOpportunity"
          }
        },
        {
          "name": "CompanyAssociationPrerequisiteNotYetLocal",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "payload.associationType",
              "operator": "EqualTo",
              "rightValue": {
                "stringValue": "DEAL_TO_COMPANY"
              }
            },
            {
              "leftValueReference": "payload.associationRemoved",
              "operator": "EqualTo",
              "rightValue": {
                "booleanValue": false
              }
            }
          ],
          "connector": {
            "targetReference": "RetryUntilPrerequisiteLands"
          }
        }
      ]
    }
  ],
  "recordUpdates": [
    {
      "name": "ClearAccountIdOnOpportunity",
      "label": "Clear Opportunity.AccountId for removed company association",
      "locationX": 280,
      "locationY": 480,
      "object": "Opportunity",
      "filterLogic": "and",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$opportunity.Id"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "AccountId",
          "value": null
        }
      ]
    },
    {
      "name": "StampAccountIdOnOpportunity",
      "label": "Stamp Opportunity.AccountId from associated company Account",
      "locationX": 400,
      "locationY": 480,
      "object": "Opportunity",
      "filterLogic": "and",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$opportunity.Id"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "AccountId",
          "value": {
            "elementReference": "$account.Id"
          }
        }
      ]
    }
  ],
  "actionCalls": [
    {
      "name": "RetryUntilPrerequisiteLands",
      "label": "Retry inbound event: deal-company association arrived before its Opportunity/Account create landed",
      "locationX": 600,
      "locationY": 480,
      "actionName": "setRetriable",
      "actionType": "setRetriable",
      "inputParameters": [
        {
          "name": "reason",
          "value": {
            "stringValue": "deal-company association processed before its prerequisite Opportunity/company Account exist locally; retrying until they land"
          }
        }
      ]
    }
  ]
}
