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.

JavaScript Fetch API in Data Source

Community Alums
Not applicable

I am attempting to populate a table in a custom application using data gathered from another tools API. The API uses GraphQL and I'm attempting to use a custom JavaScript Data Source to load the data. To do so I was trying to use the JavaScript Fetch API which to my knowledge should be included in Vanilla JavaScript, but it does not appear to be. Additionally I know my fetch request works because I am able to get the data in other JavaScript compilers.

 

Is there a way to make JavaScript Fetch method inside of ServiceNow? If no, is there some other way to make a GraphQL call and use the response to populate a table? Any help/thoughts would be appreciated.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Alright after much digging I've figured out a better method than using the JavaScript Fetch API.

 

It turns out that there is a way to make a GraphQl call in ServiceNow, it just takes a little workaround. I found the solution in this post but I'll also explain it here:

 

As noted in the post by Ihor you can make a GraphQL call using an action with a REST Step and giving it the Header "Content-Type" : "application/json". Then all you have to do is pass the body in JSON format.

EvanAmyotte_0-1685982675513.png

Ihor did this by turning his query into a JSON format ahead of time and pasting it into the Request Body. I did it slightly differently by creating a Script step before my REST step to transform my desired request into JSON format using the JavaScript JSON.stringify() method, then passed the data pill in as the request body.

2023-06-05 10_35_02-Flow Designer - Action Designer _ firstRequest.png

EvanAmyotte_2-1685983128482.png

EvanAmyotte_3-1685983173224.png

 

After that the GraphQL call worked as expected. Hope this helps others avoid all the headache this caused me.

 

View solution in original post

1 REPLY 1

Community Alums
Not applicable

Alright after much digging I've figured out a better method than using the JavaScript Fetch API.

 

It turns out that there is a way to make a GraphQl call in ServiceNow, it just takes a little workaround. I found the solution in this post but I'll also explain it here:

 

As noted in the post by Ihor you can make a GraphQL call using an action with a REST Step and giving it the Header "Content-Type" : "application/json". Then all you have to do is pass the body in JSON format.

EvanAmyotte_0-1685982675513.png

Ihor did this by turning his query into a JSON format ahead of time and pasting it into the Request Body. I did it slightly differently by creating a Script step before my REST step to transform my desired request into JSON format using the JavaScript JSON.stringify() method, then passed the data pill in as the request body.

2023-06-05 10_35_02-Flow Designer - Action Designer _ firstRequest.png

EvanAmyotte_2-1685983128482.png

EvanAmyotte_3-1685983173224.png

 

After that the GraphQL call worked as expected. Hope this helps others avoid all the headache this caused me.