- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2021 02:54 AM
Hi All,
Hope everyone is doing good.
I am working on a catalog form with Variables,
Name (reference to User table), Department, Work Location, Telephone, Email. When value changes in Name variable, all the other variables get populated automatically if the user in Name variable has that values exist. If not, we are allowing users to manually enter values in Department, work location, telephone, email variables.
We have a MRVS with the same variables as mentioned above.
Now, We have a checkbox variable on the same form with name "Self". When user checks it to true, all the values in the Name, Department, work location, telephone, email should be copied to variables in MRVS. If he unchecks, the values should get cleared (only the row should get deleted which is populated when the variable is checked). This should work on change of both Name and Self variables.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-21-2021 06:17 AM
Done and working fine in native
please check below output
Please mark my response as correct and close the question.
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-22-2022 08:48 AM
Hi
I tried replicating your scripts to populate the asset variables to no avail, I'd appreciate a review of the scripts below;
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.clearValue('assetlist'); // give name of MRVS variable set here
}
if (oldValue != newValue) {
var ga = new GlideAjax('populateAssetfromList');
ga.addParam('sysparm_name', 'listcollector');
ga.addParam('sysparm_user_info', g_form.getValue('requestor')); // give here the requestor variable name
ga.getXML(listcolleValues);
function listcolleValues(response) {
var val = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('assetlist', val); // give name of MRVS variable set here
}
}
}
Script include
var populateAssetfromList = Class.create();
populateAssetfromList.prototype = Object.extendsObject(AbstractAjaxProcessor, {
listcollector: function() {
var listValuename = [];
var userInfo = this.getParameter('sysparm_user_info');
var query = 'sys_idIN' + userInfo;
if (userInfo) {
var gr = new GlideRecord('alm_asset');
gr.addEncodedQuery(gs.getMessage('assigned_to={0}', userInfo));
gr.query();
if (gr.next()) {
listValuename.push({
"asset_tag": gr.getValue('asset_assettag'),
"model": gr.getValue('asset_model'),
"model_category": gr.getValue('asset_category'),
"serial_number": gr.getValue('asset_serial'),
"display_name": gr.getValue('asset_name')
});
}
}
gs.info('ARB JSON' + JSON.stringify(listValuename));
return JSON.stringify(listValuename);
},
type: 'populateAssetfromList'
});
Variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2021 10:35 PM
Hi
Sure I will do like you said. Could you please tell me where should be fine to add alerts?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2021 10:47 PM
Hi,
in for loop
try updating as this
if(newParser.attendee_name.toString() != toRemove.toString()){
finalArr.push(obj1);
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2021 11:20 PM
Hi
I have written two Onchange scripts on two variables, on Self (checkbox) and on Name (reference field). And changed as you said above, but deleting all rows.
onChange script on Name field:
function onChange(control, oldValue, newValue, isLoading) {
var self = g_form.getValue('self');
var selfDetails = [];
var details = '';
var obj = {};
obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
obj["email_address"] = g_form.getValue('u_email');
obj["department"] = g_form.getValue('u_department');
obj["u_work_location"] = g_form.getValue('work_location');
obj["telephone_number"] = g_form.getValue('u_telephone');
selfDetails.push(obj);
details = JSON.stringify(selfDetails);
if (newValue != '' && self == 'true') {
g_form.setValue('attendees_information', details);
} else if (newValue == '' || self == 'false') {
obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
obj["email_address"] = g_form.getValue('u_email');
obj["department"] = g_form.getValue('u_department');
obj["u_work_location"] = g_form.getValue('work_location');
obj["telephone_number"] = g_form.getValue('u_telephone');
selfDetails.push(obj);
details = JSON.stringify(selfDetails);
var toRemove = g_form.getDisplayBox('u_requested_for').value;
var finalArr = [];
var parser = JSON.parse(details);
for (var i = 0; i < parser.length; i++) {
var obj1 = parser[i];
alert(obj1);
var newParser = JSON.parse(JSON.stringify(obj1));
if (newParser.attendee_name.toString() != toRemove.toString()) {
finalArr.push(obj1);
}
}
g_form.setValue('attendees_information', JSON.stringify(finalArr));
}
}
onChange script on Self variable:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var user = g_form.getDisplayBox('u_requested_for').value;
var selfDetails = [];
var details = '';
var obj = {};
obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
obj["email_address"] = g_form.getValue('u_email');
obj["department"] = g_form.getValue('u_department');
obj["u_work_location"] = g_form.getValue('work_location');
obj["telephone_number"] = g_form.getValue('u_telephone');
selfDetails.push(obj);
details = JSON.stringify(selfDetails);
if (newValue == 'true' & user != '') {
g_form.setValue('attendees_information', details);
} else if (newValue == 'false' || user == '') {
obj["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
obj["email_address"] = g_form.getValue('u_email');
obj["department"] = g_form.getValue('u_department');
obj["u_work_location"] = g_form.getValue('work_location');
obj["telephone_number"] = g_form.getValue('u_telephone');
selfDetails.push(obj);
details = JSON.stringify(selfDetails);
var toRemove = g_form.getDisplayBox('u_requested_for').value;
var finalArr = [];
var parser = JSON.parse(details);
for (var i = 0; i < parser.length; i++) {
var obj1 = parser[i];
alert(obj1);
var newParser = JSON.parse(JSON.stringify(obj1));
if (newParser.attendee_name.toString() != toRemove.toString()) {
finalArr.push(obj1);
}
}
g_form.setValue('attendees_information', JSON.stringify(finalArr));
}
//Type appropriate comment here, and begin script below
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2021 11:33 PM
Hi,
try adding alerts and debug
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader