Passing JSON Response to Widget in Correct Format

bdsibert
Tera Contributor

I have developed a Widget based on many of the examples available in the community that displays results from an external REST API call. The widget reacts appropriately (it updates based on the selection in a field on the form), but I am having a difficult time figuring out how to display the results correctly on my catalog item HTML. 

Server Script

(function() {

    var r = new sn_ws.RESTMessageV2();
    r.setHttpMethod('get');
    r.setRequestHeader("Content-Type", "application/json");
    r.setMIDServer("DEV - MID Server 1");
    r.setEndpoint('https://test.com/acm/db/api/v1.0/users?pam_group=' + input.message);
    var response = r.execute();
    data.result = JSON.parse(response.getBody());

})();

Client Script

function($rootScope) {
    var c = this;
    $rootScope.$on("field.change", function(evt, parms) {
        if (parms.field.name == 'cyberark_selectpam_v2') {
            c.data.value1 = parms.newValue;
            c.data.message = c.data.value1;
            c.server.update();
        }
    });

}

HTML

<div>
   <p class="label label-info">
    These users are part of the selected PAM Group
  </p>
  </div>

<div>
<table>
   <tr style="background-color: white">
     <th style="background-color: #E8E8E8">Name
     </th>
     <tr ng-repeat="name in data.result">
       <td style="background-color:white">{{name}}//the name variable wrapped in double curly braces didn't show up when I use the code editor here. 
    </td>
  </tr>
  </table>

</div>

When I use the widget, I get results like this:

find_real_file.png

But I would like each individual to have their own row. How can I parse this to do that when the JSON returned is like this:

"data": [
"Damera, Murali",
"Kumar, Ashish",
"Sankaranarayanan, Karthikeyan",
"Venkatesan, Rajesh Kumar"
]

Thank you!

1 ACCEPTED SOLUTION

Hi,

I don't know all the specifics of your page, but...

I believe you should be able to add an additional condition with something like:

if (parms.field.name == 'cyberark_selectpam_v2' && $scope.page.g_form.getValue('field_name') == 'add_server') {

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

7 REPLIES 7

bdsibert
Tera Contributor

So I updated my code on the server script as follows and it seems to be return the correct information. Can anyone confirm if this code seems appropriate:

 

(function() {
var arr=[];
	var r = new sn_ws.RESTMessageV2();
			r.setHttpMethod('get');
			r.setRequestHeader("Content-Type","application/json");
			r.setMIDServer("DEV - MID Server 1");
			r.setEndpoint('https://test.com/users?pam_group='+ input.message);
      var response = r.execute();
	var result=JSON.parse(response.getBody());
	for (var loop=0; loop<result.data.length; loop++)
		{
			arr.push(result.data[loop]);
		}
	data.result=arr;
})();

Hi,

That looks correct to me and sets the data.result to your array for future handling.

And you've set your application/json to ensure correct format.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi There,

Yes this is correct code.

 

THanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

bdsibert
Tera Contributor

On another but connected topic, I only want my server script to fire (meaning my call to the API) if my request type field on the form is set to Add Servers. Is there a way to set another parm or a condition so that when the parm.field.name = 'cyberark_selectpam_v2' and that field is changed, that the widget will only run if a separate field is set to Add Servers. In other words, if the cyberark_selectpam_v2 field is changed but the request type field is not Add Server, the widget will not run.

I only ask this because I have two widgets that display different pieces of information (and use a different endpoint) that are watching for the same field to be changed to update. Therefore, every time that field is updated, two separate calls are being made to APIs.