- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2024 12:34 AM
I have Created a Onchange client script and calling Script Include to Achieve the Same, my full requirement is to populate company on the bases of caller if company is not available for the caller then fetch the value from CI 's company field but for now i am not getting the company on the bases of the caller.
Client scipt:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 05:05 AM
Let's go back to the basics: you are triggering the onChange Client Script on change of which field? If you add alerts like this to the script, do you see one or both when changing the value of that field?
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
alert('TEST: client script running');
if (isLoading || newValue === '') {
return;
}
var callerDetails = g_form.getValue("caller_id");
var ga = new GlideAjax("autopopulate_CallerInfo");
ga.addParam('sysparm_name', 'getCallerInfo');
ga.addParam('sysparm_value', callerDetails);
ga.getXML(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
alert('TEST: value received from server script = ' + answer);
g_form.setValue('company', answer);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 05:17 AM
Hi @Atharvabx ,
Try below code i just checked in my PDI its working,
CS:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var userSysid = g_form.getValue('caller_id');
var userName = new GlideAjax('getUserCompany');
userName.addParam('sysparm_name', 'getUser');
userName.addParam('sysparm_sysid', userSysid);
userName.getXML(callback);
//Type appropriate comment here, and begin script below
}
function callback(response) {
var ans = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('company', ans);
}
SI:
var getUserCompany = Class.create();
getUserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUser: function(){
var sys_id =this.getParameter('sysparm_sysid');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',sys_id);
gr.query();
if(gr.next()){
var company = gr.company;
}
gs.info('company is ' + company);
return company;
},
type: 'getUserCompany'
});
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2024 05:37 AM - edited ‎02-26-2024 09:06 AM
There is one issue with each script. In the client script, you need to include the function name in single or double quotes as neglecting to do so results in getCallerInfo undefined, as you should have noticed when testing.
ga.addParam('sysparm_name', 'getCallerInfo');
In the Script Include, since the company field on the sys_user table is the type of reference, and you want to populate the company field on the incident, which is also the type of reference, just get the value (sys_id) not the displayValue.
var comp=x.getValue('company');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2024 06:20 AM
Tried with the same but still no luck 😞
CS:-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2024 09:17 AM - edited ‎02-26-2024 09:38 AM
Hi @Atharvabx ,
You have added flower braces at wrong place I have corrected it please check,
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var callerDetails = g_form.getValue('caller_id');
var ga = new GlideAjax('autopopulate_CallerInfo');
ga.addParam('sysparm_name', 'getCallerInfo');
ga.addParam('sysparm_value', callerDetails);
ga.getXML(callback);
}
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('company', answer);
}
if the company field which you are setting through client script is reference field then try the below code in script include ,
var autopopulate_CallerInfo = Class.create();
autopopulate_CallerInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCallerInfo: function() {
var sysIdOfCaller = this.getParameter('sysparm_value');
var x = new GlideRecord('sys_user');
x.addQuery('sys_id', sysIdOfCaller);
x.query();
if (x.next()) {
var comp=x.comapny;
}
return comp;
},
type: 'autopopulate_CallerInfo'
});
if its a string field which you are setting then replace "var comp=x.comapny;" with "var comp =x.company.getDisplayValue();"
Please try and let me know if it still doesn't work,
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 02:50 AM
Tried the Same code on PDI but still it not working @Brad Bowman Appreciate you Prompt Reply.