Updating an incident form to display data obtained from a rest api get request

Sidhartha Bose
Tera Contributor

Hi So im doing a project where i have to send a get a request to a third party api with basic authentication. It send a response body that contains solution numbers and solution names based on a category

This is the response body obtained after a GET request

[
    {
        "category_id": 10,
        "category": "Software Installations",
        "category_description": "",
        "category_fa_icon_class": "fa fa-desktop",
        "solutions": [
            {
                "sol_num": 1508.0,
                "sol_name": "Chrome Installation",
                "sol_description": "This solution will help to install chrome.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport1508.exe"
            },
            {
                "sol_num": 16005.0,
                "sol_name": "Ultra VNC Installation",
                "sol_description": "This solution will help to download and install Ultra VNC.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16005.exe"
            },
            {
                "sol_num": 16002.0,
                "sol_name": "Java Installation",
                "sol_description": "This solution will help to download and install Java.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16002.exe"
            },
            {
                "sol_num": 16003.0,
                "sol_name": "Open Office Instllation",
                "sol_description": "This solution will help to download and install Open Office.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16003.exe"
            },
            {
                "sol_num": 16004.0,
                "sol_name": "Thunderbird Installation",
                "sol_description": "This solution will help to download and install Thunderbird.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16004.exe"
            },
            {
                "sol_num": 16006.0,
                "sol_name": "Acrobat Reader Installation",
                "sol_description": "This solution will help to download and install Acrobat Reader.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16006.exe"
            },
            {
                "sol_num": 16007.0,
                "sol_name": "uniFLOW_West Installation",
                "sol_description": "This solution will help to download and install uniFLOW_West.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16007.exe"
            },
            {
                "sol_num": 16008.0,
                "sol_name": "7-Zip Installation",
                "sol_description": "This solution will help to download and install 7-Zip.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16008.exe"
            },
            {
                "sol_num": 16009.0,
                "sol_name": "Adobe DC Installation",
                "sol_description": "This solution will help to download and install Adobe DC.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16009.exe"
            },
            {
                "sol_num": 16010.0,
                "sol_name": "Check Point VPN Installation",
                "sol_description": "This solution will help to download and install Check Point VPN.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16010.exe"
            },
            {
                "sol_num": 16011.0,
                "sol_name": "Adobe XI Installation",
                "sol_description": "This solution will help to download and install Adobe XI.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16011.exe"
            }
        ]
    },
    {
        "category_id": 11,
        "category": "Outlook",
        "category_description": "This category helps to resolve outlook related issues",
        "category_fa_icon_class": "glyphicon glyphicon-envelope",
        "solutions": [
            {
                "sol_num": 557.0,
                "sol_name": "Unable to send recieve mails in Outlook",
                "sol_description": "This solution will resolve the issues of failed sending or receival of e-mails by connecting the Mail Box to the mail server and cleaning a temporary Outlook files.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport557.exe"
            },
            {
                "sol_num": 571.0,
                "sol_name": "Excel attachments not opening in Outlook",
                "sol_description": "This solution helps to resolve Excel attachment not opening in outlook by correcting protected view settings",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport571.exe"
            },
            {
                "sol_num": 4.0,
                "sol_name": "Speed up Outlook",
                "sol_description": "This solution helps to speed up Outlook by removing redundant Add-ins, creating new profile and checking the size of Outlook Data File",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport4.exe"
            },
            {
                "sol_num": 32.0,
                "sol_name": "Configure Outlook",
                "sol_description": "This solution helps to configure Outlook by creating a new profile",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport32.exe"
            },
            {
                "sol_num": 1201.0,
                "sol_name": "Add a signature to your e-mails in Outlook",
                "sol_description": "This solution will help you to add a signature to your e-mails in Outlook.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport1201.exe"
            },
            {
                "sol_num": 16012.0,
                "sol_name": "Ice warp to Outlook mail configuration",
                "sol_description": "This solution will help with Ice warp to Outlook mail configuration.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16012.exe"
            },
            {
                "sol_num": 13.0,
                "sol_name": "Outlook is not connecting",
                "sol_description": "This solution helps when MS Outlook is giving error cannot open your default e-mail folders",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport13.exe"
            },
            {
                "sol_num": 66.0,
                "sol_name": "Archive older mails manually in Outlook",
                "sol_description": "This Solution will help you to Archive older mails manually in Outlook",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport66.exe"
            },
            {
                "sol_num": 10.0,
                "sol_name": "Repair Outlook data (PST) files",
                "sol_description": "This solution helps to Repair Outlook data (PST) files.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport10.exe"
            }
        ]
    },
    {
        "category_id": 12,
        "category": "Microsoft Office",
        "category_description": "This category helps to resolve Microsoft Office related issues",
        "category_fa_icon_class": "glyphicon glyphicon-folder-open",
        "solutions": [
            {
                "sol_num": 51.0,
                "sol_name": "Speed up Excel",
                "sol_description": "This solution will speed up Excel by adjusting virtual memory, disabling hardware accleration, changing default printer and removing redundant Add-ins",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport51.exe"
            },
            {
                "sol_num": 503.0,
                "sol_name": "Office Quick Repair",
                "sol_description": "This solution will resolve issues and crashes with Outlook, Excel, Word etc by doing quick repair of Office",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport503.exe"
            }
        ]
    },
    {
        "category_id": 14,
        "category": "System",
        "category_description": "This category helps to resolve System related issues.",
        "category_fa_icon_class": "glyphicon glyphicon-blackboard",
        "solutions": [
            {
                "sol_num": 905.0,
                "sol_name": "Speed up your machine",
                "sol_description": "This solution helps to speed up your machine by freeing up disk space and various system optimization operation",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport905.exe"
            },
            {
                "sol_num": 1102.0,
                "sol_name": "Networking Troubleshooter",
                "sol_description": "This solution helps to run Networking Troubleshooter",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport1102.exe"
            },
            {
                "sol_num": 16000.0,
                "sol_name": "Uninstall/Reinstall Antivirus",
                "sol_description": "This solution will help to uninstall/reinstall Antivirus.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16000.exe"
            },
            {
                "sol_num": 16001.0,
                "sol_name": "AV Patch Update",
                "sol_description": "This solution will help to update AV Patch.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16001.exe"
            },
            {
                "sol_num": 16013.0,
                "sol_name": "How to configure New cloud email services",
                "sol_description": "This solution will help to configure new cloud email services.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport16013.exe"
            }
        ]
    },
    {
        "category_id": 15,
        "category": "Connectivity",
        "category_description": "This category helps to resolve Connectivity related issues.",
        "category_fa_icon_class": "glyphicon glyphicon-signal",
        "solutions": [
            {
                "sol_num": 904.0,
                "sol_name": "Maintain proxy setting",
                "sol_description": "This solution helps to resolve browsing issues by maintain proxy settings for Microsoft Edge and Chrome",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport904.exe"
            },
            {
                "sol_num": 1160.0,
                "sol_name": "Wi-Fi issue",
                "sol_description": "This solution helps to forget all your networks, enable and disable Wi-Fi, release and renew IP addresses, and display available Wi-Fi connections.",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport1160.exe"
            }
        ]
    },
    {
        "category_id": 16,
        "category": "Browser",
        "category_description": "This category helps to resolve Browser related issues",
        "category_fa_icon_class": "glyphicon glyphicon-globe",
        "solutions": [
            {
                "sol_num": 20.0,
                "sol_name": "Unable to browse",
                "sol_description": "This solution helps to resolve browsing issues by switching off proxy, cache and cookies of Chrome, remove add-ons and reset IE setting",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport20.exe"
            }
        ]
    },
    {
        "category_id": 17,
        "category": "Printer",
        "category_description": "This category helps to resolve Printer related issues.",
        "category_fa_icon_class": "glyphicon glyphicon-print",
        "solutions": [
            {
                "sol_num": 1107.0,
                "sol_name": "Printer Troubleshooter",
                "sol_description": "This solution helps to find and fix problems with printing",
                "sol_download_url": "/uploads/solutions/demo/techselfsupport1107.exe"
            }
        ]
    }
]

 

