Skip to content

Commit db65e15

Browse files
authored
Pass multiple values from server to client side in an object (#1558)
1 parent b190a7a commit db65e15

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Use the script provided in script_include.js and script.js to set fetch multiple values from server to client side by passing an
2+
object from server to the client side and setting values on your form. This can be used to pass multiple parameters from server to
3+
client side.
4+
5+
Use Case:
6+
Consider you have a reference field on your form referring to "sn_si_incident" and you need to set Severity, state and assigned to
7+
onChange of the reference field.
8+
9+
Solution:
10+
Create a client callable script include as mentioned in script_include.js and pass the required values to your client script.
11+
Then use the onChange client script in script.js to set values on the form.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//onChange client script
2+
3+
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
4+
if (isLoading || newValue === '') {
5+
6+
return;
7+
}
8+
9+
10+
//Type appropriate comment here, and begin script below
11+
var ga = new GlideAjax('getSIRDetails'); // calling script include
12+
ga.addParam('sysparm_name', 'getDetails');
13+
ga.addParam('sysparm_sir', newValue); //passing newValue to the script include
14+
ga.getXMLAnswer(callBackFunction);
15+
16+
17+
function callBackFunction(response) {
18+
var ans = JSON.parse(response);
19+
g_form.setValue('severity', ans.severity); // setting values from the obj to appropriate fields
20+
g_form.setValue('soc_sir_state', ans.state);
21+
g_form.setValue('soc_sir_assigned_to', ans.assignedto);
22+
23+
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//Client callable script include
2+
var getSIRDetails = Class.create();
3+
getSIRDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
4+
getDetails: function() {
5+
var sir = this.getParameter('sysparm_sir'); //getting the newValue of Security Inc from onChange client script
6+
var obj = {}; //declare an object
7+
var gr = new GlideRecord('sn_si_incident');
8+
gr.addQuery('sys_id', sir); //Query to security incident table with the newValue
9+
gr.query();
10+
if (gr.next()) {
11+
obj.severity = gr.severity.getDisplayValue(); //Setting values in the obj
12+
obj.state = gr.state.getDisplayValue();
13+
obj.assignedto = gr.assigned_to.getDisplayValue();
14+
}
15+
return JSON.stringify(obj); //passing the object to client script
16+
},
17+
type: 'getSIRDetails'
18+
});

0 commit comments

Comments
 (0)