{
  "id": "APR-006",
  "name": "Approval submit: first non-matching step with GotoNextStep starts next matching step",
  "description": "Regression for #2054. The initial submit path must use the same step criteria and ifCriteriaNotMet semantics as later approval advancement, so a first non-matching step with GotoNextStep creates the pending work item for the next matching step.",
  "object": "Account",
  "method": "POST",
  "endpoint": "/services/data/v63.0/process/approvals",
  "setup": [
    {
      "type": "sql",
      "name": "Load admin profile for approver user",
      "sql": "SELECT ProfileId AS profile_id FROM User WHERE Username = %s LIMIT 1",
      "params": ["admin@test.com"],
      "fetch": "one",
      "extract": {
        "profile_id": "profile_id"
      }
    },
    {
      "type": "sql",
      "name": "Create second-step approver",
      "sql": "INSERT INTO User (Id, Username, Email, Password, FirstName, LastName, ProfileId, IsActive, IsDeleted, CreatedDate, LastModifiedDate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "005APR006APP00001",
        "apr006.approver@example.com",
        "apr006.approver@example.com",
        "unused",
        "APR006",
        "Approver",
        "{profile_id}",
        1,
        0,
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Account for APR-006",
      "sql": "INSERT INTO Account (Id, Name, Industry, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "001APR006ACC00001",
        "APR-006 Initial Step Skip",
        "Finance",
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create ProcessDefinition for APR-006",
      "sql": "INSERT INTO ProcessDefinition (Id, Name, DeveloperName, State, TableEnumOrId, IsActive, Metadata, ProcessOrder, CreatedDate, LastModifiedDate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "04aAPR006PD00001",
        "APR-006 Initial Step Skip",
        "APR006_Initial_Step_Skip",
        "Active",
        "Account",
        1,
        "{\"approvalStep\":[{\"name\":\"First_Review\",\"label\":\"First Review\",\"entryCriteria\":{\"criteriaItems\":[{\"field\":\"Account.Industry\",\"operation\":\"equals\",\"value\":\"Technology\"}]},\"ifCriteriaNotMet\":\"GotoNextStep\"},{\"name\":\"Second_Review\",\"label\":\"Second Review\"}]}",
        -999,
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create first non-matching ProcessNode",
      "sql": "INSERT INTO ProcessNode (Id, Name, DeveloperName, ProcessDefinitionId, ApproverType, ApproverUserId, AllowComments, RequireComments, StepOrder, CreatedDate, LastModifiedDate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "04bAPR006PN00001",
        "First Review",
        "First_Review",
        "04aAPR006PD00001",
        "User",
        "missing-first-step-approver@example.com",
        1,
        0,
        1,
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create second matching ProcessNode",
      "sql": "INSERT INTO ProcessNode (Id, Name, DeveloperName, ProcessDefinitionId, ApproverType, ApproverUserId, AllowComments, RequireComments, StepOrder, CreatedDate, LastModifiedDate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "04bAPR006PN00002",
        "Second Review",
        "Second_Review",
        "04aAPR006PD00001",
        "User",
        "005APR006APP00001",
        1,
        0,
        2,
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    }
  ],
  "payload": {
    "ObjectApiName": "Account",
    "requests": [
      {
        "actionType": "Submit",
        "contextId": "001APR006ACC00001",
        "comments": "APR-006 initial step skip"
      }
    ]
  },
  "expects": {
    "status_code": 200
  },
  "response_assertions": [
    {
      "field": "0.success",
      "operator": "eq",
      "value": true
    },
    {
      "field": "0.instanceStatus",
      "operator": "eq",
      "value": "Pending"
    },
    {
      "field": "0.actorIds.0",
      "operator": "eq",
      "value": "005APR006APP00001"
    },
    {
      "field": "0.newWorkitemIds.length",
      "operator": "eq",
      "value": 1
    }
  ],
  "extract": {
    "workitem_id": "0.newWorkitemIds.0"
  },
  "query_after": {
    "object": "ProcessInstanceWorkitem",
    "record_id": "{workitem_id}",
    "assertions": [
      {
        "field": "ActorId",
        "operator": "eq",
        "value": "005APR006APP00001"
      }
    ]
  },
  "teardown": [
    {
      "type": "sql",
      "name": "Clean up ProcessInstanceWorkitem",
      "sql": "DELETE FROM ProcessInstanceWorkitem WHERE ProcessInstanceId IN (SELECT Id FROM ProcessInstance WHERE TargetObjectId = %s)",
      "params": ["001APR006ACC00001"]
    },
    {
      "type": "sql",
      "name": "Clean up ProcessInstanceNode",
      "sql": "DELETE FROM ProcessInstanceNode WHERE ProcessInstanceId IN (SELECT Id FROM ProcessInstance WHERE TargetObjectId = %s)",
      "params": ["001APR006ACC00001"]
    },
    {
      "type": "sql",
      "name": "Clean up ProcessInstance",
      "sql": "DELETE FROM ProcessInstance WHERE TargetObjectId = %s",
      "params": ["001APR006ACC00001"]
    },
    {
      "type": "sql",
      "name": "Delete ProcessNode rows",
      "sql": "DELETE FROM ProcessNode WHERE ProcessDefinitionId = %s",
      "params": ["04aAPR006PD00001"]
    },
    {
      "type": "sql",
      "name": "Delete ProcessDefinition row",
      "sql": "DELETE FROM ProcessDefinition WHERE Id = %s",
      "params": ["04aAPR006PD00001"]
    },
    {
      "type": "sql",
      "name": "Delete Account row",
      "sql": "DELETE FROM Account WHERE Id = %s",
      "params": ["001APR006ACC00001"]
    },
    {
      "type": "sql",
      "name": "Delete approver user",
      "sql": "DELETE FROM User WHERE Id = %s",
      "params": ["005APR006APP00001"]
    }
  ],
  "priority": "P0",
  "bug_ref": "#2054",
  "scene": "approval"
}