I need to get the solution number and put it in a, html link displaying the solution name preferably inside the incident form, the link for solution number for example 1160 is

https://xyz/api/solutions/download?sn=1160

 

I have been trying to do this using a script include and a business rule but am unable to get it display any values .

The sscript include has the get request with credentials and the business rule is set to trigger when an incident is created or updated. Can sombeody please help me with this and tell me how to procede further. Am i missing something which is resulting in nothing happening when i submit my incident ticket.

 

this is the script include

// FetchingsSolutions Script Include

var FetchingSolutions = Class.create();

FetchingSolutions.prototype = {
initialize: function() {},

// Method to handle HTTP request asynchronously
handleHttpRequest: function(payload) {
// Extract payload data
var incidentSysId = payload.incidentSysId;
var category = payload.category;
var apiUrl = payload.apiUrl;
var username = payload.username;
var password = payload.password;

// Make the GET request with basic authentication and get the response body
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(apiUrl);
request.setHttpMethod("GET");
request.setBasicAuth(username, password);
var response = request.execute();
var responseBody = response.getBody();

// Parse the response body and extract solution details based on the category
var solutionData = JSON.parse(responseBody);
var matchingCategory = solutionData.find(function(item) {
return item.category === category;
});

if (matchingCategory) {
var firstSolution = matchingCategory.solutions[0];
var solNumber = firstSolution.sol_num;
var solName = firstSolution.sol_name;

// Use the solNumber and solName as needed (e.g., update fields in the incident record)
var incidentRecord = new GlideRecord('incident');
if (incidentRecord.get(incidentSysId)) {
var solutionString = 'Solution Number: ' + solNumber + ', Solution Name: ' + solName;

// Log information for debugging
gs.info('Updating field x_1061975_anakag_0_solutionsforcategory with value: ' + solutionString);

// Update the custom field with the concatenated string
incidentRecord.x_1061975_anakag_0_solutionsforcategory = solutionString;
incidentRecord.update();

// Log information for debugging
gs.info('Field updated successfully.');
} else {
// Log information for debugging
gs.info('Incident record not found with sys_id: ' + incidentSysId);
}
} else {
// Log information for debugging
gs.info('No matching category found for: ' + category);
}
},

type: 'FetchingSolutions'
};

 

