{
  "apiVersion": 60,
  "label": "Credits - Share With Pre-Sales",
  "description": "Root fix (Option A) for the pre-sales visibility gap, self-healing. After-save on Credits__c it enforces the invariant: the ONLY PreSalesAuto _System_RecordShare rows on a Credit are for its CURRENT parent Opportunity's LIVE pre-sales user (Opportunity.Pre_Sales_Owner__c, read fresh via Get_Opportunity — never the mutable Credits.Pre_Sales_Owner__c snapshot). It (1) loops all existing PreSalesAuto shares on the Credit and DELETES any whose ShareWithUserId is not the current pre-sales (this revokes the previous opp's pre-sales when Credits.Opportunity__c is reparented to a different Opportunity, since Opportunity__c is updateable), then (2) creates the share for the current pre-sales if missing (skips when pre-sales == owner). Credits__c is sharingModel=Private with no pre-sales grant in its permission matrix (proposal section 8). Runs in trigger SystemContext so the share writes are permitted. Idempotent. Pairs with Opportunity_Resync_Credits_PreSales_Share (which handles the opp-side Pre_Sales_Owner__c change).",
  "processType": "AutoLaunchedFlow",
  "status": "Active",
  "start": {
    "locationX": 176,
    "locationY": 0,
    "object": "Credits__c",
    "triggerType": "RecordAfterSave",
    "recordTriggerType": "CreateAndUpdate",
    "filterLogic": "and",
    "filters": [
      { "field": "Opportunity__c", "operator": "IsNull", "value": { "booleanValue": false } }
    ],
    "connector": { "targetReference": "Get_Opportunity" }
  },
  "recordLookups": [
    {
      "name": "Get_Opportunity",
      "label": "Get parent Opportunity (live pre-sales)",
      "locationX": 176,
      "locationY": 120,
      "object": "Opportunity",
      "filterLogic": "and",
      "filters": [
        { "field": "Id", "operator": "EqualTo", "value": { "elementReference": "$Record.Opportunity__c" } }
      ],
      "getFirstRecordOnly": true,
      "storeOutputAutomatically": true,
      "assignNullValuesIfNoRecordsFound": false,
      "connector": { "targetReference": "Get_All_PreSales_Shares" }
    },
    {
      "name": "Get_All_PreSales_Shares",
      "label": "Get all PreSalesAuto shares on this Credit",
      "locationX": 176,
      "locationY": 240,
      "object": "_System_RecordShare",
      "filterLogic": "and",
      "filters": [
        { "field": "ObjectApiName", "operator": "EqualTo", "value": { "stringValue": "Credits__c" } },
        { "field": "RecordId", "operator": "EqualTo", "value": { "elementReference": "$Record.Id" } },
        { "field": "Reason", "operator": "EqualTo", "value": { "stringValue": "PreSalesAuto" } }
      ],
      "getFirstRecordOnly": false,
      "storeOutputAutomatically": true,
      "connector": { "targetReference": "Loop_Shares" }
    },
    {
      "name": "Get_Current_Share",
      "label": "Get the current pre-sales share (if any)",
      "locationX": 350,
      "locationY": 600,
      "object": "_System_RecordShare",
      "filterLogic": "and",
      "filters": [
        { "field": "ObjectApiName", "operator": "EqualTo", "value": { "stringValue": "Credits__c" } },
        { "field": "RecordId", "operator": "EqualTo", "value": { "elementReference": "$Record.Id" } },
        { "field": "ShareWithUserId", "operator": "EqualTo", "value": { "elementReference": "Get_Opportunity.Pre_Sales_Owner__c" } },
        { "field": "Reason", "operator": "EqualTo", "value": { "stringValue": "PreSalesAuto" } }
      ],
      "getFirstRecordOnly": true,
      "storeOutputAutomatically": true,
      "assignNullValuesIfNoRecordsFound": false,
      "connector": { "targetReference": "Need_Share" }
    }
  ],
  "loops": [
    {
      "name": "Loop_Shares",
      "label": "Loop existing PreSalesAuto shares",
      "locationX": 176,
      "locationY": 360,
      "collectionReference": "Get_All_PreSales_Shares",
      "assignNextValueToReference": "$currentShare",
      "iterationOrder": "Asc",
      "nextValueConnector": { "targetReference": "Is_Stale_Share" },
      "noMoreValuesConnector": { "targetReference": "PreSales_Present" }
    }
  ],
  "decisions": [
    {
      "name": "Is_Stale_Share",
      "label": "Share is for a different (stale) user",
      "locationX": 50,
      "locationY": 480,
      "defaultConnectorLabel": "Current - keep",
      "defaultConnector": { "targetReference": "Loop_Shares" },
      "rules": [
        {
          "name": "Stale",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "$currentShare.ShareWithUserId", "operator": "NotEqualTo", "rightValue": { "elementReference": "Get_Opportunity.Pre_Sales_Owner__c" } }
          ],
          "connector": { "targetReference": "Delete_Stale_Share" },
          "label": "Stale"
        }
      ]
    },
    {
      "name": "PreSales_Present",
      "label": "Opp has a pre-sales who is not the owner",
      "locationX": 350,
      "locationY": 480,
      "defaultConnectorLabel": "No pre-sales / is owner",
      "rules": [
        {
          "name": "Has_PreSales",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "Get_Opportunity.Pre_Sales_Owner__c", "operator": "IsNull", "rightValue": { "booleanValue": false } },
            { "leftValueReference": "Get_Opportunity.Pre_Sales_Owner__c", "operator": "NotEqualTo", "rightValue": { "elementReference": "$Record.OwnerId" } }
          ],
          "connector": { "targetReference": "Get_Current_Share" },
          "label": "Has pre-sales"
        }
      ]
    },
    {
      "name": "Need_Share",
      "label": "Current share missing",
      "locationX": 350,
      "locationY": 720,
      "defaultConnectorLabel": "Already shared",
      "rules": [
        {
          "name": "Create_When_Missing",
          "conditionLogic": "and",
          "conditions": [
            { "leftValueReference": "Get_Current_Share", "operator": "IsNull", "rightValue": { "booleanValue": true } }
          ],
          "connector": { "targetReference": "Create_PreSales_Share" },
          "label": "Create when missing"
        }
      ]
    }
  ],
  "recordDeletes": [
    {
      "name": "Delete_Stale_Share",
      "label": "Delete stale pre-sales share",
      "locationX": 50,
      "locationY": 600,
      "inputReference": "$currentShare",
      "connector": { "targetReference": "Loop_Shares" }
    }
  ],
  "recordCreates": [
    {
      "name": "Create_PreSales_Share",
      "label": "Create pre-sales record share (Read)",
      "locationX": 250,
      "locationY": 840,
      "object": "_System_RecordShare",
      "inputAssignments": [
        { "field": "ObjectApiName", "value": { "stringValue": "Credits__c" } },
        { "field": "RecordId", "value": { "elementReference": "$Record.Id" } },
        { "field": "ShareWithUserId", "value": { "elementReference": "Get_Opportunity.Pre_Sales_Owner__c" } },
        { "field": "AccessLevel", "value": { "stringValue": "Read" } },
        { "field": "Reason", "value": { "stringValue": "PreSalesAuto" } }
      ]
    }
  ]
}
