on submit client script

charukamble
Tera Contributor

we want to get data on the form from the server and after populating that field form will submit, without using getxmlwait method
how to achieve this

1 REPLY 1

sourav1999
Mega Guru

You can achieve this by using GlideAjax in ServiceNow. GlideAjax is a JavaScript class that allows a client-side script to call a server-side script. Here are the steps:

1. Create a Script Include that will be called from the client-side script. This Script Include should be accessible from the client-side.

javascript
var GetServerData = Class.create();
GetServerData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getServerData: function() {
var sysId = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('table_name');
if (gr.get(sysId)) {
return gr.getValue('field_name');
}
},
type: 'GetServerData'
});


2. Call the Script Include from the client-side script using GlideAjax.

javascript
var ga = new GlideAjax('GetServerData');
ga.addParam('sysparm_name', 'getServerData');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.getXML(Answer);

function Answer(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('field_name', answer);
g_form.submit();
}


In the above code:

- 'GetServerData' is the name of the Script Include.
- 'getServerData' is the function in the Script Include.
- 'sysparm_sys_id' is the parameter passed to the server-side script.
- 'field_name' is the field you want to populate with the data from the server.
- The Answer function is the callback function that is called when the server-side script returns. It sets the value of the field and submits the form.

 

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - nowgpt.ai