{
  "apiVersion": 63,
  "status": "Active",
  "label": "Relink Lead children to converted records",
  "description": "Re-points CampaignMember/Task child records from the original Lead to the converted Contact/Opportunity/Account. Invoked as a subflow from Std_ConvertLead. Runs atomically; inherits the parent's transaction.",
  "runAtomically": true,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": { "targetReference": "GuardContactPresent" }
  },
  "variables": [
    { "name": "leadId", "dataType": "String", "isInput": true },
    { "name": "contactId", "dataType": "String", "isInput": true },
    { "name": "opportunityId", "dataType": "String", "isInput": true },
    { "name": "accountId", "dataType": "String", "isInput": true },
    { "name": "whatAnchor", "dataType": "String" }
  ],
  "decisions": [
    {
      "name": "GuardContactPresent",
      "label": "Skip when no converted Contact to relink to",
      "locationX": 40,
      "locationY": 120,
      "rules": [
        {
          "name": "ContactReady",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "leadId", "operator": "IsNotBlank" },
            { "leftValueReference": "contactId", "operator": "IsNotBlank" }
          ],
          "connector": { "targetReference": "ResolveWhatAnchor" }
        }
      ]
    },
    {
      "name": "ResolveWhatAnchor",
      "label": "Choose Task.WhatId target: Opportunity if present, else Account",
      "locationX": 40,
      "locationY": 240,
      "rules": [
        {
          "name": "OpportunityWins",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "opportunityId", "operator": "IsNotBlank" }
          ],
          "connector": { "targetReference": "SetAnchorToOpportunity" }
        }
      ],
      "defaultConnector": { "targetReference": "SetAnchorToAccount" }
    }
  ],
  "assignments": [
    {
      "name": "SetAnchorToOpportunity",
      "label": "whatAnchor := opportunityId",
      "locationX": 40,
      "locationY": 360,
      "assignmentItems": [
        { "assignToReference": "whatAnchor", "operator": "Assign", "value": { "elementReference": "opportunityId" } }
      ],
      "connector": { "targetReference": "RelinkCampaignMember" }
    },
    {
      "name": "SetAnchorToAccount",
      "label": "whatAnchor := accountId",
      "locationX": 220,
      "locationY": 360,
      "assignmentItems": [
        { "assignToReference": "whatAnchor", "operator": "Assign", "value": { "elementReference": "accountId" } }
      ],
      "connector": { "targetReference": "RelinkCampaignMember" }
    }
  ],
  "recordUpdates": [
    {
      "name": "RelinkCampaignMember",
      "label": "Point CampaignMember.LeadId rows at the converted Contact",
      "locationX": 40,
      "locationY": 480,
      "object": "CampaignMember",
      "filterLogic": "and",
      "filters": [
        { "field": "LeadId", "operator": "EqualTo", "value": { "elementReference": "leadId" } }
      ],
      "inputAssignments": [
        { "field": "ContactId", "value": { "elementReference": "contactId" } },
        { "field": "LeadId", "value": null }
      ],
      "connector": { "targetReference": "RelinkOpenTasksWho" }
    },
    {
      "name": "RelinkOpenTasksWho",
      "label": "Repoint open Task.WhoId to the converted Contact",
      "locationX": 40,
      "locationY": 600,
      "object": "Task",
      "filterLogic": "and",
      "filters": [
        { "field": "WhoId", "operator": "EqualTo", "value": { "elementReference": "leadId" } },
        { "field": "IsClosed", "operator": "EqualTo", "value": false }
      ],
      "inputAssignments": [
        { "field": "WhoId", "value": { "elementReference": "contactId" } }
      ],
      "connector": { "targetReference": "RelinkOpenTasksWhat" }
    },
    {
      "name": "RelinkOpenTasksWhat",
      "label": "Repoint open Task.WhatId to Opportunity (or Account fallback)",
      "locationX": 40,
      "locationY": 720,
      "object": "Task",
      "filterLogic": "and",
      "filters": [
        { "field": "WhatId", "operator": "EqualTo", "value": { "elementReference": "leadId" } },
        { "field": "IsClosed", "operator": "EqualTo", "value": false }
      ],
      "inputAssignments": [
        { "field": "WhatId", "value": { "elementReference": "whatAnchor" } }
      ]
    }
  ]
}
