0

Service Now Case APIs

by
Published Aug 20, 2025

Service Now Case APIs

Script servicenow
  • Submitted by vinayak saokar174 Python3
    Created 294 days ago
    1
    
    
    2
    
    
    3
    def main(servicenow_instance: str, username: str, password: str, short_description: str, description: str, contact: str = None, account: str = None):
    4
        """
    5
        Creates a new case in ServiceNow.
    6
    
    
    7
        :param servicenow_instance: The URL of the ServiceNow instance (e.g., your-instance.service-now.com).
    8
        :param username: The username for authentication.
    9
        :param password: The password for authentication.
    10
        :param short_description: A short description of the case.
    11
        :param description: A detailed description of the case.
    12
        :param contact: The contact for the case.
    13
        :param account: The account for the case.
    14
        """
    15
        url = f"https://{servicenow_instance}/api/sn_customerservice/case"
    16
        
    17
        headers = {
    18
            "Content-Type": "application/json",
    19
            "Accept": "application/json",
    20
        }
    21
        
    22
        data = {
    23
            "short_description": short_description,
    24
            "description": description,
    25
        }
    26
        
    27
        if contact:
    28
            data["contact"] = contact
    29
        if account:
    30
            data["account"] = account
    31
            
    32
        response = requests.post(url, auth=(username, password), headers=headers, data=json.dumps(data))
    33
        
    34
        if response.status_code == 201:
    35
            return response.json()
    36
        else:
    37
            return {"error": response.text, "status_code": response.status_code}
    38
    
    
    39
    if __name__ == "__main__":
    40
        # Example usage (replace with your actual data)
    41
        # You would typically run this within the Windmill environment
    42
        # For local testing, you can set up environment variables or a config file
    43
        instance = "your-instance.service-now.com"
    44
        user = "your-username"
    45
        pwd = "your-password"
    46
        short_desc = "Case created via API"
    47
        desc = "This is a test case created using the Windmill script."
    48
        
    49
        result = main(instance, user, pwd, short_desc, desc)
    50
        print(result)
    51