{
  "apiVersion": 56,
  "label": "Credits — Pre-fill Snapshot Fields From Account / Opportunity",
  "description": "Before a new Credits record is committed, look up Account (always required) and Opportunity (if linked) and stamp the snapshot fields onto the record. Covers fields that ui_action prefillMappings can't reach via transitive lookup (Opp.Account.* etc.). Runs before the Validation Rules + ProcessDefinition entry — by the time approval submission happens, all snapshot fields are populated. See proposal §6 / §3 snapshot rule.",
  "processType": "AutoLaunchedFlow",
  "status": "Active",
  "start": {
    "object": "Credits__c",
    "triggerType": "RecordBeforeSave",
    "recordTriggerType": "Create",
    "connector": {"targetReference": "Check_Account_Present"}
  },
  "recordLookups": [
    {
      "name": "Get_Account",
      "label": "Get Account for Snapshot Fields",
      "locationX": 200,
      "locationY": 100,
      "object": "Account",
      "filterLogic": "and",
      "filters": [
        {"field": "Id", "operator": "EqualTo", "value": {"elementReference": "$Record.Account__c"}}
      ],
      "getFirstRecordOnly": true,
      "storeOutputAutomatically": true,
      "assignNullValuesIfNoRecordsFound": false,
      "connector": {"targetReference": "Check_Opportunity_Present"}
    },
    {
      "name": "Get_Opportunity",
      "label": "Get Opportunity for Snapshot Fields",
      "locationX": 100,
      "locationY": 300,
      "object": "Opportunity",
      "filterLogic": "and",
      "filters": [
        {"field": "Id", "operator": "EqualTo", "value": {"elementReference": "$Record.Opportunity__c"}}
      ],
      "getFirstRecordOnly": true,
      "storeOutputAutomatically": true,
      "assignNullValuesIfNoRecordsFound": false,
      "connector": {"targetReference": "Stamp_Account_And_Opportunity_Snapshot"}
    }
  ],
  "formulas": [
    {
      "name": "Credits_Name_Value",
      "dataType": "String",
      "expression": "BLANKVALUE({!$Record.Account__r.Name}, \"Credits\") & \" — \" & TEXT({!$Record.Credit_Type__c}) & \" Credits\""
    }
  ],
  "decisions": [
    {
      "name": "Check_Account_Present",
      "label": "Account linked? (defensive guard)",
      "locationX": 200,
      "locationY": 50,
      "rules": [
        {
          "name": "account_present",
          "label": "Yes — proceed to lookup",
          "conditionLogic": "and",
          "conditions": [
            {"leftValueReference": "$Record.Account__c", "operator": "IsNull", "rightValue": {"booleanValue": false}}
          ],
          "connector": {"targetReference": "Get_Account"}
        }
      ],
      "defaultConnectorLabel": "No — terminate (schema required validation will surface the error)"
    },
    {
      "name": "Check_Opportunity_Present",
      "label": "Opportunity linked?",
      "locationX": 200,
      "locationY": 200,
      "rules": [
        {
          "name": "opportunity_present",
          "label": "Yes — fetch Opportunity",
          "conditionLogic": "and",
          "conditions": [
            {"leftValueReference": "$Record.Opportunity__c", "operator": "IsNull", "rightValue": {"booleanValue": false}}
          ],
          "connector": {"targetReference": "Get_Opportunity"}
        }
      ],
      "defaultConnector": {"targetReference": "Stamp_Account_Snapshot_Only"},
      "defaultConnectorLabel": "No — stamp Account fields only"
    }
  ],
  "assignments": [
    {
      "name": "Stamp_Account_And_Opportunity_Snapshot",
      "label": "Stamp Snapshot Fields From Account + Opportunity",
      "locationX": 100,
      "locationY": 400,
      "assignmentItems": [
        {"assignToReference": "$Record.Account_Name__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Name"}},
        {"assignToReference": "$Record.Account_Owner__c", "operator": "Assign", "value": {"elementReference": "Get_Account.OwnerId"}},
        {"assignToReference": "$Record.Region__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Region__c"}},
        {"assignToReference": "$Record.Industry__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Industry"}},
        {"assignToReference": "$Record.Vertical__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Vertical__c"}},
        {"assignToReference": "$Record.Opportunity_Name__c", "operator": "Assign", "value": {"elementReference": "Get_Opportunity.Name"}},
        {"assignToReference": "$Record.Pre_Sales_Owner__c", "operator": "Assign", "value": {"elementReference": "Get_Opportunity.Pre_Sales_Owner__c"}}
      ],
      "connector": {"targetReference": "Stamp_Record_Name"}
    },
    {
      "name": "Stamp_Account_Snapshot_Only",
      "label": "Stamp Snapshot Fields From Account (no Opp)",
      "locationX": 300,
      "locationY": 400,
      "assignmentItems": [
        {"assignToReference": "$Record.Account_Name__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Name"}},
        {"assignToReference": "$Record.Account_Owner__c", "operator": "Assign", "value": {"elementReference": "Get_Account.OwnerId"}},
        {"assignToReference": "$Record.Region__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Region__c"}},
        {"assignToReference": "$Record.Industry__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Industry"}},
        {"assignToReference": "$Record.Vertical__c", "operator": "Assign", "value": {"elementReference": "Get_Account.Vertical__c"}}
      ],
      "connector": {"targetReference": "Stamp_Record_Name"}
    },
    {
      "name": "Stamp_Record_Name",
      "label": "Stamp Record Name (after snapshot so Account_Name__c is committed on $Record)",
      "locationX": 200,
      "locationY": 500,
      "assignmentItems": [
        {"assignToReference": "$Record.Name", "operator": "Assign", "value": {"elementReference": "Credits_Name_Value"}}
      ]
    }
  ]
}
