
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 09:09 AM
I want show the user first name and mobile number when caller field changes. I am trying to use a client script to call a script include to do this. I did set Client callable to true .But I am getting null value.
client script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 05:19 PM
When you add Parameters to a Client Script and retrieve them in the Script Include, the name must start with sysparm_, not sysparam_ so your Client Script will look like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
g_form.setReadOnly('u_custom_mobile', true);
g_form.setReadOnly('u_first_name', true);
return;
}
alert('Script Runs '+g_form.getValue('caller_id'));
var gx = new GlideAjax('userDetails');
gx.addParam('sysparm_name', 'callernameandphone');
gx.addParam('sysparm_user', g_form.getValue('caller_id'));
gx.getXML(getData);
function getData(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);
if(answer) {
var temValue = answer.toString().split('|');
g_form.setValue('u_custom_mobile', temValue[0]);
g_form.setValue('u_first_name', temValue[1]);
}
}
}
and the Script Include:
var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
callernameandphone: function() {
var usrId = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
if (gr.get(usrId)) {
return gr.getValue('mobile_phone') + '|' + gr.getValue('first_name');
}
},
type: 'userDetails'
});
You can further troubleshoot this by adding more alerts to the Client Script and gs.info lines to the Script Include to confirm it's running, and see the parameter values and how far it is getting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 03:09 PM
Hello @Community Alums ,
Try updating these two lines in your code, as this might resolve the issue:
gx.addParam('sysparm_name', 'callernameandphone');
gx.addParam('sysparm_user', g_form.getValue('caller_id'));
Additionally, check if the Script Include is set as client-callable, as this could also be affecting the script's execution.
If this solution helps you then, mark it as accepted solution ✔️ and give thumbs up 👍!
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 03:25 PM
Instead of retrieving the information, your users could just hover over the "i" icon of the Caller field to view extra information. And wouldn't the first name show up in the Caller field? Unless you've changed the Display field for the User table.
Or you can add the dot-walked fields onto the form itself and not worry about scripting and saving redundant info in new custom fields. Does it make sense to save the first name and phone number in each Incident record? Probably not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 05:19 PM
When you add Parameters to a Client Script and retrieve them in the Script Include, the name must start with sysparm_, not sysparam_ so your Client Script will look like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
g_form.setReadOnly('u_custom_mobile', true);
g_form.setReadOnly('u_first_name', true);
return;
}
alert('Script Runs '+g_form.getValue('caller_id'));
var gx = new GlideAjax('userDetails');
gx.addParam('sysparm_name', 'callernameandphone');
gx.addParam('sysparm_user', g_form.getValue('caller_id'));
gx.getXML(getData);
function getData(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);
if(answer) {
var temValue = answer.toString().split('|');
g_form.setValue('u_custom_mobile', temValue[0]);
g_form.setValue('u_first_name', temValue[1]);
}
}
}
and the Script Include:
var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
callernameandphone: function() {
var usrId = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
if (gr.get(usrId)) {
return gr.getValue('mobile_phone') + '|' + gr.getValue('first_name');
}
},
type: 'userDetails'
});
You can further troubleshoot this by adding more alerts to the Client Script and gs.info lines to the Script Include to confirm it's running, and see the parameter values and how far it is getting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 10:37 PM - edited 10-03-2024 10:57 PM
To be clear, the "name" parameter must be "sysparm_name" (which is why it is not working) but the others can be anything: I just used "scoobydoo_id" as an example and it works fine. Although it is a good idea to standardize on "sysparm_" as a prefix. Just like any standardized naming conventions your organization should have throughout the platform. Hint, the OP is using terrible names for the Class (Script Include) and function ("callernameandphone").
And using strings with a separator to return the data is not the best way to do it. Take a look at this post for an alternative method: TNT: Returning Data from GlideAjax Calls