- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 01:35 AM
Below on change client script is working fine on the form level, but its not working on the workspace level.
Can anyone help me on this please?
Client script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 04:34 AM - edited 01-22-2024 04:35 AM
Hi @XYD23
Follow below steps :
Step 1 : Create client callable script include
Name : TechTaskUtils
var TechTaskUtils = Class.create();
TechTaskUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/* Function */
getTechTaskDetails : function(){
/*1. Declare & initialize necessary variables/JSON */
var result = {
'title' :'',
'assigned_to' : '',
'due_date' :''
};
/*2.Get newValue (sys_id) from client script */
var getSysId = this.getParameter('sysparm_sysID');
/*3. Glide record on your table */
var grTech = new GlideRecord('u_tech_task');
grTech.addQuery('sys_id',getSysId);
grTech.query();
if(grTech.next()){
/*3.1 Set JSON object */
result.title = grTech.getValue('u_title');
result.assigned_to = grTech.getValue('u_assigned_to');
result.due_date = grTech.getValue('u_due_date');
}
/*4.Stringify result & return it */
return JSON.stringify(result);
},
type: 'TechTaskUtils'
});
Step 2 : Write onChange Client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
/*1. Call Script include using GlideAjax */
var ga = new GlideAjax('TechTaskUtils'); //script include name
ga.addParam('sysparm_name','getTechTaskDetails'); //function name within script include
ga.addParam('sysparm_sysID',newValue); //pass the value to script incldue
ga.getXMLAnswer(callBackFunction);
function callBackFunction(answer){
/* here we will get the result from script include & Parse the answer */
var result = JSON.parse(answer);
/* Set value on form */
g_form.setValue('u_title',result.title);
g_form.setValue('u_assigned_to',result.assigned_to);
g_form.setValue('u_due_date',result.due_date);
}
}
Hope this helps...!!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 04:14 AM
Hi @XYD23
Can you please put some debug statements in your On-Change script to validate whether it is being called or not ? If called, where it is failing exactly ?
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 04:17 AM
I mentioned it , it is working fine on form level, but not working on workspace level
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 04:13 AM
Hi @XYD23
You are using GlideRecord() in client script which is not recommended.
Instead use GlideAjax & Script include
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 04:15 AM
can you help me with that, I have less knowledge on script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 04:34 AM - edited 01-22-2024 04:35 AM
Hi @XYD23
Follow below steps :
Step 1 : Create client callable script include
Name : TechTaskUtils
var TechTaskUtils = Class.create();
TechTaskUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/* Function */
getTechTaskDetails : function(){
/*1. Declare & initialize necessary variables/JSON */
var result = {
'title' :'',
'assigned_to' : '',
'due_date' :''
};
/*2.Get newValue (sys_id) from client script */
var getSysId = this.getParameter('sysparm_sysID');
/*3. Glide record on your table */
var grTech = new GlideRecord('u_tech_task');
grTech.addQuery('sys_id',getSysId);
grTech.query();
if(grTech.next()){
/*3.1 Set JSON object */
result.title = grTech.getValue('u_title');
result.assigned_to = grTech.getValue('u_assigned_to');
result.due_date = grTech.getValue('u_due_date');
}
/*4.Stringify result & return it */
return JSON.stringify(result);
},
type: 'TechTaskUtils'
});
Step 2 : Write onChange Client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
/*1. Call Script include using GlideAjax */
var ga = new GlideAjax('TechTaskUtils'); //script include name
ga.addParam('sysparm_name','getTechTaskDetails'); //function name within script include
ga.addParam('sysparm_sysID',newValue); //pass the value to script incldue
ga.getXMLAnswer(callBackFunction);
function callBackFunction(answer){
/* here we will get the result from script include & Parse the answer */
var result = JSON.parse(answer);
/* Set value on form */
g_form.setValue('u_title',result.title);
g_form.setValue('u_assigned_to',result.assigned_to);
g_form.setValue('u_due_date',result.due_date);
}
}
Hope this helps...!!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates