Skip to content

Pass multiple values from server to client side in an object #1558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Client Scripts/Set Severity, state & assigned to/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Use the script provided in script_include.js and script.js to set fetch multiple values from server to client side by passing an
object from server to the client side and setting values on your form. This can be used to pass multiple parameters from server to
client side.

Use Case:
Consider you have a reference field on your form referring to "sn_si_incident" and you need to set Severity, state and assigned to
onChange of the reference field.

Solution:
Create a client callable script include as mentioned in script_include.js and pass the required values to your client script.
Then use the onChange client script in script.js to set values on the form.
25 changes: 25 additions & 0 deletions Client Scripts/Set Severity, state & assigned to/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//onChange client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {

return;
}


//Type appropriate comment here, and begin script below
var ga = new GlideAjax('getSIRDetails'); // calling script include
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_sir', newValue); //passing newValue to the script include
ga.getXMLAnswer(callBackFunction);


function callBackFunction(response) {
var ans = JSON.parse(response);
g_form.setValue('severity', ans.severity); // setting values from the obj to appropriate fields
g_form.setValue('soc_sir_state', ans.state);
g_form.setValue('soc_sir_assigned_to', ans.assignedto);


}
}
18 changes: 18 additions & 0 deletions Client Scripts/Set Severity, state & assigned to/script_include.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//Client callable script include
var getSIRDetails = Class.create();
getSIRDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDetails: function() {
var sir = this.getParameter('sysparm_sir'); //getting the newValue of Security Inc from onChange client script
var obj = {}; //declare an object
var gr = new GlideRecord('sn_si_incident');
gr.addQuery('sys_id', sir); //Query to security incident table with the newValue
gr.query();
if (gr.next()) {
obj.severity = gr.severity.getDisplayValue(); //Setting values in the obj
obj.state = gr.state.getDisplayValue();
obj.assignedto = gr.assigned_to.getDisplayValue();
}
return JSON.stringify(obj); //passing the object to client script
},
type: 'getSIRDetails'
});
Loading