{
  "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": "GetLeadCampaignMembers" }
    },
    {
      "name": "SetAnchorToAccount",
      "label": "whatAnchor := accountId",
      "locationX": 220,
      "locationY": 360,
      "assignmentItems": [
        { "assignToReference": "whatAnchor", "operator": "Assign", "value": { "elementReference": "accountId" } }
      ],
      "connector": { "targetReference": "GetLeadCampaignMembers" }
    }
  ],
  "recordLookups": [
    {
      "name": "GetLeadCampaignMembers",
      "label": "Get CampaignMember rows still linked to the converted Lead",
      "locationX": 40,
      "locationY": 480,
      "object": "CampaignMember",
      "filterLogic": "and",
      "filters": [
        { "field": "LeadId", "operator": "EqualTo", "value": { "elementReference": "leadId" } }
      ],
      "queriedFields": ["Id"],
      "getFirstRecordOnly": false,
      "outputReference": "$leadCampaignMembers",
      "connector": { "targetReference": "RelinkCampaignMembers" }
    }
  ],
  "recordUpdates": [
    {
      "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" } }
      ]
    }
  ],
  "loops": [
    {
      "name": "RelinkCampaignMembers",
      "label": "Relink each CampaignMember through the protected prepared-update path",
      "locationX": 40,
      "locationY": 600,
      "collectionReference": "$leadCampaignMembers",
      "assignNextValueToReference": "$currentLeadCampaignMember",
      "iterationOrder": "Asc",
      "nextValueConnector": { "targetReference": "PrepareCampaignMemberRelink" },
      "noMoreValuesConnector": { "targetReference": "RelinkOpenTasksWho" }
    }
  ],
  "actionCalls": [
    {
      "name": "PrepareCampaignMemberRelink",
      "label": "Prepare the standard-action-only CampaignMember parent switch",
      "locationX": 220,
      "locationY": 600,
      "actionName": "previewRecordUpdate",
      "actionType": "previewRecordUpdate",
      "inputParameters": [
        { "name": "object", "value": "CampaignMember" },
        { "name": "recordId", "value": { "elementReference": "$currentLeadCampaignMember.Id" } },
        {
          "name": "internalUpdates",
          "value": {
            "ContactId": { "elementReference": "contactId" },
            "LeadId": null
          }
        }
      ],
      "connector": { "targetReference": "CommitCampaignMemberRelink" }
    },
    {
      "name": "CommitCampaignMemberRelink",
      "label": "Commit the prepared CampaignMember parent switch",
      "locationX": 400,
      "locationY": 600,
      "actionName": "commitPreparedRecordUpdate",
      "actionType": "commitPreparedRecordUpdate",
      "inputParameters": [
        { "name": "object", "value": "CampaignMember" },
        { "name": "recordId", "value": { "elementReference": "$currentLeadCampaignMember.Id" } }
      ],
      "connector": { "targetReference": "RelinkCampaignMembers" }
    }
  ]
}