This is the Business Rule

 

(function executeRule(current, previous /*null when async*/) {

// Check if the incident record is new or updated
if (current.isNewRecord() || current.update()) {

// Get the value from the Category field in the incident form
var category = current.category.toString(); // Adjust field name as needed

// Instantiate the FetchingSolutions script include
var fetchingSolutions = new FetchingSolutions();

// Specify the URL for the REST API GET request
var apiUrl = 'https://xyz.com/api/teams/category?facility=demo';

var password = '';

// Define the payload for the scheduled job
var payload = {
incidentSysId: current.sys_id.toString(),
category: category,
apiUrl: apiUrl,
username: '',
password: password
};

// Create a scheduled job to handle the HTTP request
var job = new GlideRecord('sysauto_script');
job.script = 'new FetchingSolutions().handleHttpRequest(payload);'; // Custom method to handle HTTP request
job.name = 'Run HTTP Request for Incident: ' + current.number;
job.run_type = 'once';
job.script_type = 'javascript';
job.script_include = 'FetchingSolutions'; // Adjust to your actual script include name
job.run_as = gs.getUserID();
job.active = true;
job.next_action = gs.nowDateTime();
job.insert();

// Optionally, you can log a message or update fields to indicate that the HTTP request is scheduled
gs.info('HTTP request scheduled for Incident: ' + current.number);
}

})(current, previous);

 

 

 

4 REPLIES 4

Amit Verma
Kilo Patron
Kilo Patron

Hi @Sidhartha Bose 

 

Can you please let me know what is the type of variable "x_1061975_anakag_0_solutionsforcategory"? Also, are you getting any log from gs.info('Updating field x_1061975_anakag_0_solutionsforcategory with value: ' + solutionString); ?

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

They are string type variables no im not getting any log from gs info

 

displayWhatever
Tera Contributor

 

Based on what i see in the script include, it appears that the authentication credentials (username and password) for the GET request (line 17) are set to null because the credentials are equal to the payload's username and password (line 11 & 12).

even with the password details its still showing the same error ive erased it to prevent unauthorised usage