{
  "id": "CTR-001",
  "name": "Create Contract from Order: auto-fill AccountId and ContractNumber",
  "description": "When creating Contract from activated Order, should auto-link AccountId and generate ContractNumber (AutoNumber). Bug: #555 - Contract AutoNumber not auto-generated.",
  "object": "Contract",
  "method": "POST",
  "endpoint": "/services/data/v63.0/sobjects/Contract",
  "setup": [
    {
      "name": "Create seed Account for CTR-001",
      "type": "sql",
      "sql": "INSERT INTO Account (Id, Name, Type, Phone, BillingCountryCode, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "0015g00000CTR001AA0",
        "CTR001 Account",
        "Customer - Direct",
        "5550101",
        "US",
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ],
      "id_var": "seed_Account_0",
      "id_value": "0015g00000CTR001AA0",
      "object": "Account"
    },
    {
      "name": "Seed ContractNumber autonumber counter",
      "type": "sql",
      "sql": "INSERT INTO `_System_AutoNumberCounter` (Id, FieldId, ScopeKey, NextValue, CreatedDate, LastModifiedDate) SELECT 'SYS_CTR_NUM', f.Id, '', 1, NOW(6), NOW(6) FROM `_System_Field` f WHERE f.ApiName = 'ContractNumber' AND f.ObjectId = (SELECT Id FROM `_System_Object` WHERE ApiName = 'Contract') ON DUPLICATE KEY UPDATE NextValue = GREATEST(NextValue, VALUES(NextValue))"
    }
  ],
  "payload": {
    "AccountId": "{seed_Account_0}",
    "Name": "CTR-001-Contract",
    "StatusCode": "Draft",
    "Status": "Draft",
    "StartDate": "2025-01-01",
    "ContractTerm": 12,
    "OwnerId": "0055g000001i4R9AAI"
  },
  "expects": {
    "status_code": 201
  },
  "query_after": {
    "object": "Contract",
    "assertions": [
      {
        "field": "AccountId",
        "operator": "is_not_null",
        "value": true
      },
      {
        "field": "ContractNumber",
        "operator": "is_not_null",
        "value": true
      }
    ]
  },
  "teardown": [
    {
      "method": "DELETE",
      "endpoint": "/services/data/v63.0/sobjects/Contract/{id}"
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Account WHERE Id = %s",
      "params": [
        "0015g00000CTR001AA0"
      ]
    }
  ],
  "priority": "P1",
  "bug_ref": "#555",
  "scene": "contract"
}
