how do I get an array from the API in the script include and call it to the catalog client script

Topan Kamil
Tera Contributor

hello everyone, I am just learning ServiceNow, here I am trying to integrate Jira with ServiceNow using the API, but I have a problem in the script include here I am confused (do not understand) how to make the script include can make data from the array that comes from the API into a sting so that it can be called in the catalog client script, please help to explain whether my script call is wrong because the portal has read the data, but the results are still not correct, what should appear in the customer column is only the name, for example, topan. kamil.

 

thank you for your help, all your explanations will be very helpful for me and I will be very happy and hopefully if there are people who find the same case as me, they can be helped by this question.

1 ACCEPTED SOLUTION

@Topan Kamil 

sample script will be like this

function onLoad() {
	// var arruj=[];
	var ga = new GlideAjax('getUserJira');
	ga.addParam('sysparm_name', "GetUserJiraUppk");
	ga.getXML(HelloWorldParse);

	function HelloWorldParse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var parsedData = JSON.parse(answer);

		var nameArr = [];

		for(var i=0;i<parsedData.length;i++)
			nameArr.push(parsedData[i].name.toString());

		g_form.clearOptions('customer');

		for(var j in nameArr)		
			g_form.addOption('customer', nameArr[j], nameArr[j]);

	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@Topan Kamil 

can you share the scripts here along with images

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar Sorry I forgot the script, please help and guidance.

============================================================

Script Include 

var getUserJira = Class.create();
getUserJira.prototype = Object.extendsObject(AbstractAjaxProcessor, {

GetUserJiraUppk: function() {
var r = new sn_ws.RESTMessageV2('REST Jira UPPK', 'Default GET');
r.setEndpoint('http://localhost:8080/rest/api/2/user/search?username=.');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var ResponseData = JSON.parse(responseBody);
return response.getBody();
},

type: 'getUserJira'
});

==============================================================

 

Catalog Client Script

function onLoad() {
// var arruj=[];
var ga = new GlideAjax('getUserJira');
ga.addParam('sysparm_name', "GetUserJiraUppk");
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var uj = answer;
alert(uj);
g_form.clearOptions('customer');
g_form.addOption('customer', uj, uj);

}
}

@Topan Kamil 

what came in alert for answer?

what came in the response body? share the json

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

1. alert for answer 

2. share the JSON you can access this link http://jsonblob.com/1065855205463310336 

or 

[{"self":"http://localhost:8080/rest/api/2/user?username=kamil","key":"JIRAUSER10100","name":"kamil","emailAddress":"topankamil82@gmail.com","avatarUrls":{"48x48":"https://www.gravatar.com/avatar/167c39ddf215bad036c4ef73d1e666f5?d=mm&s=48","24x24":"https://www.gravatar.com/avatar/167c39ddf215bad036c4ef73d1e666f5?d=mm&s=24","16x16":"https://www.gravatar.com/avatar/167c39ddf215bad036c4ef73d1e666f5?d=mm&s=16","32x32":"https://www.gravatar.com/avatar/167c39ddf215bad036c4ef73d1e666f5?d=mm&s=32"},"displayName":"kamilberjasa","active":true,"deleted":false,"timeZone":"Asia/Bangkok","locale":"en_ID"},{"self":"http://localhost:8080/rest/api/2/user?username=topan.kamil","key":"JIRAUSER10000","name":"topan.kamil","emailAddress":"topan.kamil@berjasa.info","avatarUrls":{"48x48":"https://www.gravatar.com/avatar/1fe0cb64a680c73d8c76a630c43534c8?d=mm&s=48","24x24":"https://www.gravatar.com/avatar/1fe0cb64a680c73d8c76a630c43534c8?d=mm&s=24","16x16":"https://www.gravatar.com/avatar/1fe0cb64a680c73d8c76a630c43534c8?d=mm&s=16","32x32":"https://www.gravatar.com/avatar/1fe0cb64a680c73d8c76a630c43534c8?d=mm&s=32"},"displayName":"topan.kamil@berjasa.info","active":true,"deleted":false,"timeZone":"Asia/Bangkok","locale":"en_ID"}]