{
  "id": "CTR-004",
  "name": "Contract Record Type should be saved correctly and echoed back after creation",
  "description": "Contract creation with Record Type selection (10 types: JP-CSA, GB-MA, US-MA, etc.) should save correctly and echo on detail page. Bug: #556 - Record Type not saved/displayed correctly after creation.",
  "object": "Contract",
  "method": "POST",
  "endpoint": "/services/data/v63.0/sobjects/Contract",
  "payload": {
    "AccountId": "{seed_Account_0}",
    "StatusCode": "Draft",
    "Status": "Draft",
    "RecordTypeId": "{recordtype_id}",
    "ContractTerm": 12,
    "StartDate": "2025-01-01",
    "OwnerId": "0055g000001i4R9AAI"
  },
  "setup": [
    {
      "name": "Create seed Account for CTR-004",
      "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": [
        "0015g00000CTR004AA0",
        "CTR004 Account",
        "Customer - Direct",
        "5550104",
        "US",
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ],
      "id_var": "seed_Account_0",
      "id_value": "0015g00000CTR004AA0",
      "object": "Account"
    },
    {
      "name": "Look up an active Contract RecordType for CTR-004",
      "type": "sql",
      "sql": "SELECT rt.Id AS recordtype_id FROM _System_RecordType rt JOIN _System_Object so ON so.Id = rt.ObjectId WHERE so.ApiName IN ('Contract','contract') AND rt.IsActive = 1 AND (rt.IsDeleted = 0 OR rt.IsDeleted IS NULL) ORDER BY CASE WHEN rt.Name = 'TiDB Cloud Services Agreement' THEN 0 ELSE 1 END, rt.Name ASC LIMIT 1",
      "fetch": "one",
      "extract": {
        "recordtype_id": "recordtype_id"
      }
    },
    {
      "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))"
    }
  ],
  "expects": {
    "status_code": 201
  },
  "query_after": {
    "object": "Contract",
    "assertions": [
      {
        "field": "RecordTypeId",
        "operator": "eq",
        "value": "{recordtype_id}"
      }
    ]
  },
  "teardown": [
    {
      "method": "DELETE",
      "endpoint": "/services/data/v63.0/sobjects/Contract/{id}"
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Account WHERE Id = %s",
      "params": [
        "0015g00000CTR004AA0"
      ]
    }
  ],
  "priority": "P1",
  "bug_ref": "#556",
  "scene": "contract",
  "skip": true,
  "skip_reason": "Regression case is structurally repaired, but current product behavior still returns 500 because Contract Name autonumber cannot resolve {Account.BillingCountryCode} before the RecordType assertion can execute"
}
