Help with creating a json payload from catalog variables

Mollie V
Tera Guru

Hello community,

I'm a novice at best in scripting and mostly repurposing what is available in our platform as well as great help from this community. I have a task to create a complex json object from a short list of catalog variables. However, where I'm getting stuck in is getting the list collector variable to display as an array object within the json object.

 

These are my variables to start:

    User = reference type to sys_user table

    Module = reference to a u_module table

    Role = list collector pulling values from a u_role table and has 2 values: AP Inquiry Profile, AP Invoice Specialist Profile

 

This is the end result I need.

{
    "Username": "abc",
    "Module": "AP",
    "Role": [
           {
                  "RoleCode": "AP Inquiry Profile"
           },
           {
                  "RoleCode": "AP Invoice Specialist Profile"
           }
       ]
}

 

Here is my script:

var grRitm = new GlideRecordSecure('sc_req_item');
grRitm.addQuery('number','RITM0355236');
grRitm.query();

while (grRitm.next()) {
var key2;
var objValue = {};

for (key2 in grRitm.variables) {
var currentVar2 = grRitm.variables[key2];

if (key2 == 'ofc_requested_for') {
gs.info( key2 + ':' + currentVar2.getDisplayValue());
objValue.Username = currentVar2.user_name.toLowerCase();
}

if (key2 == 'ofc_erp_module') {
gs.info( key2 + ':' + currentVar2.getDisplayValue());
objValue.Module = currentVar2.getDisplayValue();
}

if (key2 == 'ofc_role_profile') {
gs.info( key2 + ':' + currentVar2.getDisplayValue().toString());
var list = currentVar2.getDisplayValue().toString();
var roleValue = [];
var rolCode = {};
var listSplit = list.split(',');
var len = listSplit.length;
gs.info('list length '+ len);

for(var i=0; i<len; i++){
gs.info(i + ' rolecode ' + listSplit[i].trim());
var rolValue = listSplit[i].trim();
rolCode.RoleCode = rolValue;
roleValue.push(rolCode);
gs.info('...in loop ' + rolValue);
}
objValue.Role = roleValue;
}


}

gs.info(JSON.stringify(objValue));

}

 

My script below is working until I get to building the role object where it's duplicating the last value in the list collector. It steps through in the "for loop" but it always ends up with just the last value. Below this is the result of my script.

{
"Username": "mlv@panduit.com",
"Module": "Accounts Payable",
"Role": [
       {
            "RoleCode": "AP Invoice Specialist Profile"
       },
       {
            "RoleCode": "AP Invoice Specialist Profile"
       }
   ]
}

 

 

I appreciate anyone's help and feedback on this. 

Thank you.

Mollie

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Mollie V ,

 

The issue with your script is that you are using the same object rolCode for all iterations of your loop, which causes the values to be overwritten each time. To fix this, you need to create a new object for each role inside the loop.

Please find the updated script below-

var grRitm = new GlideRecordSecure('sc_req_item');
grRitm.addQuery('number', 'RITM0355236');
grRitm.query();

while (grRitm.next()) {
    var key2;
    var objValue = {};

    for (key2 in grRitm.variables) {
        var currentVar2 = grRitm.variables[key2];

        if (key2 == 'ofc_requested_for') {
            gs.info(key2 + ':' + currentVar2.getDisplayValue());
            objValue.Username = currentVar2.user_name.toLowerCase();
        }

        if (key2 == 'ofc_erp_module') {
            gs.info(key2 + ':' + currentVar2.getDisplayValue());
            objValue.Module = currentVar2.getDisplayValue();
        }

        if (key2 == 'ofc_role_profile') {
            gs.info(key2 + ':' + currentVar2.getDisplayValue().toString());
            var list = currentVar2.getDisplayValue().toString();
            var roleValue = [];
            var listSplit = list.split(',');
            var len = listSplit.length;
            gs.info('list length ' + len);

            for (var i = 0; i < len; i++) {
                gs.info(i + ' rolecode ' + listSplit[i].trim());
                var rolValue = listSplit[i].trim();
                var rolCode = {}; // Create a new object for each role
                rolCode.RoleCode = rolValue;
                roleValue.push(rolCode);
                gs.info('...in loop ' + JSON.stringify(rolCode));
            }
            objValue.Role = roleValue;
        }
    }

    gs.info(JSON.stringify(objValue));
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Hi @Mollie V ,

 

The issue with your script is that you are using the same object rolCode for all iterations of your loop, which causes the values to be overwritten each time. To fix this, you need to create a new object for each role inside the loop.

Please find the updated script below-

var grRitm = new GlideRecordSecure('sc_req_item');
grRitm.addQuery('number', 'RITM0355236');
grRitm.query();

while (grRitm.next()) {
    var key2;
    var objValue = {};

    for (key2 in grRitm.variables) {
        var currentVar2 = grRitm.variables[key2];

        if (key2 == 'ofc_requested_for') {
            gs.info(key2 + ':' + currentVar2.getDisplayValue());
            objValue.Username = currentVar2.user_name.toLowerCase();
        }

        if (key2 == 'ofc_erp_module') {
            gs.info(key2 + ':' + currentVar2.getDisplayValue());
            objValue.Module = currentVar2.getDisplayValue();
        }

        if (key2 == 'ofc_role_profile') {
            gs.info(key2 + ':' + currentVar2.getDisplayValue().toString());
            var list = currentVar2.getDisplayValue().toString();
            var roleValue = [];
            var listSplit = list.split(',');
            var len = listSplit.length;
            gs.info('list length ' + len);

            for (var i = 0; i < len; i++) {
                gs.info(i + ' rolecode ' + listSplit[i].trim());
                var rolValue = listSplit[i].trim();
                var rolCode = {}; // Create a new object for each role
                rolCode.RoleCode = rolValue;
                roleValue.push(rolCode);
                gs.info('...in loop ' + JSON.stringify(rolCode));
            }
            objValue.Role = roleValue;
        }
    }

    gs.info(JSON.stringify(objValue));
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar

Thank you for your suggestion. Your solution worked!