How to automatically create multiple child requests from a parent request with a configurable JSON file (V3)

How to automatically create multiple child requests from a parent request with a configurable JSON file (V3)


This is a sample Python script to automatically trigger the creation of multiple child requests from a parent request, based on the request template chosen. The request ID of the child requests will be added as a Note in the parent request.

This is achieved using an API call to create a request in ServiceDesk Plus. Details of the child requests are stored in a JSON file and provided as the primary parameter to the script. The parent request details are stored as a JSON object in a file and its path is provided as the second parameter.  A separate API  is used to add the request ids of the child requests as notes to the Parent request. This is achieved using Request Custom Triggers and the link here will provide more information on that.

Use Case: When certain Services are requests by a user, under the service category 'User Management', multiple child requests have to be triggered. In the sample shown above, a single child request will be triggered when a Service Request is created using the template 'Request to start the process associated to a new hire'.

Updating the Configuration file:
The configuration file has the name of the request template of the parent request and the list of child requests to be created. The script matches the template 's name received from the parent request against the name in the JSON file and creates child requests accordingly.The Child request details are stored in the configuration file labeled "ChildRequestsV3.json", in the format shown below.

{
  "Request to start the process associated to a new hire": [
    {
      "request": {
        "subject": "Create a VPN Account for the employee $udf_fields,$udf_sline_19 - Parent ID : $id",
        "requester": "$requester"
      }
    }
  ]
}
The highlighted text is the name of the template, 
$udf_fields,$udf_sline_19 additional field values from the parent request
Refer here for more parameters
Note: The variable names enclosed within the dollar symbol are those of the fields that are fetched from the parent request.

Example: New hire Template UI is as below


API format after adding the request is as below


Now if you would like to move the User information to the child request just configure the ChildRequestsV3.json like this

{
  "New Hire": [
    {
      "request": {
        "subject": "Create a VPN Account for the employee $udf_fields,$udf_sline_19 - Parent ID : $id",
        "requester": "$requester",
        "description":"Employee Name: $udf_fields,$udf_sline_19 \n Mobile : $udf_fields,$udf_sline_23  \n Department : $udf_fields,$udf_sline_20 \n Seating Location : $udf_fields,$udf_sline_21 \n Joining Date : $udf_fields,$udf_date_901,$display_value" 
        
      }
    }
  ]
}

For example, if you want to populate employee name to the child request subject just add $udf_fields,$udf_sline_19 this will replace with exact value from the parent request when child request is created

For moving the UDF fields from parent request to child request

{
  "Default Request": [
    {
      "request": {
        "subject": "Create a VPN Account for the employee $udf_fields,$udf_sline_1201 and parent Id :  $id",
        "requester": "$requester",
"udf_fields": {
"udf_sline_1201": "$udf_fields,$udf_sline_301",
"udf_sline_301": "$udf_fields,$udf_sline_1201"
}
      }
    }
  ]
}

{
  "Default Request": [
    {
      "request": {
        "subject": "Create a VPN Account for the employee $udf_fields,$udf_sline_1201 and parent Id :  $id",
        "requester": "$requester",
         "udf_fields": "$udf_fields"
      }
    }
  ]
}

Important Note:
1. Make sure that the criteria configured for the parent request in the custom trigger do not match the child requests during creation. If it matches, then it will end up in a loop, where the criteria will match and requests will be created in the loop.
2.Execution Command: py MultipleRequestsV3.py ChildRequestsV3.json $COMPLETE_V3_JSON_FILE

Execution steps:
1. Download the attached zip
2. Move all files to sdp_home/integration/custom_scripts folder
Note: Don't move configuration.json if there is a file with the same name exists
3. Edit configuration.json update url and technicianKey
4. Add the template names and child request details in the JSON configuration file labeled ChildRequestsV3.json.
5. Configure the script and its parameter under the Admin -> Custom Triggers . The script file to run parameter will be set as shown below.
py MultipleRequestsV3.py ChildRequestsV3.json $COMPLETE_V3_JSON_FILE
I've attached a screenshot showing a sample implementation below.



A short guide on setting up python is available here