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

Thank you very much @Ankur Bawiskar 

Topan Kamil
Tera Contributor

@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);

}
}