0

Fetch a single record from any ServiceNow table by its sys_id.

by
Published Apr 19, 2026

Retrieves a single record from the ServiceNow Table API using the record's sys_id. Works with any table (incident, change_request, cmdb_ci, etc.). Optionally accepts a comma-separated fields list to limit the returned payload; omit it to get all fields. Authentication is handled automatically via the shared get_token helper — tokens are cached and reused across calls.

Script servicenow
  • Submitted by elib3n379 Python3
    Created 51 days ago
    1
    
    
    2
    import requests
    3
    from typing import TypedDict
    4
    
    
    5
    import wmill
    6
    from f.ServiceNow.auth import servicenow, get_token
    7
    
    
    8
    
    
    9
    def main(
    10
        snow: servicenow,
    11
        table: str,
    12
        sys_id: str,
    13
        fields: str = "",
    14
    ) -> dict:
    15
        """
    16
        Fetch a single ServiceNow record by sys_id.
    17
    
    
    18
        Args:
    19
            snow:   ServiceNow connection resource (instance_url, credentials).
    20
            table:  Table name, e.g. 'incident', 'change_request', 'cmdb_ci'.
    21
            sys_id: The 32-character sys_id of the record.
    22
            fields: Comma-separated list of fields to return (empty = all fields).
    23
        """
    24
        token = get_token(snow)
    25
    
    
    26
        params = {"sysparm_display_value": "false"}
    27
        if fields.strip():
    28
            params["sysparm_fields"] = fields.strip()
    29
    
    
    30
        url = f"{snow['instance_url'].rstrip('/')}/api/now/table/{table}/{sys_id}"
    31
        resp = requests.get(
    32
            url,
    33
            headers={
    34
                "Authorization": f"Bearer {token}",
    35
                "Accept": "application/json",
    36
            },
    37
            params=params,
    38
            timeout=30,
    39
        )
    40
    
    
    41
        if resp.status_code == 404:
    42
            raise ValueError(f"Record not found: table={table}, sys_id={sys_id}")
    43
    
    
    44
        resp.raise_for_status()
    45
        record = resp.json().get("result", {})
    46
        print(f"Retrieved record from {table}: {sys_id}")
    47
        return record
    48