Call script include on click of button an Ui page.

Arohi1
Giga Contributor

I need to call API, which i am calling from Script include and this script include should be called on click of a button on an UI page. I am sure I am missing something to achieve this. I tried to put code in client script of Ui page, still not working. Please advice!

UI page HTML code:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:ui_form>

<j2:set var="jvar_task_id" value="${sysparm_task_id}" />
<input type="hidden" id="task_id" name="task_id" value="$[jvar_task_id]" />
<table border="0" width="100%">
<tr>
<td valign="top" width="100%" height="100%">
<span style="font-size: 11pt">${gs.getMessage("Enter a 'From Instance' and then a ' To Instance', then click Ok to continue.")}</span>
</td>
</tr>

<table>
<tr>
<td id="label.Finst" type="string" choice="0" class="label" height="23px" nowrap="true">
<label for="Finst" dir="ltr" class=" control-label" onclick="">
<span class="label-text" data-html="false">From Instance</span>
</label>
</td>
<td style=";" nowrap="true">
<select name="Finst" id="Finst">
<option value="">--None--</option>
<option value="test">https://test.service-now.com</option>
<option value="Test">https://Test.service-now.com</option>
</select>
</td>
</tr>
</table>

<table>
<tr>
<td id="label.Tinst" type="string" choice="0" class="label" height="28px" nowrap="true">
<label for="Tinst" dir="ltr" class=" control-label" onclick="">
<span class="label-text" data-html="false">To Instance</span>
</label>
</td>
<td style=";" nowrap="true">
<select name="Tinst" id="Tinst">
<option value="">--None--</option>
<option value="test">https://test.service-now.com</option>
<option value="Test">https://Test.service-now.com</option>
</select>
</td>
</tr>
</table>
<tr>
<td>

<button type="button" onclick="runCode()">Execution</button>
</td>
</tr>
</table>
</g:ui_form>
</j:jelly>

 

Client Script:

function runCode()
{
var ga = new GlideAjax('updatesetintegrationRESTMessageV2');
ga.addParam('sysparm_name','sampleRESTMessageV2');
ga.getXMLWait();
var result=ga.getAnswer();
alert('test');
}

Script Include:

var updatesetintegrationRESTMessageV2 = Class.create();
updatesetintegrationRESTMessageV2.prototype = Object.extendsObject(AbstractAjaxProcessor,{
//initialize: function() {
// },

sampleRESTMessageV2:function() {
gs.log('Test1');
try{
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://dev.service-now.com/api/now/v2/table/sys_update_set');
request.setHttpMethod('GET');
gs.log('Test2');

var user = 'admin';
var password = 'admin';

request.setBasicAuth(user,password);

request.setQueryParameter("sysparm_query","active=true");
var response = request.execute();
//gs.log(response.getBody());
gs.log('Test3');
var responseBody = response.getBody(); //fetches JSON response

var response= JSON.parse(responseBody);
gs.log(response);

httpResponseStatus = response.getStatusCode();

gs.print(" http response status_code: " + httpResponseStatus);
}
catch(ex){
var message = ex.getMessage();
gs.print(message);
}

}
}
//type: 'updatesetintegrationRESTMessageV2'

);

 

Please suggest changes in scripts. 

thanks in advance.

 

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

i believe this is a duplicate thread kindly close this thread as well by marking the answer correct. 

 

https://community.servicenow.com/community?id=community_question&sys_id=3103fc58db64bb8854250b55ca96...

View solution in original post

3 REPLIES 3

Harsh Vardhan
Giga Patron

are you getting log in your script include? 

please validate the status code 

use gs.log() instead of gs.print()

First of all I am not sure if script include is getting called because I am not getting any logs. The client script part of UI page, is that correct?

Harsh Vardhan
Giga Patron

i believe this is a duplicate thread kindly close this thread as well by marking the answer correct. 

 

https://community.servicenow.com/community?id=community_question&sys_id=3103fc58db64bb8854250b55ca96...