{
  "apiVersion": 60,
  "label": "Opportunity - Resync Credits Pre-Sales Share",
  "description": "Root fix (Option A) reassignment handler. After-save on Opportunity when Pre_Sales_Owner__c changes: for every Credits__c under the opp it deletes the stale _System_RecordShare (Reason='PreSalesAuto') pointing at the PREVIOUS pre-sales ($Record__Prior) and creates the share for the NEW pre-sales (idempotent). It does NOT write the Credits.Pre_Sales_Owner__c snapshot (that field is a locked create-time snapshot; an after-save write to it is not a supported path and is unnecessary — access is driven by the live Opportunity, read fresh in Credits_Share_With_Presales, so no stale snapshot can re-grant a former pre-sales user). Runs in trigger SystemContext.",
  "processType": "AutoLaunchedFlow",
  "status": "Active",
  "start": {
    "locationX": 176,
    "locationY": 0,
    "object": "Opportunity",
    "triggerType": "RecordAfterSave",
    "recordTriggerType": "Update",
    "connector": { "targetReference": "PreSales_Changed" }
  },
  "decisions": [
    {
      "name": "PreSales_Changed",
      "label": "Pre-Sales owner changed",
      "locationX": 176,
      "locationY": 120,
      "defaultConnectorLabel": "Unchanged",
      "rules": [
        {
          "name": "Changed",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "$Record.Pre_Sales_Owner__c", "operator": "NotEqualTo", "rightValue": { "elementReference": "$Record__Prior.Pre_Sales_Owner__c" } }
          ],
          "connector": { "targetReference": "Get_Credits_For_Opp" },
          "label": "Changed"
        }
      ]
    },
    {
      "name": "Old_Share_Exists",
      "label": "Stale share exists",
      "locationX": 300,
      "locationY": 480,
      "defaultConnectorLabel": "None",
      "defaultConnector": { "targetReference": "New_PreSales_Present" },
      "rules": [
        {
          "name": "Has_Old_Share",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "Get_Stale_Share", "operator": "IsNull", "rightValue": { "booleanValue": false } }
          ],
          "connector": { "targetReference": "Delete_Stale_Share" },
          "label": "Has old share"
        }
      ]
    },
    {
      "name": "New_PreSales_Present",
      "label": "New pre-sales is set",
      "locationX": 300,
      "locationY": 720,
      "defaultConnectorLabel": "Cleared - continue",
      "defaultConnector": { "targetReference": "Loop_Credits" },
      "rules": [
        {
          "name": "Has_New_PreSales",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "$Record.Pre_Sales_Owner__c", "operator": "IsNull", "rightValue": { "booleanValue": false } }
          ],
          "connector": { "targetReference": "Get_New_Share" },
          "label": "Has new pre-sales"
        }
      ]
    },
    {
      "name": "New_Share_Needed",
      "label": "New share missing",
      "locationX": 300,
      "locationY": 960,
      "defaultConnectorLabel": "Already shared - continue",
      "defaultConnector": { "targetReference": "Loop_Credits" },
      "rules": [
        {
          "name": "Create_New",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "Get_New_Share", "operator": "IsNull", "rightValue": { "booleanValue": true } },
            { "leftValueReference": "$currentCredit.OwnerId", "operator": "NotEqualTo", "rightValue": { "elementReference": "$Record.Pre_Sales_Owner__c" } }
          ],
          "connector": { "targetReference": "Create_New_Share" },
          "label": "Create new share"
        }
      ]
    }
  ],
  "recordLookups": [
    {
      "name": "Get_Credits_For_Opp",
      "label": "Get Credits for this Opportunity",
      "locationX": 176,
      "locationY": 240,
      "object": "Credits__c",
      "filterLogic": "and",
      "filters": [
        { "field": "Opportunity__c", "operator": "EqualTo", "value": { "elementReference": "$Record.Id" } }
      ],
      "getFirstRecordOnly": false,
      "storeOutputAutomatically": true,
      "connector": { "targetReference": "Loop_Credits" }
    },
    {
      "name": "Get_Stale_Share",
      "label": "Get stale pre-sales share (previous owner)",
      "locationX": 300,
      "locationY": 480,
      "object": "_System_RecordShare",
      "filterLogic": "and",
      "filters": [
        { "field": "ObjectApiName", "operator": "EqualTo", "value": { "stringValue": "Credits__c" } },
        { "field": "RecordId", "operator": "EqualTo", "value": { "elementReference": "$currentCredit.Id" } },
        { "field": "ShareWithUserId", "operator": "EqualTo", "value": { "elementReference": "$Record__Prior.Pre_Sales_Owner__c" } },
        { "field": "Reason", "operator": "EqualTo", "value": { "stringValue": "PreSalesAuto" } }
      ],
      "getFirstRecordOnly": true,
      "storeOutputAutomatically": true,
      "assignNullValuesIfNoRecordsFound": false,
      "connector": { "targetReference": "Old_Share_Exists" }
    },
    {
      "name": "Get_New_Share",
      "label": "Get existing share for new pre-sales",
      "locationX": 300,
      "locationY": 840,
      "object": "_System_RecordShare",
      "filterLogic": "and",
      "filters": [
        { "field": "ObjectApiName", "operator": "EqualTo", "value": { "stringValue": "Credits__c" } },
        { "field": "RecordId", "operator": "EqualTo", "value": { "elementReference": "$currentCredit.Id" } },
        { "field": "ShareWithUserId", "operator": "EqualTo", "value": { "elementReference": "$Record.Pre_Sales_Owner__c" } },
        { "field": "Reason", "operator": "EqualTo", "value": { "stringValue": "PreSalesAuto" } }
      ],
      "getFirstRecordOnly": true,
      "storeOutputAutomatically": true,
      "assignNullValuesIfNoRecordsFound": false,
      "connector": { "targetReference": "New_Share_Needed" }
    }
  ],
  "loops": [
    {
      "name": "Loop_Credits",
      "label": "Loop over the opp's Credits",
      "locationX": 176,
      "locationY": 360,
      "collectionReference": "Get_Credits_For_Opp",
      "assignNextValueToReference": "$currentCredit",
      "iterationOrder": "Asc",
      "nextValueConnector": { "targetReference": "Get_Stale_Share" }
    }
  ],
  "recordDeletes": [
    {
      "name": "Delete_Stale_Share",
      "label": "Delete stale pre-sales share",
      "locationX": 200,
      "locationY": 600,
      "inputReference": "Get_Stale_Share",
      "connector": { "targetReference": "New_PreSales_Present" }
    }
  ],
  "recordCreates": [
    {
      "name": "Create_New_Share",
      "label": "Create share for new pre-sales (Read)",
      "locationX": 200,
      "locationY": 1080,
      "object": "_System_RecordShare",
      "inputAssignments": [
        { "field": "ObjectApiName", "value": { "stringValue": "Credits__c" } },
        { "field": "RecordId", "value": { "elementReference": "$currentCredit.Id" } },
        { "field": "ShareWithUserId", "value": { "elementReference": "$Record.Pre_Sales_Owner__c" } },
        { "field": "AccessLevel", "value": { "stringValue": "Read" } },
        { "field": "Reason", "value": { "stringValue": "PreSalesAuto" } }
      ],
      "connector": { "targetReference": "Loop_Credits" }
    }
  ]
}
