{
  "apiVersion": 63,
  "status": "Active",
  "label": "HubSpot Company Merge Reconcile",
  "description": "Reconciles HubSpot company merge aliases into one canonical NexusCRM Account by preserving one survivor, deleting merged-away aliases, and rekeying the survivor to the canonical HubSpot id.",
  "runAtomically": true,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": {
      "targetReference": "FindAccountByCanonicalHubSpotId"
    }
  },
  "variables": [
    {
      "name": "payload",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "record",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "sourceID",
      "dataType": "String",
      "isInput": true
    },
    {
      "name": "survivorAccountId",
      "dataType": "String"
    },
    {
      "name": "currentMergedHubSpotId",
      "dataType": "String"
    }
  ],
  "recordLookups": [
    {
      "name": "FindAccountByCanonicalHubSpotId",
      "label": "Find Account already bound to the canonical HubSpot id",
      "locationX": 40,
      "locationY": 120,
      "object": "Account",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "effectiveCanonicalHubSpotId"
          }
        }
      ],
      "outputReference": "$canonicalAccount",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "ChooseCanonicalAccount"
      }
    },
    {
      "name": "FindAccountByPrimaryHubSpotId",
      "label": "Find Account still bound to the merge-winner legacy HubSpot id",
      "locationX": 320,
      "locationY": 120,
      "object": "Account",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "effectivePrimaryHubSpotId"
          }
        }
      ],
      "outputReference": "$primaryAccount",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "HandlePrimaryAccount"
      }
    },
    {
      "name": "FindMergedAccountByHubSpotId",
      "label": "Find Account by merged-away HubSpot id",
      "locationX": 600,
      "locationY": 360,
      "object": "Account",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "currentMergedHubSpotId"
          }
        }
      ],
      "outputReference": "$mergedAccount",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "HandleMergedAccount"
      }
    }
  ],
  "decisions": [
    {
      "name": "ChooseCanonicalAccount",
      "label": "Preserve an existing canonical Account when one is already re-keyed",
      "locationX": 180,
      "locationY": 240,
      "rules": [
        {
          "name": "CanonicalAccountFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$canonicalAccount.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "UseCanonicalAccountAsSurvivor"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "FindAccountByPrimaryHubSpotId"
      }
    },
    {
      "name": "HandlePrimaryAccount",
      "label": "Keep, delete, or skip the primary legacy id mapping",
      "locationX": 460,
      "locationY": 240,
      "rules": [
        {
          "name": "PrimaryAccountMissing",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$primaryAccount.Id",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "LoopMergedObjectIds"
          }
        },
        {
          "name": "PrimaryAccountBecomesSurvivor",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$primaryAccount.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "survivorAccountId",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "UsePrimaryAccountAsSurvivor"
          }
        },
        {
          "name": "PrimaryAccountAlreadySurvivor",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$primaryAccount.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$primaryAccount.Id",
              "operator": "EqualTo",
              "rightValue": {
                "elementReference": "survivorAccountId"
              }
            }
          ],
          "connector": {
            "targetReference": "LoopMergedObjectIds"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "DeletePrimaryAccountAlias"
      }
    },
    {
      "name": "HandleMergedAccount",
      "label": "Delete merged-away aliases after selecting a survivor",
      "locationX": 760,
      "locationY": 480,
      "rules": [
        {
          "name": "MergedAccountMissing",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$mergedAccount.Id",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "LoopMergedObjectIds"
          }
        },
        {
          "name": "MergedAccountBecomesSurvivor",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$mergedAccount.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "survivorAccountId",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "UseMergedAccountAsSurvivor"
          }
        },
        {
          "name": "MergedAccountAlreadySurvivor",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$mergedAccount.Id",
              "operator": "IsNotBlank"
            },
            {
              "leftValueReference": "$mergedAccount.Id",
              "operator": "EqualTo",
              "rightValue": {
                "elementReference": "survivorAccountId"
              }
            }
          ],
          "connector": {
            "targetReference": "LoopMergedObjectIds"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "DeleteMergedAccountAlias"
      }
    },
    {
      "name": "FinalizeMerge",
      "label": "Retry until a local survivor exists; otherwise re-key the survivor to the canonical id",
      "locationX": 760,
      "locationY": 720,
      "rules": [
        {
          "name": "NoLocalAccountYet",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "survivorAccountId",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "RetryUntilLocalAccountLands"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "RekeySurvivorAccountToCanonicalId"
      }
    }
  ],
  "formulas": [
    {
      "name": "effectiveCanonicalHubSpotId",
      "dataType": "String",
      "expression": "TEXT(BLANKVALUE(payload.newObjectId, BLANKVALUE(payload.primaryObjectId, sourceID)))"
    },
    {
      "name": "effectivePrimaryHubSpotId",
      "dataType": "String",
      "expression": "TEXT(BLANKVALUE(payload.primaryObjectId, sourceID))"
    }
  ],
  "assignments": [
    {
      "name": "UseCanonicalAccountAsSurvivor",
      "label": "Preserve canonical Account",
      "locationX": 180,
      "locationY": 360,
      "assignmentItems": [
        {
          "assignToReference": "survivorAccountId",
          "operator": "Assign",
          "value": {
            "elementReference": "$canonicalAccount.Id"
          }
        }
      ],
      "connector": {
        "targetReference": "FindAccountByPrimaryHubSpotId"
      }
    },
    {
      "name": "UsePrimaryAccountAsSurvivor",
      "label": "Preserve primary legacy Account",
      "locationX": 460,
      "locationY": 360,
      "assignmentItems": [
        {
          "assignToReference": "survivorAccountId",
          "operator": "Assign",
          "value": {
            "elementReference": "$primaryAccount.Id"
          }
        }
      ],
      "connector": {
        "targetReference": "LoopMergedObjectIds"
      }
    },
    {
      "name": "UseMergedAccountAsSurvivor",
      "label": "Preserve first merged-away alias when nothing else exists locally",
      "locationX": 760,
      "locationY": 600,
      "assignmentItems": [
        {
          "assignToReference": "survivorAccountId",
          "operator": "Assign",
          "value": {
            "elementReference": "$mergedAccount.Id"
          }
        }
      ],
      "connector": {
        "targetReference": "LoopMergedObjectIds"
      }
    }
  ],
  "loops": [
    {
      "name": "LoopMergedObjectIds",
      "label": "Loop merged-away HubSpot ids",
      "locationX": 600,
      "locationY": 240,
      "collectionReference": "payload.mergedObjectIds",
      "assignNextValueToReference": "currentMergedHubSpotId",
      "nextValueConnector": {
        "targetReference": "FindMergedAccountByHubSpotId"
      },
      "noMoreValuesConnector": {
        "targetReference": "FinalizeMerge"
      }
    }
  ],
  "recordUpdates": [
    {
      "name": "RekeySurvivorAccountToCanonicalId",
      "label": "Re-key survivor Account to the canonical HubSpot id",
      "locationX": 760,
      "locationY": 840,
      "object": "Account",
      "filterLogic": "and",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "survivorAccountId"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "effectiveCanonicalHubSpotId"
          }
        }
      ]
    }
  ],
  "recordDeletes": [
    {
      "name": "DeletePrimaryAccountAlias",
      "label": "Delete primary legacy Account alias after merge",
      "locationX": 460,
      "locationY": 360,
      "object": "Account",
      "inputReference": "$primaryAccount",
      "connector": {
        "targetReference": "LoopMergedObjectIds"
      }
    },
    {
      "name": "DeleteMergedAccountAlias",
      "label": "Delete merged-away Account alias after merge",
      "locationX": 940,
      "locationY": 600,
      "object": "Account",
      "inputReference": "$mergedAccount",
      "connector": {
        "targetReference": "LoopMergedObjectIds"
      }
    }
  ],
  "actionCalls": [
    {
      "name": "RetryUntilLocalAccountLands",
      "label": "Retry until a local Account exists for the merged HubSpot company cluster",
      "locationX": 940,
      "locationY": 720,
      "actionName": "setRetriable",
      "actionType": "setRetriable",
      "inputParameters": [
        {
          "name": "reason",
          "value": {
            "stringValue": "company merge arrived before any local Account in the merged HubSpot id cluster existed; retrying until a survivor lands locally"
          }
        }
      ]
    }
  ]
}
