Call script include on clcik of button on UI page.

Arohi1
Giga Contributor

I am calling REST API via script include. And this script include needs to be called via a onclick of a button which will be on UI page. 

I'm trying to to call script include by using ajax.

here are the details:

Please advice changes in code. 

UI page HTML:

<?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://developer.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.setRequestHeader("Accept","application/json");
//request.setRequestHeader('Content-Type','application/json');

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'

);

Thanks In Advance

1 ACCEPTED SOLUTION

 

can you try now.. if this will not work then can you try to create new script include and paste that code inside the script include , make sure client callable should be checked . 

 

function runCode()
{
alert('client part is working');
var ga = new GlideAjax('updatesetintegrationRESTMessageV2'); 
ga.addParam('sysparm_name', 'sampleRESTMessageV2');
ga.getXML(callback);
 
function callback(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer);
  }
  }

View solution in original post

4 REPLIES 4

Harsh Vardhan
Giga Patron

i doubt on your script include because of the comment line //initialize: function() { // }, 

if the script include has client callable then it should not come.

 

can you confirm the log details? are you getting some log ? 

Hi Harshvardhan, 

 

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?

 

can you try now.. if this will not work then can you try to create new script include and paste that code inside the script include , make sure client callable should be checked . 

 

function runCode()
{
alert('client part is working');
var ga = new GlideAjax('updatesetintegrationRESTMessageV2'); 
ga.addParam('sysparm_name', 'sampleRESTMessageV2');
ga.getXML(callback);
 
function callback(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer);
  }
  }

Arohi1
Giga Contributor

Thanks Harshvardhan, this worked for me.