{
  "apiVersion": 63,
  "status": "Active",
  "label": "Sync OpportunityContactRole",
  "description": "For a given Opportunity + Contact pair, upserts the OpportunityContactRole row as IsPrimary=true and demotes any other primary OCR on the same opportunity. Runs atomically — invoked as a subflow from Std_ConvertLead.",
  "runAtomically": true,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": { "targetReference": "GuardBothIdsPresent" }
  },
  "variables": [
    { "name": "opportunityId", "dataType": "String", "isInput": true },
    { "name": "contactId", "dataType": "String", "isInput": true }
  ],
  "decisions": [
    {
      "name": "GuardBothIdsPresent",
      "label": "Skip when either Id is blank",
      "locationX": 40,
      "locationY": 120,
      "rules": [
        {
          "name": "BothPresent",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "opportunityId", "operator": "IsNotBlank" },
            { "leftValueReference": "contactId", "operator": "IsNotBlank" }
          ],
          "connector": { "targetReference": "GetExistingPair" }
        }
      ]
    },
    {
      "name": "CheckPairExists",
      "label": "Existing pair?",
      "locationX": 40,
      "locationY": 480,
      "rules": [
        {
          "name": "PairFound",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "$pair.Id", "operator": "IsNotBlank" }
          ],
          "connector": { "targetReference": "PromoteExistingPair" }
        }
      ],
      "defaultConnector": { "targetReference": "CreatePrimaryPair" }
    }
  ],
  "recordLookups": [
    {
      "name": "GetExistingPair",
      "label": "Lookup OCR for this Opportunity+Contact",
      "locationX": 40,
      "locationY": 240,
      "object": "OpportunityContactRole",
      "filterLogic": "and",
      "filters": [
        { "field": "OpportunityId", "operator": "EqualTo", "value": { "elementReference": "opportunityId" } },
        { "field": "ContactId", "operator": "EqualTo", "value": { "elementReference": "contactId" } }
      ],
      "outputReference": "$pair",
      "getFirstRecordOnly": true,
      "connector": { "targetReference": "DemoteOtherPrimaries" }
    }
  ],
  "recordUpdates": [
    {
      "name": "DemoteOtherPrimaries",
      "label": "Clear IsPrimary on other OCRs for this Opportunity",
      "locationX": 40,
      "locationY": 360,
      "object": "OpportunityContactRole",
      "filterLogic": "and",
      "filters": [
        { "field": "OpportunityId", "operator": "EqualTo", "value": { "elementReference": "opportunityId" } },
        { "field": "IsPrimary", "operator": "EqualTo", "value": true },
        { "field": "ContactId", "operator": "NotEqualTo", "value": { "elementReference": "contactId" } }
      ],
      "inputAssignments": [
        { "field": "IsPrimary", "value": false }
      ],
      "connector": { "targetReference": "CheckPairExists" }
    },
    {
      "name": "PromoteExistingPair",
      "label": "Mark existing pair primary",
      "locationX": 40,
      "locationY": 600,
      "object": "OpportunityContactRole",
      "inputReference": "$pair",
      "inputAssignments": [
        { "field": "IsPrimary", "value": true }
      ]
    }
  ],
  "recordCreates": [
    {
      "name": "CreatePrimaryPair",
      "label": "Create OCR as primary",
      "locationX": 220,
      "locationY": 600,
      "object": "OpportunityContactRole",
      "inputAssignments": [
        { "field": "OpportunityId", "value": { "elementReference": "opportunityId" } },
        { "field": "ContactId", "value": { "elementReference": "contactId" } },
        { "field": "IsPrimary", "value": true }
      ]
    }
  ]
}
