{
  "apiVersion": 63,
  "status": "Active",
  "label": "HubSpot Company \u2192 Account Upsert",
  "description": "Creates or updates a NexusCRM Account from a fetched HubSpot company record.",
  "runAtomically": true,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": {
      "targetReference": "FindAccountByHubSpotId"
    }
  },
  "variables": [
    {
      "name": "payload",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "record",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "accountId",
      "dataType": "String",
      "isOutput": true
    },
    {
      "name": "sourceID",
      "dataType": "String",
      "isInput": true
    }
  ],
  "recordLookups": [
    {
      "name": "FindAccountByHubSpotId",
      "label": "Find Account by HubSpot remote id",
      "locationX": 40,
      "locationY": 120,
      "object": "Account",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "sourceID"
          }
        }
      ],
      "outputReference": "$account",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "ChooseCreateOrUpdate"
      }
    },
    {
      "name": "FindAccountOwnerBinding",
      "label": "Find Account owner binding",
      "locationX": 220,
      "locationY": 720,
      "object": "HubSpotOwnerBinding__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpotOwnerId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "record.hubspot_owner_id"
          }
        },
        {
          "field": "BindingStatus__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "bound"
          }
        },
        {
          "field": "CRMPrincipalType__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "User"
          }
        }
      ],
      "outputReference": "$accountOwnerBinding",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "CheckAccountOwnerBinding"
      }
    },
    {
      "name": "FindAccountOwnerBindingDup",
      "label": "Find duplicate Account owner binding",
      "locationX": 400,
      "locationY": 840,
      "object": "HubSpotOwnerBinding__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpotOwnerId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "record.hubspot_owner_id"
          }
        },
        {
          "field": "BindingStatus__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "bound"
          }
        },
        {
          "field": "CRMPrincipalType__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "User"
          }
        },
        {
          "field": "Id",
          "operator": "NotEqualTo",
          "value": {
            "elementReference": "$accountOwnerBinding.Id"
          }
        }
      ],
      "outputReference": "$accountOwnerBindingDup",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "CheckAccountOwnerAmbiguous"
      }
    },
    {
      "name": "FindDefaultOwnerBinding",
      "label": "Find default owner User (hardcoded fallback owner)",
      "locationX": 240, "locationY": 300,
      "object": "User",
      "filterLogic": "and",
      "filters": [
        { "field": "Email", "operator": "EqualTo", "value": { "stringValue": "admin@example.invalid" } },
        { "field": "IsActive", "operator": "EqualTo", "value": true }
      ],
      "outputReference": "$defaultOwnerUser",
      "getFirstRecordOnly": true,
      "connector": { "targetReference": "GetDefaultAccountRecordType" }
    },
    {
      "name": "GetDefaultAccountRecordType",
      "label": "Get default Account record type",
      "locationX": 240,
      "locationY": 360,
      "object": "_System_RecordType",
      "filterLogic": "and",
      "filters": [
        {
          "field": "SobjectType",
          "operator": "EqualTo",
          "value": {
            "stringValue": "Account"
          }
        },
        {
          "field": "Name",
          "operator": "EqualTo",
          "value": {
            "stringValue": "Customer"
          }
        },
        {
          "field": "IsActive",
          "operator": "EqualTo",
          "value": {
            "booleanValue": true
          }
        }
      ],
      "outputReference": "$accountRecordType",
      "getFirstRecordOnly": true,
      "connector": { "targetReference": "CheckDefaultOwnerForShell" }
    },
    {
      "name": "FindPendingContactCompanyAssociationsForAccount",
      "label": "Find pending contact-company associations for this HubSpot company",
      "locationX": 40,
      "locationY": 720,
      "object": "HubSpotPendingContactCompanyAssociation__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "CompanyHubSpotId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "sourceID"
          }
        }
      ],
      "outputReference": "$pendingContactCompanyAssociations",
      "getFirstRecordOnly": false,
      "assignNullValuesIfNoRecordsFound": true,
      "connector": {
        "targetReference": "LoopPendingContactCompanyAssociations"
      }
    },
    {
      "name": "FindLeadForPendingContactCompanyAssociation",
      "label": "Find Lead for pending contact-company association",
      "locationX": 40,
      "locationY": 960,
      "object": "Lead",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$pendingContactCompanyAssociation.ContactHubSpotId__c"
          }
        }
      ],
      "outputReference": "$pendingAssociationLead",
      "getFirstRecordOnly": true,
      "assignNullValuesIfNoRecordsFound": true,
      "connector": {
        "targetReference": "CheckPendingContactCompanyAssociationLead"
      }
    }
  ],
  "decisions": [
    {
      "name": "ChooseCreateOrUpdate",
      "label": "Create when no matching Account exists",
      "locationX": 220,
      "locationY": 240,
      "rules": [
        {
          "name": "AccountMissing",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$account.Id",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "FindDefaultOwnerBinding"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "ApplyMappingsToExistingAccount"
      }
    },
    {
      "name": "CheckAccountOwnerBinding",
      "label": "Apply bound Account owner?",
      "locationX": 220,
      "locationY": 840,
      "rules": [
        {
          "name": "BoundAccountOwnerFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$accountOwnerBinding.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "FindAccountOwnerBindingDup"
          }
        }
      ]
    },
    {
      "name": "CheckAccountOwnerAmbiguous",
      "label": "Duplicate Account owner binding found?",
      "locationX": 400,
      "locationY": 960,
      "rules": [
        {
          "name": "AccountOwnerDupFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$accountOwnerBindingDup.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "FailAmbiguousAccountOwner"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "ApplyBoundOwnerToAccount"
      }
    },
    {
      "name": "CheckDefaultOwnerForShell",
      "label": "Own the new Account shell with the Integration User when a default owner binding exists?",
      "locationX": 240, "locationY": 330,
      "rules": [
        { "name": "DefaultOwnerConfigured", "conditionLogic": "and",
          "conditions": [ { "leftValueReference": "$defaultOwnerUser.Id", "operator": "IsNotBlank" } ],
          "connector": { "targetReference": "CreateAccountShellWithIntegrationOwner" } }
      ],
      "defaultConnector": { "targetReference": "CreateAccountShell" }
    },
    {
      "name": "CheckPendingContactCompanyAssociationLead",
      "label": "Apply pending contact-company association to a resolved Lead?",
      "locationX": 220,
      "locationY": 1080,
      "rules": [
        {
          "name": "PendingLeadFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$pendingAssociationLead.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "ApplyAccountToPendingLead"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "LoopPendingContactCompanyAssociations"
      }
    }
  ],
  "recordCreates": [
    {
      "name": "CreateAccountShell",
      "label": "Create Account shell",
      "locationX": 40,
      "locationY": 360,
      "object": "Account",
      "inputAssignments": [
        {
          "field": "Name",
          "value": {
            "elementReference": "record.name"
          }
        },
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "sourceID"
          }
        },
        {
          "field": "HubSpot_Sync_Enabled__c",
          "value": true
        },
        {
          "field": "Industry",
          "value": {
            "stringValue": "Others"
          }
        },
        {
          "field": "NumberOfEmployees",
          "value": {
            "elementReference": "effectiveEmployeeCount"
          }
        },
        {
          "field": "RecordTypeId",
          "value": {
            "elementReference": "$accountRecordType.Id"
          }
        },
        {
          "field": "Region__c",
          "value": {
            "stringValue": "NA"
          }
        },
        {
          "field": "Vertical__c",
          "value": {
            "stringValue": "Others"
          }
        },
        {
          "field": "Website",
          "value": {
            "elementReference": "effectiveWebsite"
          }
        }
      ],
      "assignRecordIdToReference": "accountId",
      "connector": {
        "targetReference": "ApplyMappingsToCreatedAccount"
      }
    },
    {
      "name": "CreateAccountShellWithIntegrationOwner",
      "label": "Create Account shell owned by the Integration User",
      "locationX": 240,
      "locationY": 360,
      "object": "Account",
      "inputAssignments": [
        {
          "field": "Name",
          "value": {
            "elementReference": "record.name"
          }
        },
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "sourceID"
          }
        },
        {
          "field": "HubSpot_Sync_Enabled__c",
          "value": true
        },
        {
          "field": "Industry",
          "value": {
            "stringValue": "Others"
          }
        },
        {
          "field": "NumberOfEmployees",
          "value": {
            "elementReference": "effectiveEmployeeCount"
          }
        },
        {
          "field": "RecordTypeId",
          "value": {
            "elementReference": "$accountRecordType.Id"
          }
        },
        {
          "field": "Region__c",
          "value": {
            "stringValue": "NA"
          }
        },
        {
          "field": "Vertical__c",
          "value": {
            "stringValue": "Others"
          }
        },
        {
          "field": "Website",
          "value": {
            "elementReference": "effectiveWebsite"
          }
        },
        {
          "field": "OwnerId",
          "value": {
            "elementReference": "$defaultOwnerUser.Id"
          }
        }
      ],
      "assignRecordIdToReference": "accountId",
      "connector": {
        "targetReference": "ApplyMappingsToCreatedAccount"
      }
    }
  ],
  "recordUpdates": [
    {
      "name": "StampRemoteIdOnExistingAccount",
      "label": "Stamp HubSpot remote id on existing Account",
      "locationX": 400,
      "locationY": 480,
      "object": "Account",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$account.Id"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "sourceID"
          }
        }
      ],
      "connector": {
        "targetReference": "ExposeMatchedAccountId"
      }
    },
    {
      "name": "ApplyBoundOwnerToAccount",
      "label": "Apply bound owner to Account",
      "locationX": 220,
      "locationY": 960,
      "object": "Account",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "accountId"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "OwnerId",
          "value": {
            "elementReference": "$accountOwnerBinding.CRMPrincipalId__c"
          }
        }
      ]
    },
    {
      "name": "ApplyAccountToPendingLead",
      "label": "Apply current Account to pending Lead association",
      "locationX": 40,
      "locationY": 1200,
      "object": "Lead",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$pendingAssociationLead.Id"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "BN_Account__c",
          "value": {
            "elementReference": "accountId"
          }
        }
      ],
      "connector": {
        "targetReference": "DeletePendingContactCompanyAssociationForAccount"
      }
    }
  ],
  "recordDeletes": [
    {
      "name": "DeletePendingContactCompanyAssociationForAccount",
      "label": "Delete pending contact-company association after Account backfill",
      "locationX": 40,
      "locationY": 1320,
      "object": "HubSpotPendingContactCompanyAssociation__c",
      "inputReference": "$pendingContactCompanyAssociation",
      "connector": {
        "targetReference": "LoopPendingContactCompanyAssociations"
      }
    }
  ],
  "actionCalls": [
    {
      "name": "ApplyMappingsToExistingAccount",
      "label": "Apply HubSpot mappings to existing Account",
      "locationX": 400,
      "locationY": 360,
      "actionName": "applyConnectorFieldMappings",
      "actionType": "applyConnectorFieldMappings",
      "inputParameters": [
        {
          "name": "source",
          "value": {
            "stringValue": "pingcap-hubspot-companion"
          }
        },
        {
          "name": "sourceObject",
          "value": {
            "stringValue": "companies"
          }
        },
        {
          "name": "targetObject",
          "value": {
            "stringValue": "Account"
          }
        },
        {
          "name": "actionType",
          "value": {
            "stringValue": "hubspotCompanyAccountInbound"
          }
        },
        {
          "name": "direction",
          "value": {
            "stringValue": "inbound"
          }
        },
        {
          "name": "targetId",
          "value": {
            "elementReference": "$account.Id"
          }
        },
        {
          "name": "sourceRecord",
          "value": {
            "elementReference": "record"
          }
        }
      ],
      "connector": {
        "targetReference": "StampRemoteIdOnExistingAccount"
      }
    },
    {
      "name": "ApplyMappingsToCreatedAccount",
      "label": "Apply HubSpot mappings to created Account",
      "locationX": 40,
      "locationY": 480,
      "actionName": "applyConnectorFieldMappings",
      "actionType": "applyConnectorFieldMappings",
      "inputParameters": [
        {
          "name": "source",
          "value": {
            "stringValue": "pingcap-hubspot-companion"
          }
        },
        {
          "name": "sourceObject",
          "value": {
            "stringValue": "companies"
          }
        },
        {
          "name": "targetObject",
          "value": {
            "stringValue": "Account"
          }
        },
        {
          "name": "actionType",
          "value": {
            "stringValue": "hubspotCompanyAccountInbound"
          }
        },
        {
          "name": "direction",
          "value": {
            "stringValue": "inbound"
          }
        },
        {
          "name": "targetId",
          "value": {
            "elementReference": "accountId"
          }
        },
        {
          "name": "sourceRecord",
          "value": {
            "elementReference": "record"
          }
        }
      ],
      "connector": {
        "targetReference": "RegisterCreatedAccountAuthorship"
      }
    },
    {
      "name": "RegisterCreatedAccountAuthorship",
      "label": "Finalize inbound remote identity (created)",
      "locationX": 40,
      "locationY": 600,
      "actionName": "finalizeConnectorInboundRemoteIdentity",
      "actionType": "finalizeConnectorInboundRemoteIdentity",
      "inputParameters": [
        {
          "name": "targetFlowDeveloperName",
          "value": {
            "stringValue": "HubSpot_Account_Company_Outbound_Update"
          }
        },
        {
          "name": "targetFlowActionName",
          "value": {
            "stringValue": "PushAccountToHubSpotCompany"
          }
        }
      ],
      "connector": {
        "targetReference": "FindPendingContactCompanyAssociationsForAccount"
      }
    },
    {
      "name": "RegisterExistingAccountAuthorship",
      "label": "Finalize inbound remote identity (existing)",
      "locationX": 400,
      "locationY": 600,
      "actionName": "finalizeConnectorInboundRemoteIdentity",
      "actionType": "finalizeConnectorInboundRemoteIdentity",
      "inputParameters": [
        {
          "name": "targetFlowDeveloperName",
          "value": {
            "stringValue": "HubSpot_Account_Company_Outbound_Update"
          }
        },
        {
          "name": "targetFlowActionName",
          "value": {
            "stringValue": "PushAccountToHubSpotCompany"
          }
        }
      ],
      "connector": {
        "targetReference": "FindPendingContactCompanyAssociationsForAccount"
      }
    }
  ],
  "loops": [
    {
      "name": "LoopPendingContactCompanyAssociations",
      "label": "Loop pending contact-company associations for this company",
      "locationX": 40,
      "locationY": 840,
      "collectionReference": "$pendingContactCompanyAssociations",
      "assignNextValueToReference": "$pendingContactCompanyAssociation",
      "nextValueConnector": {
        "targetReference": "FindLeadForPendingContactCompanyAssociation"
      },
      "noMoreValuesConnector": {
        "targetReference": "FindAccountOwnerBinding"
      }
    }
  ],
  "formulas": [
    {
      "name": "effectiveEmployeeCount",
      "dataType": "Number",
      "expression": "BLANKVALUE(record.numberofemployees, 0)"
    },
    {
      "name": "effectiveWebsite",
      "dataType": "String",
      "expression": "BLANKVALUE(record.website, \"https://hubspot.invalid/company/\" & sourceID)"
    }
  ],
  "assignments": [
    {
      "name": "ExposeMatchedAccountId",
      "label": "Expose matched Account Id",
      "locationX": 400,
      "locationY": 600,
      "assignmentItems": [
        {
          "assignToReference": "accountId",
          "operator": "Assign",
          "value": {
            "elementReference": "$account.Id"
          }
        }
      ],
      "connector": {
        "targetReference": "RegisterExistingAccountAuthorship"
      }
    }
  ],
  "customErrors": [
    {
      "name": "FailAmbiguousAccountOwner",
      "label": "Ambiguous Account owner binding",
      "locationX": 580,
      "locationY": 960,
      "customErrorMessages": [
        {
          "errorMessage": "HubSpot owner id resolves to multiple active Account owner bindings; refusing to assign an arbitrary owner",
          "statusCode": "HUBSPOT_OWNER_BINDING_AMBIGUOUS_ACCOUNT"
        }
      ]
    }
  ]
}
