Write Custom Script in Python

Write Custom Script in Python

You need working knowledge of:
  1. JSON
  2. Python

Custom Script File Structure 

  1. Import required packages
  2. Get input arguments
  3. Implement the logic
  4. Return JSON

Frequently Used Packages 

Package
Usage
Sys
Fetches the input arguments
json
Manipulates JSON data
requests
Makes API calls
datetime
Transforms time from milliseconds to the required date format
 

Getting Input Arguments 

The script file arguments can be fetched using sys.argv[index] where index starts from 1 to the number of arguments passed.
When the argument passed is $COMPLETE_V3_JSON_FILE (the path to the file containing the request JSON), the JSON file can be read using the following snippet:
file_Path = sys.argv[1] 
with open(file_Path) as data_file: 
data = json.load(data_file) 

Implementing the Logic 

Snippet to make API call:
 with requests.Session() as s: 
 url = 'api_url' 
 r = s.post(url,verify=True, data=post_data,headers=headers) 

Construct the api_url, post_data and headers as required.
Snippet to transform time from millisec to required date format:

date = datetime.datetime.fromtimestamp(int(millisec)/1e3).strftime('%d %b %Y, %H:%M:%S')

Constructing Return JSON 

A sample JSON such as {"key":"value"} construction:
json = {} 
json["key"] = "value" 
print(json)

A sample JSON array such as [{"key":"value"}] construction:
json = {} 
json["key"] = "value" 
result = [] 
result.append(json) 
print(result) 

    • Related Articles

    • Custom Actions

      Custom actions refer to user defined actions that can be performed on different entities across modules. For a custom action to be performed on an entity, it should be used with automation rules, subject to the availability of support. Availability ...
    • Custom Scheduled Function

      SupportCenter Plus allows administrators to execute custom schedules using a built-in script execution tool called Custom Scheduled Function. The custom scheduled function scripts can be built from scratch using Deluge, Zoho's propriety scripting ...
    • Custom Triggers - Requests

      Custom Triggers are used to configure automated actions for incoming requests or records that fulfill certain preconfigured criteria. You can automate several processes, such as sending customized email notifications, creating a new request based on ...
    • Request Custom Functions

      Request custom functions can be used in business rules, request custom triggers, request life cycle, and request timer actions. Using custom functions, you can perform automated actions on requests, other modules, and external applications. Go to the ...
    • Sample Python Script for Business Rules

      Use Case Support rep mustn't be allowed to change the request status to Waiting For Purchase unless the request is approved. Below are the list of Packages used in the script import sys, requests import json,os import datetime ################ Method ...