Virtual agent

HARSHA GOWDA R
Tera Contributor
(function execute() {
/* Code your script logic here. For example, you might want to call an external web service
to find the best local Italian restaurants. Call a Restaurants Web Service then use the results
to populate a rows array.
var headers = [
'Name', // Column 1 header
'Description' // Column 2 header
];
var rows = [];
rows.push([ // Row 1
'De Napoli', // Column 1 value
gs.getMessageLang("Best Italian Restaurant Ever", vaContext.getRequesterLang()) // Column 2 value
]);
rows.push([ // Row 2
'Albertos', // Column 1 value
gs.getMessageLang("Even Better Italian Restaurant", vaContext.getRequesterLang()) // Column 2 value
]);
return {
columnHeaders: headers,
columnValues: rows
};
*/

var headers = ['Access', 'Endpoint']; // Define column headers
var rows = []; // Array to hold row values
var roleObject = {};
var userRoles = new GlideRecord('tablename');
userRoles.addQuery('directory.servicenow_id', gs.getUserID());
userRoles.addQuery('stage', '=', 'granted'); // Ensure the 'stage' field is correct
userRoles.orderBy("iam_roles.application");
userRoles.query();

while (userRoles.next()) {
var roleID = userRoles.getDisplayValue('iam_roles');
roleObject[roleID] = userRoles.getDisplayValue('iam_roles.application');;
// Push each role and application as a row
rows.push([roleID, roleObject[roleID]]);
}
// Return the table structure
return {
columnHeaders: headers, // Column headers for the table
columnValues: rows // Row values for the table
};
})();
This code i have written in table form bot response in virtual agent.This is giving me the correct output currently its giving me in this format

HARSHAGOWDAR_0-1730180889220.png
But i need access and endpoint should come in separate column,below i have pasted the reference of it

HARSHAGOWDAR_1-1730181015813.png

 



6 REPLIES 6

Ravi Gaurav
Giga Sage
Giga Sage

Hi @HARSHA GOWDA R 

 

Try the below Script 

function execute() {
// Define column headers
var headers = ['Access', 'Endpoint'];
var rows = []; // Array to hold row values

var userRoles = new GlideRecord('tablename');
userRoles.addQuery('directory.servicenow_id', gs.getUserID());
userRoles.addQuery('stage', '=', 'granted'); // Ensure the 'stage' field is correct
userRoles.orderBy("iam_roles.application");
userRoles.query();

while (userRoles.next()) {
// Get values for 'Access' and 'Endpoint' columns
var access = userRoles.getDisplayValue('iam_roles');
var endpoint = userRoles.getDisplayValue('iam_roles.application');

// Push each role and application as a separate row
rows.push([access, endpoint]);
}

// Return the table structure
return {
columnHeaders: headers, // Column headers for the table
columnValues: rows // Row values for the table
};
}

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi @Ravi Gaurav 
Its not working

Hi @HARSHA GOWDA R 

I added a log /info can you tell me the output of that

function execute() {
var headers = ['Access', 'Endpoint'];
var rows = [];

// Create a GlideRecord to query the relevant table
var userRoles = new GlideRecord('tablename');
userRoles.addQuery('directory.servicenow_id', gs.getUserID());
userRoles.addQuery('stage', '=', 'granted');
userRoles.orderBy("iam_roles.application");
userRoles.query();

// Loop through each result and push values into rows
while (userRoles.next()) {
// Get values for Access and Endpoint columns
var access = userRoles.getDisplayValue('iam_roles'); // Access value
var endpoint = userRoles.getDisplayValue('iam_roles.application'); // Endpoint value

// Log values for debugging
gs.info("Access: " + access + ", Endpoint: " + endpoint);

// Push the row into the array
rows.push([access, endpoint]);
}

// Log rows array for verification
gs.info("Rows Array: " + JSON.stringify(rows));

// Return the table structure for Virtual Agent
return {
columnHeaders: headers,
columnValues: rows
};
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

HARSHAGOWDAR_0-1730183799692.png