Sample Python Script for Business Rules 

    Use Case  

    Technician 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 Definition Starts #############


    # ------------------ Function to parse input from Request JSON file----------------------


    def read_file(file_Path, key=None):

        with open(file_Path) as data_file:

            data = json.load(data_file)

        if key==None:

            return data

        else:

            dataObj = data[key]

            return dataObj



    #----------- Function to get diff json old value FOR V3 Format -------------------

    # Will return old value present in the diff json


    def getValueFromDiffJSON_V3(diffJSON,key):


        try:

            if key in diffJSON['old']:

                if(isinstance(diffJSON['old'][key],dict)):

                    return diffJSON['old'][key]['name']

                else:

                    return diffJSON['old'][key]

            else:

                    print("No value present in Diff JSON")

        except:

            print("Unexpected error in parsing diff json"+diffJSON)


    #------------ Constructing the Json Object for updating the request.---------


    # data is Json that will have the Field Name/Value that needs to be updated in the request

    # data = {"LEVEL":"TIER 1","PRIORITY":"High","IMPACT":"High"}

    # Need to pass the data to actionPlugin_constructReqJSON to vet the full json

    # UPDATE,Negate are supporting for V3.


    def actionPlugin_UpdateRequest(data,OperationName="EDIT_REQUEST",module=None,additionalParams=""):

         if module is not None:

             temp={}

             temp[module]=json.loads(data)

             tempString=json.dumps(temp)

             data=tempString

         json_data = '''{

             "INPUT_DATA": [''' + data +    '''],

             "OPERATIONNAME": "''' + OperationName + '''",

            ''' + additionalParams + '''

         },'''

         return json_data


    #------------ Constructing the Json for default return functionality.---------


    # Use a combination of above two functions to construct the data parameter

    # data = actionPlugin_AddNote("Ticket has been created in JIRA and information populated in SDP MSP")


    def actionPlugin_constructReqJSON(data, message="Request Updated Successfully"):

         json_data = '''{

                  "message":"''' +message + '''",

                  "result":"success",

                  "operation":[''' + data + ''']

                  }''' 

         return json_data

    ################ Method Definition ends #############

    # File containing request details will be stored as json object and the file path will be passed as argument to the script replacing the $COMPLETE_JSON_FILE argument

    file_Path = sys.argv[1]

    # Load the json content which contains request details (Changed)

    requestObj = read_file(file_Path,'request')

    diffObj = read_file(file_Path,'diff')

    status=requestObj['status']['name']

    appr_status=requestObj['approval_status']['name']

    if  appr_status!=None and appr_status!="Approved" and status=="Waiting For Purchase":

            

         #Constructing the Json Object for updating the request.

        

        #The following is a Sample of the JSON structure for Updating a Request.

         '''{

             "result": "success",

             "operation": [

             {

                  "OPERATIONNAME": "NEGATE",

                 "REASON": "Negate Reason"

                  }

             ]

             }'''


         message = "Cannot change the request status to "+status+" unless the request is approved"

         #Creating the Json that will have the Field Name/Value that needs to be updated in the request

         updatejson = actionPlugin_UpdateRequest("","NEGATE",additionalParams='''"REASON":"'''+message+'''"''')

         returnJson = actionPlugin_constructReqJSON(updatejson,message)

         #Returning the Constructed Json Object

         print(returnJson)


    elif  appr_status!=None and appr_status=="Pending Approval":


        #The following is a Sample of the JSON structure for Updating a Request.


         '''{

           "result": "success",

           "message": "Sample Python script",

           "operation": [

             {

               "OPERATIONNAME": "UPDATE",

               "INPUT_DATA": [

                 {

                   "request": {

                     "urgency": {

                       "name": "High"

                     },

                     "group": {

                       "name": "Network"

                     },

                     "priority": {

                       "name": "High"

                     }

                   }

                 }

               ]

             }

           ]

         }'''


         message = "Set priority to High when approval status is Pending approval"

         #Creating the Json that will have the Field Name/Value that needs to be updated in the request.

         updatejson = actionPlugin_UpdateRequest('''{"priority":{"name":"High"}}''',"UPDATE","request")

         returnJson = actionPlugin_constructReqJSON(updatejson,message)

         #Returning the Constructed Json Object.

         print(returnJson)


    else :

         print("No changes found") # This message will be printed in the History if not of the Conditions in the Script was matched.

      

     

     

     

     

     

     

     

    Zoho Corp. All rights reserved.