- 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-17-2021 09:40 AM
Hi
I have tested the script by placing alerts in different places. I found that for loop is not executing, because every variable defined in it is showing null values.
alert(parser) is showing the details of user in Requested for field. Till this it is working, but if we place alerts for variables inside for loop, they are showing null values(empty braces).
alert(obj1), alert(newParser) these are showing null values.
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 == '') {
var selfDetails1 = [];
var details1 = '';
var obj2 = {};
obj2["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
obj2["email_address"] = g_form.getValue('u_email');
obj2["department"] = g_form.getValue('u_department');
obj2["u_work_location"] = g_form.getValue('work_location');
obj2["telephone_number"] = g_form.getValue('u_telephone');
selfDetails1.push(obj2);
details1 = JSON.stringify(selfDetails1);
var toRemove = g_form.getDisplayBox('u_requested_for').value;
//alert(toRemove);
var finalArr = [];
var parser = JSON.parse(JSON.stringify(details1));
alert (parser);
for (var i = 0; i < parser.length; i++) {
var obj1 = parser[i];
alert(obj1);
var newParser = JSON.stringify(obj1);
//alert(newParser);
if (newParser.attendee_name.toString() != toRemove.toString()) {
finalArr.push(obj1);
}
}
alert('new JSON' + JSON.stringify(finalArr));
if (parser.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-18-2021 10:26 PM
Hi,
Did you check this json has something in it?
var obj2 = {};
obj2["attendee_name"] = g_form.getDisplayBox('u_requested_for').value;
obj2["email_address"] = g_form.getValue('u_email');
obj2["department"] = g_form.getValue('u_department');
obj2["u_work_location"] = g_form.getValue('work_location');
obj2["telephone_number"] = g_form.getValue('u_telephone');
selfDetails1.push(obj2);
details1 = JSON.stringify(selfDetails1);
alert(details1);
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-18-2021 10:36 PM
Hi
Yes it is showing the values present in the variables, i.e, the details of requestor.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-18-2021 10:52 PM
Hi,
if that alert is giving the JSON then you should be able to iterate over it
but now the array has only 1 JSON object which you set in else
Sorry but I am not getting clear understanding but I believe I have provided you the guidance on how to remove json object from array of json objects
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-18-2021 11:16 PM
Hi
But here obj1 is not getting any values, even though the parser has values in it. As obj1 is having null values, all the below lines to it are not executing.
for (var i = 0; i < parser.length; i++) {
var obj1 = parser[i];
alert(obj1);
var newParser = JSON.stringify(obj1);
//alert(newParser);
if (newParser.attendee_name.toString() != toRemove.toString()) {
finalArr.push(obj1);
}
}