Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script Include to Return Multiple Objects to Catalog Client Script

triciav
Kilo Sage

I need to get all the Entitlements for a user and return all the applications and the access they have into a string field 

example:

Application: App1        Access: View

Application: App2        Access: Admin

I am getting back the values but I do not know how to combine them in the client script and then pass all the results into 1 string field?

 

resSTR Application Compose Access Level Admin Rights
resSTR Application Test Application Access Level View Only
resSTR Application Compass Access Level View Only

triciav_0-1680732415113.png

 

 

 

 

 getAllEntitlements: function() {

        var resultObj = {};
        var user = this.getParameter('sysparm_user');
        var app = this.getParameter('sysparm_app');
        var allApps = [];
        var grApp = new GlideRecord('u_business_application_entitlements');
        grApp.addQuery('u_user', user);
        grApp.query();
        while (grApp.next()) {
            resultObj = {
                "application": grApp.u_application.getDisplayValue().toString(),
                "access": grApp.u_access_level.getDisplayValue().toString()
            };
			
            var json = new global.JSON();
			gs.info("TRICIA DATA " + json.encode(resultObj));

return json.encode(resultObj);
          //  return json.encode(resultObj);

            
        }
    },

 

TRICIA DATA {"access":"Admin Rights","application":"Compose"}

 

Catalog CLient Script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.hideFieldMsg('current_applications');
    var gInv = g_form.getValue('access_type');
    alert(gInv);
    if (gInv == 'revoke_access') {
        var getUserIn = new GlideAjax('GetEntitlementsUtil');
        getUserIn.addParam('sysparm_name', 'getAllEntitlements');
        getUserIn.addParam('sysparm_user', g_form.getValue('requested_by'));
        getUserIn.getXML(populateDetails);

    }


    function populateDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

      var returneddata = JSON.parse(answer);  
//TRICIA DATA {"access":"Admin Rights","application":"Compose"}

           alert(answer);

            g_form.setValue('current_applications', answer);
            var msg = getMessage('This User has Entitlements to: ' + "'" + answer + "." + "The entitlements for all applications in ServiceNow will be set to inactive.   .", function(msg) {
                g_form.showFieldMsg('current_applications', msg, 'warning');
            });
        }


    }

 

 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @triciav,

 

 in your script include,

//Initialize your resultObj variable to array type

var resultObj = [];

 

//In the while loop, push values like this

while(grApp.next()){

  resultObj.push('Application:' + grApp.u_application.getDisplayValue().toString() + ' - Access Level: ' + grApp.u_access_level.getDisplayValue().toString());

}

//return the result

return JSON.stringify(resultObj);

 

 

In Your Client Script populateDetails finction, after getting the answer, parse it back to array

 

var data = JSON.parse(answer);

//append all the values in to one string

var data_str = '';

data.forEach(function(elem){

  data_str = data_str + elem + '\n';

});

 

Now you can use the data_str variable to populate the field.

 

g_form.setValue('current_applications', data_str);

 

 

Thanks,

Anvesh

 

 

 

 

 

 

Thanks,
Anvesh

View solution in original post

3 REPLIES 3

AnveshKumar M
Tera Sage
Tera Sage

Hi @triciav,

 

 in your script include,

//Initialize your resultObj variable to array type

var resultObj = [];

 

//In the while loop, push values like this

while(grApp.next()){

  resultObj.push('Application:' + grApp.u_application.getDisplayValue().toString() + ' - Access Level: ' + grApp.u_access_level.getDisplayValue().toString());

}

//return the result

return JSON.stringify(resultObj);

 

 

In Your Client Script populateDetails finction, after getting the answer, parse it back to array

 

var data = JSON.parse(answer);

//append all the values in to one string

var data_str = '';

data.forEach(function(elem){

  data_str = data_str + elem + '\n';

});

 

Now you can use the data_str variable to populate the field.

 

g_form.setValue('current_applications', data_str);

 

 

Thanks,

Anvesh

 

 

 

 

 

 

Thanks,
Anvesh

Awesome! Thank you so much Anvesh!

This was exactly what I needed

@triciav 

I'm glad it helped you.

Thanks,
Anvesh