How to create an incident using REST APIs that will be triggered by a Service Portal widget

JC S_
Mega Guru

We need to create an incident record using REST APIs which will be triggered by one of our Service portal widget. This is going to be on a public facing page as such no users will be logged in and we will just use an internal integration account to send a REST API call. How should we do this?

 

Intended usage: We have a form in a widget that will validate if the email entered is being used by any user in the system, if not then an incident ticket will be automatically created so the accounts management team can validate if account should be created.

6 REPLIES 6

Jon Barnes
Kilo Sage

I have built several widgets that do rest calls (usually I am doing this with services and sharing data across widgets), but in those cases, I don't do anything in the widget's server side script (it is usually blank, with the work being done in the scripted REST API). You can just update your data model in your client controller when your REST call completes. But without knowing the use case for your widget, I can't say that is best for you. You can have both working for you if you want, but I would try to minimize round trips to the server.

If we can create an incident record through scripted REST API then just call it on the widget it would be better. We just need to pass the email entered on the description of the incident record.

Hi Jon,

 

Here's our use case:

We have a form in a widget that will validate if the email entered is being used by any user in the system, if not then an incident ticket will be automatically created so the accounts management team can validate if account should be created.

 

How can we do the REST calls? How do we setup the scripted REST API then trigger it on the widget?

Q: "How do we setup the scripted REST API?"

A: https://developer.servicenow.com/app.do#!/document/content/app_store_doc_scripted_rest_jakarta_t_CreateAScriptedRESTService?v=jakarta

Q: "How to trigger from a widget?"

A:

from Client Controller use $http - https://docs.angularjs.org/api/ng/service/$http

  Basic setup:

$http({
    method: 'GET',
    url:    'https://your_scripted_REST_API_Path?your_params_or_queries.....',
}).then(function success(resp){
    //do stuff here with successful resp
}, function err(resp){
    // do stuff here with an error
})

 

Or from the Server Side scripting of the widget use RESTMessageV2() - https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=c_RESTMessageV2API

  Basic setup:

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://your_scripted_REST_API_Path?your_params_or_queries.....');
request.setHttpMethod('GET');

request.setRequestHeader("Accept","application/json");

var response = request.execute();
var body = response.getBody();
// continue to do stuff based on response...

You will have to realize though that since your intended use is for non-logged in users, your Scripted API won't have any authentication to it and will also be open.

As an alternative the same thing can be accomplished with a Script Include accessed through the Server Side scripting of the widget.