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 

Fetches the input arguments
Manipulates JSON data
Makes API calls
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" 

A sample JSON array such as [{"key":"value"}] construction:
json = {} 
json["key"] = "value" 
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 ...
    • 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 ...
    • Custom Schedules

      SupportCenter Plus allows you to define various custom actions and schedule them for periodic execution.  This is a portal-specific configuration. To create a custom schedule: 1. Go to Admin>>General Settings>>Custom Schedules. 2. Click New to go to ...
    • Custom Triggers - Tasks

      Custom triggers execute automated actions on a task when the following events occur: Created Edited Deleted Triggered Task schedule is changed Define Custom Triggers Go to Admin > Automation > Custom Triggers > Task. Click Add New Action. Provide ...
    • Custom Module API

      Custom Module API allows customers to add, edit, and delete records through REST API from scripts or external clients. The list of supported operations with the expected URL and input_data is given below. For more details about the custom module ...