{
  "id": "FLOW-013",
  "name": "BeforeSave flow $Profile.Name resolves in both formula resources AND Decision conditions (#2259 regression lock)",
  "description": "Regression lock for #2259. Round-1 wired $Profile.Name into formula.Engine scope (evaluateFlowFormulas / Path A). Round-2 also fixes the Decision-condition path (evaluateConditionCtx / Path B) by injecting $Profile/$User/$UserRole into interview.ContextData via injectUserGlobalsIntoContext. This case proves BOTH paths:\n  Path A — FlowFormula resource: $Profile.Name expression captures the running user's profile name and writes it to Account.Description.\n  Path B — Decision condition: a Decision element with condition '$Profile.Name EqualTo System Administrator' takes the admin branch and writes 'admin-branch' to Account.Phone; non-admin would write 'nonadmin-branch'. Running as admin@test.com (ProfileName='System Administrator') both Description and Phone must receive the admin values.\nGreenfield-safe: uses only standard SObjects (Account) and standard system fields (Description, Phone).",
  "object": "Account",
  "method": "PATCH",
  "endpoint": "/services/data/v63.0/sobjects/Account/{flow013_account_id}",
  "setup": [
    {
      "type": "http",
      "name": "Create test Account for the flow to fire on",
      "method": "POST",
      "endpoint": "/services/data/v63.0/sobjects/Account",
      "payload": {
        "Name": "FLOW-013-ProfileName-Account"
      },
      "expects": {
        "status_code": 201
      },
      "extract": {
        "flow013_account_id": "id"
      }
    },
    {
      "type": "http",
      "name": "Create FlowDefinition for the profile-name capture + decision flow",
      "method": "POST",
      "endpoint": "/services/data/v63.0/tooling/sobjects/FlowDefinition",
      "payload": {
        "DeveloperName": "Flow013_ProfileName_Decision",
        "MasterLabel": "FLOW-013 $Profile.Name formula + Decision condition"
      },
      "expects": {
        "status_code": 201
      },
      "extract": {
        "flow013_def_id": "id"
      }
    },
    {
      "type": "http",
      "name": "Create FlowVersion: formula captures $Profile.Name; Decision routes on it; writes to Description and Phone",
      "method": "POST",
      "endpoint": "/services/data/v63.0/tooling/sobjects/Flow",
      "payload": {
        "FlowDefinitionId": "{flow013_def_id}",
        "Metadata": {
          "processType": "AutoLaunchedFlow",
          "start": {
            "triggerType": "RecordBeforeSave",
            "object": "Account",
            "recordTriggerType": "Update",
            "filters": [],
            "filterLogic": "and"
          },
          "formulas": [
            {
              "name": "profileNameVar",
              "dataType": "String",
              "expression": "$Profile.Name"
            }
          ],
          "decisions": [
            {
              "name": "IsAdminDecision",
              "defaultConnector": {
                "targetReference": "WriteNonAdmin"
              },
              "defaultConnectorLabel": "Non-Admin",
              "rules": [
                {
                  "name": "IsAdminRule",
                  "label": "Is System Administrator",
                  "connector": {
                    "targetReference": "WriteAdmin"
                  },
                  "conditionLogic": "and",
                  "conditions": [
                    {
                      "leftValueReference": "$Profile.Name",
                      "operator": "EqualTo",
                      "rightValue": "System Administrator"
                    }
                  ]
                }
              ]
            }
          ],
          "recordUpdates": [
            {
              "name": "WriteProfileName",
              "connector": {
                "targetReference": "IsAdminDecision"
              },
              "object": "Account",
              "filters": [
                {
                  "field": "Id",
                  "operator": "EqualTo",
                  "value": {
                    "elementReference": "$Record.Id"
                  }
                }
              ],
              "filterLogic": "and",
              "inputAssignments": [
                {
                  "field": "Description",
                  "value": {
                    "elementReference": "profileNameVar"
                  }
                }
              ]
            },
            {
              "name": "WriteAdmin",
              "connector": null,
              "object": "Account",
              "filters": [
                {
                  "field": "Id",
                  "operator": "EqualTo",
                  "value": {
                    "elementReference": "$Record.Id"
                  }
                }
              ],
              "filterLogic": "and",
              "inputAssignments": [
                {
                  "field": "Phone",
                  "value": "admin-branch"
                }
              ]
            },
            {
              "name": "WriteNonAdmin",
              "connector": null,
              "object": "Account",
              "filters": [
                {
                  "field": "Id",
                  "operator": "EqualTo",
                  "value": {
                    "elementReference": "$Record.Id"
                  }
                }
              ],
              "filterLogic": "and",
              "inputAssignments": [
                {
                  "field": "Phone",
                  "value": "nonadmin-branch"
                }
              ]
            }
          ],
          "startElementReference": "WriteProfileName"
        }
      },
      "expects": {
        "status_code": 201
      },
      "extract": {
        "flow013_version_id": "id"
      }
    },
    {
      "type": "http",
      "name": "Activate the FLOW-013 flow",
      "method": "POST",
      "endpoint": "/services/data/v63.0/tooling/sobjects/Flow/{flow013_version_id}/activate",
      "expects": {
        "status_code": 204
      }
    }
  ],
  "payload": {
    "Name": "FLOW-013-ProfileName-Account-Updated"
  },
  "expects": {
    "status_code": 204
  },
  "query_after": {
    "object": "Account",
    "record_id": "{flow013_account_id}",
    "retry": 5,
    "delay_ms": 500,
    "assertions": [
      {
        "field": "Description",
        "operator": "eq",
        "value": "System Administrator",
        "comment": "Path A: $Profile.Name in FlowFormula resource must resolve to 'System Administrator' (admin@test.com profile)"
      },
      {
        "field": "Phone",
        "operator": "eq",
        "value": "admin-branch",
        "comment": "Path B: Decision condition '$Profile.Name EqualTo System Administrator' must take admin branch — proves injectUserGlobalsIntoContext populates ContextData for evaluateConditionCtx"
      }
    ]
  },
  "teardown": [
    {
      "type": "http",
      "method": "DELETE",
      "endpoint": "/services/data/v63.0/sobjects/Account/{flow013_account_id}"
    },
    {
      "type": "http",
      "method": "DELETE",
      "endpoint": "/services/data/v63.0/tooling/sobjects/FlowDefinition/{flow013_def_id}"
    }
  ],
  "priority": "P0",
  "bug_ref": "#2259",
  "scene": "flow"
}
