Autopopulate variable set and use it in multiple record producer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi,
I have 4 string variables and 4 reference variables in a variable set.
So If i tag this variable set to a record producer,
1. Based on the requested for or subject person depends on the record producer, it should auto populate values from the user profile.
I have tried below client script but I am doing mistake somewhere Could you please help?.. It is an onload catalog client script on variable set.
/
script:
function onLoad() {
g_form.setVisible('opco', false);
g_form.setMandatory('opco', false);
g_form.setVisible('opco_', false);
g_form.setMandatory('opco_', false);
g_form.setVisible('region', false);
g_form.setMandatory('region', false);
g_form.setVisible('work_country', false);
g_form.setMandatory('work_country', false);
g_form.setVisible('worker_type', false);
g_form.setMandatory('worker_type', false);
function getUrlParameter(name) {
var regex = new RegExp('[?&]' + name + '=([^&#]*)');
var results = regex.exec(window.location.href);
return results ? decodeURIComponent(results[1]) : null;
}
var rpId = getUrlParameter('sys_id');
alert('Step 2 - rpId: ' + rpId);
if (!rpId) return;
var rpMapping = {
'266e7b6787d543d44bd365370cbb3500': 'subject_person',
'RP_SYS_ID_2': 'u_requested_for',
'RP_SYS_ID_3': 'u_employee'
};
var refField = rpMapping[rpId];
alert('Step 3 - refField: ' + refField);
if (!refField) return;
var control = g_form.getControl(refField);
alert('Step 4 - control found: ' + (control ? 'YES' : 'NO'));
if (!control) return;
control.addEventListener('change', function() {
var userId = g_form.getValue(refField);
alert('Step 5 - userId on change: ' + userId);
if (!userId) return;
var ga = new GlideAjax('DHR_HRProfileLookup');
ga.addParam('sysparm_name', 'getHRProfileData');
ga.addParam('sysparm_user_id', userId);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
alert('Step 6 - raw answer: ' + answer);
if (!answer) return;
var data = JSON.parse(answer);
alert('Step 7 - parsed data: ' + JSON.stringify(data));
if (data.opco) {
g_form.setValue('opco', data.opco);
g_form.setVisible('opco', false);
g_form.setMandatory('opco', false);
g_form.setVisible('opco_', false);
g_form.setMandatory('opco_', false);
} else {
g_form.setValue('opco', '');
g_form.setVisible('opco', false);
g_form.setVisible('opco_', true);
g_form.setMandatory('opco_', true);
}
setOrReveal('region', data.region);
setOrReveal('work_country', data.work_country);
setOrReveal('worker_type', data.worker_type);
});
});
function setOrReveal(field, value) {
if (value) {
g_form.setValue(field, value);
g_form.setVisible(field, false);
g_form.setMandatory(field, false);
} else {
g_form.setValue(field, '');
g_form.setVisible(field, true);
g_form.setMandatory(field, true);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3m ago
Hi @Abishek1998
Don’t use window.location.href, getControl(), or addEventListener().
Create an OnChange Catalog Client Script for each user/reference variable, such as subject_person, u_requested_for, u_employee.
When the user is selected, call your DHR_HRProfileLookup Script Include with GlideAjax.
Populate the Variable Set fields from the returned profile data.
Keep the logic reusable, instead of mapping Record Producer sys_ids inside the Variable Set.
Main problem in your current script: you’re trying to dynamically listen for changes using the HTML control instead of using ServiceNow’s native OnChange Catalog Client Script.
This helps other users find accurate and useful information more easily
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
46 seconds ago
Which, if any, alerts are you receiving? Which is the next one you are expecting but not receiving? Have you ensured the isolate script box is unchecked?
