<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Scripted REST API Response returned as &amp;quot;Undefined&amp;quot; in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426287#M83213</link>
    <description>&lt;P&gt;Yes Ankur. All the counters are getting properly updated on the logs. Have added log statement for displaying the groupname as well before pushing it to the writer and am getting the group names. My guess that the issue has something to do the data passing to the writer&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jun 2019 11:32:11 GMT</pubDate>
    <dc:creator>rohit121</dc:creator>
    <dc:date>2019-06-13T11:32:11Z</dc:date>
    <item>
      <title>Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426281#M83207</link>
      <description>&lt;P&gt;Hi Folks,&lt;/P&gt;
&lt;P&gt;Have created a scripted rest API which takes in a company sys_id as input and is supposed to return all the assignment group names and member count (for the respective groups) tagged under the company. But currently I am getting back no responses once I test it through the API explorer.&lt;/P&gt;
&lt;P&gt;Below is the Get resource code I have implemented.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {&lt;/SPAN&gt;&lt;BR /&gt; &lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; // implement resource here&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var clientID = request.pathParams.client_id;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; gs.log('client id:' + clientID);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var writer = response.getStreamWriter();&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var cnt1 = 0;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var hdrs = {};&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; hdrs['Content-Type'] = 'application/json';&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; response.setStatus(200);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; response.setHeaders(hdrs);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; //start building the JSON string&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; writer.writeString("{\"results\":[");&lt;/SPAN&gt;&lt;BR /&gt; &lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; //query comapny table for sys_id validation&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var comp = new GlideRecord('core_company');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; comp.addQuery('sys_id','=',clientID);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; comp.query();&lt;/SPAN&gt;&lt;BR /&gt; &lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; cnt1 = comp.getRowCount();&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; gs.log("company count:" + cnt1);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; if(cnt1== 0){&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; writer.writeString("Error: Invalid Client ID");&lt;/SPAN&gt;&lt;BR /&gt; &lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; else{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; writer.writeString(global.JSON.stringfy("valid Client ID"));&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var grp = new GlideRecord('sys_user_group');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; grp.addQuery('company','=',clientID);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;//grp.addActiveQuery();&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; grp.query();&lt;/SPAN&gt;&lt;BR /&gt; &lt;BR /&gt; &lt;BR /&gt; &lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var cnt2 = grp.getRowCount();&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; gs.log('group count:' + cnt2);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; if(cnt2==0){&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; writer.writeString(global.JSON.stringfy("No group is associated with the specific client"));&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; else{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; while(grp.next()){&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var groupObj = {};&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var grpm = new GlideAggregate('sys_user_grmember');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; grpm.addQuery('group',grp.sys_id);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; grpm.addAggregate('COUNT');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; grpm.query();&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; var grpMembers = 0;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; if(grpm.next()){&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; grpMembers = grpm.getAggregate('COUNT');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; groupObj.groupName = grp.name + '';&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; groupObj.groupID = grp.sys_id + '';&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; groupObj.groupMembers = grpMembers;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; writer.writeString(global.JSON.stringfy(groupObj));&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; if(grp.hasNext()){&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; writer.writeString(",");&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; //close the response object&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; writer.writeString("]}");&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; }&lt;/SPAN&gt;&lt;BR /&gt; &lt;BR /&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; })(request, response);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Let me know if someone also faced something similar.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 05:43:09 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426281#M83207</guid>
      <dc:creator>rohit121</dc:creator>
      <dc:date>2019-06-12T05:43:09Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426282#M83208</link>
      <description>&lt;P&gt;Hi Rohit,&lt;/P&gt;
&lt;P&gt;First of all few corrections and check&lt;/P&gt;
&lt;P&gt;comp.addQuery('sys_id',clientID);&lt;/P&gt;
&lt;P&gt;grp.addQuery('company',clientID);&lt;/P&gt;
&lt;P&gt;are you getting cnt1 and cnt2 value ?&lt;/P&gt;
&lt;P&gt;add log statement and debug 1 by 1&lt;/P&gt;
&lt;P&gt;Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.&lt;BR /&gt;Thanks&lt;BR /&gt;Ankur&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 05:59:10 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426282#M83208</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2019-06-12T05:59:10Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426283#M83209</link>
      <description>&lt;P&gt;HI,&lt;BR /&gt;&lt;BR /&gt;Can you try using queryParms instead of this&amp;nbsp;&lt;STRONG&gt;var clientID = request.pathParams.client_id;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Meaning use this: var clientID = request.queryParams.client_id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;BR /&gt;Ashutosh Munot&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 05:59:29 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426283#M83209</guid>
      <dc:creator>Ashutosh Munot1</dc:creator>
      <dc:date>2019-06-12T05:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426284#M83210</link>
      <description>&lt;P&gt;Hi Ankur,&lt;/P&gt;
&lt;P&gt;Thanks for the response. I am getting the expected values for cnt1 and cnt2 and without and with the corrections suggested. Though still don't see the response coming up.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 13:03:47 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426284#M83210</guid>
      <dc:creator>rohit121</dc:creator>
      <dc:date>2019-06-12T13:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426285#M83211</link>
      <description>&lt;P&gt;Hi Rohit,&lt;/P&gt;
&lt;P&gt;Did you add log statements to check what value is printed for object&amp;nbsp;groupObj just before pushing it into writer&lt;/P&gt;
&lt;P&gt;Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.&lt;BR /&gt;Thanks&lt;BR /&gt;Ankur&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 13:29:55 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426285#M83211</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2019-06-12T13:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426286#M83212</link>
      <description>&lt;P&gt;Thanks for the reply Ashutosh. Have configured my API to take in the client_id from the path param. So it has to be the&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;request.pathParams.client_id.&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE id="endpointURI" class="ng-binding"&gt;GET https://mydev.service-now.com/api/amspa/company_groups_and_memebers_count/{client_id}&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jun 2019 13:38:57 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426286#M83212</guid>
      <dc:creator>rohit121</dc:creator>
      <dc:date>2019-06-12T13:38:57Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426287#M83213</link>
      <description>&lt;P&gt;Yes Ankur. All the counters are getting properly updated on the logs. Have added log statement for displaying the groupname as well before pushing it to the writer and am getting the group names. My guess that the issue has something to do the data passing to the writer&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 11:32:11 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426287#M83213</guid>
      <dc:creator>rohit121</dc:creator>
      <dc:date>2019-06-13T11:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted REST API Response returned as "Undefined"</title>
      <link>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426288#M83214</link>
      <description>&lt;P&gt;Hi Rohit,&lt;/P&gt;
&lt;P&gt;Why are you using writer object and not directly setting it into json object and then use response.setBody(jsonObj);&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Ankur&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 14:38:59 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/scripted-rest-api-response-returned-as-quot-undefined-quot/m-p/1426288#M83214</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2019-06-13T14:38:59Z</dc:date>
    </item>
  </channel>
</rss>

