How to populate Requestor Details in variables of MRVS?

rkreddy
Giga Expert

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.

1 ACCEPTED SOLUTION

@rkreddy 

Done and working fine in native

please check below output

find_real_file.png

Please mark my response as correct and close the question.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

58 REPLIES 58

Hi @Ankur Bawiskar ,

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

}

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar ,

Yes it is showing the values present in the variables, i.e, the details of requestor.

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar ,

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);
            }
        }