How to write catalog client script for populating caller email and phone number to single line text
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2024 08:42 AM - edited 02-08-2024 08:46 AM
Hi Community,
How to write catalog client script for populating caller 'email and phone' number to single line text in catalog item.
I have tried Auto-populate option in variable but it's working only email or phone based on reference selecting.
and tried below step also but it's not works.
javascript:gs.getUser().getRecord().getValue('phone_number');//phone number
javascript:gs.getUser().getRecord().getValue('email');//email
My requirement is populate both caller email and phone number in single line text field while catalog item opened in service portal.
Please suggest..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2024 08:57 AM - edited 02-08-2024 08:59 AM
Hi @ashok17, You can create a script include and client script to achieve this.
Script Include: Client callable TRUE
var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var obj = {};
var usr = new GlideRecord('sys_user');
usr.get(this.getParameter('sysparm_user_id'));
obj.manager = usr.getValue('manager');
obj.email = usr.getValue('email');
obj.number = usr.getValue('mobile_phone');
var json = new JSON();
var data = json.encode(obj); //JSON formatted string
return data;
},
type: 'GetUserDetails'
});
Client script: OnChange
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if (newValue === '') {
g_form.setValue('yourSingleLineTextVariable', '');
}
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_user_id', newValue);
ga.getXML(CallBack);
function CallBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON();
var mobileNumber = answer.number;
var emailId = answer.email;
g_form.setValue('yourSingleLineTextVariable', mobileNumber + ' - ' + emailId);
}
}
Regards,
Sunil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 02:12 AM
Hi Sunil,
Thanks for my response and above code is not works as per my requirement.
I have modified script as per my requirement and I am trying to populate if user select email check box then caller email should be populate single line text field and if user select phone check box then caller phone should be populate single line text field and user selects both email and phone then email, phone should be populate single line text field:
Please suggest for my requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 03:31 AM
Hi @ashok17, as I understand there are two onChange actions (i.e., onChange of Mobile number checkbox and onChange of EmailId checkbox). In this case, you would need two onChange Client Scripts one on Mobile checkbox and the other on Email checkbox. You can try the below client scripts.
//Onchange of MobileNumber CheckBox
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 'Yes') {
var userName = g_form.getValue('who_is_impacted');
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_user_id', userName);
ga.getXML(CallBack);
function CallBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON();
var mobileNumber = answer.number;
var emailId = answer.email;
var isEmailChecked = false;
if (g_form.getValue('emailIdVariableCheck') == 'Yes') {
isEmailChecked = true;
}
if (isEmailChecked == true) {
g_form.setValue('yourSingleLineTextvariable', +mobileNumber + ' - ' + emailId);
} else {
g_form.setValue('yourSingleLineTextvariable', +mobileNumber);
}
}
} else {
g_form.clearValue('yourSingleLineTextvariable');
if (isEmailChecked == true) {
g_form.setValue('yourSingleLineTextvariable', +emailId);
} else {
g_form.clearValue('yourSingleLineTextvariable');
}
}
}
//Onchange of EmailId CheckBox
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 'Yes') {
var userName = g_form.getValue('who_is_impacted');
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_user_id', userName);
ga.getXML(CallBack);
function CallBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON();
var mobileNumber = answer.number;
var emailId = answer.email;
var isMobileNumberChecked = false;
if (g_form.getValue('mobileNumberVariableCheck') == 'Yes') {
isMobileNumberChecked = true;
}
if (isMobileNumberChecked == true) {
g_form.setValue('yourSingleLineTextvariable', +mobileNumber + ' - ' + emailId);
} else {
g_form.setValue('yourSingleLineTextvariable', +emailId);
}
}
} else {
g_form.clearValue('yourSingleLineTextvariable');
if (isMobileNumberChecked == true) {
g_form.setValue('yourSingleLineTextvariable', +mobileNumber);
} else {
g_form.clearValue('yourSingleLineTextvariable');
}
}
}
Regards,
Sunil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2024 08:59 AM