We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

nathanfirth
Tera Guru

GraphQL is an open-source data query and manipulation language for APIs. GraphQL offers many benefits over REST API’s, including:

  • Fetching data across multiple sources from a single API call
  • Returns only the data that is requested
  • Supports validation and type checking
  • Autogenerating API documentation

In this video, I’ll show you how to construct a sample GraphQL query to fetch data in ServiceNow and then how we can use GraphQL inside of a ServicePortal widget.

I've made the source code, download links, and widget available in the full blog post, available here: https://serviceportal.io/graphql-in-servicenow/

Cheers,

-----------------
Nathan Firth
Founder and Chief Portal Officer
NewRocket, Inc.
nathan.firth@newrocket.com
http://serviceportal.io
http://newrocket.com
2 Comments
Marcus Reinhard
Giga Contributor

Hi @nathanfirth ,

 

thanks for sharing.

 

I added your video tutorial to my own HowTo 🙂

https://github.com/noxify/ServiceNow-GraphQL-Example

 

Regards,

Marcus

 

Ryan126
Tera Contributor

Hi Nathan - Thanks for posting, this was very helpful!  Wondering if you've tried any similar widgets but with more dynamic queries, such as current user.  What I ended up having to do is use your method of having the query as an instance option, using "REPLACEMEPLEASE" as a key:

  {
  GlideRecord_Query {
    sn_hr_core_profile(queryConditions: "user=REPLACEMEPLEASE") {
      _results {
        employment_start_date {
          value
        }
        date_of_birth {
          value
        }
      }
    }
  }
}

Then, in the widget Client Script, I used a replacement to mimic how you ran the query.  In my example below, "c.data.id_w7" was a replacement being called from the Server Side data object:

var c = this;  
var myqu = $scope.options.query;
var rep = myqu.replace("REPLACEMEPLEASE", c.data.id_w7)
var query = {
	"query": rep
};

It works, but not sure if it's the best way.  Any other ideas?

Also, I did try using the GraphQL APIs mentioned here in ServiceNow documentation, but ran into some cross privilege access issues querying the scoped HR tables...going to submit a HI ticket on that!