Updating an incident form to display data obtained from a rest api get request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2023 08:19 AM
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
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2023 09:00 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 02:01 AM
They are string type variables no im not getting any log from gs info
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2023 09:27 PM
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 02:00 AM
even with the password details its still showing the same error ive erased it to prevent unauthorised usage