Nick121
ServiceNow Employee
ServiceNow Employee

Summary

Using a Rest api is useful when you need to query for ServiceNow data on a public page. Querying for data requires authentication meaning all global data brokers require sign on. Using a rest api is a good way to bypass authentication and still retrieve your data. You can do other operations with a REST data broker such as transforming and altering data, eliminating the need for other data brokers.

 

  1. From your UIB page, click the data resources icon on the navbar to the bottom left
  2. Click the +Add button
  3. Click +New and select REST
  4. You will be taken to a form on the Now Platform to create a new Rest Data Broker. REST Data Broker table (sys_ux_data_broker_rest)
 

 

5. Fill out the Name and Description fields

6. In this example, our Http method is GET

7. Fill out the properties field as shown above

    1. Properties are the structure of that data you pass in (parameters)
      1. “name” is how you reference the object in your script
      2. ‘label” is the text that will appear next to the input field on UIB
      3. “description” is the text displayed when you click the info icon next to the label and input field on UIB
      4. “fieldType” is the data type of your input
    2. You can have many properties

8. Skip the API and Parameters fields for now, we will come back to this field on step 26

9. Double click the top grey bar and click save or insert and stay

    1. We will need to return to this record later
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 10. Open a new window of your instance and use the filter navigator to navigate to System Applications - Studio

11. Select the Application your page resides in

12. Click the +Create Application File button in the top left of the page

13. Type “REST” or “scripted REST API” into the filter and select Scripted REST API

  

 14. Fill out the Name field

15. The API_ID will be auto generated but you can change it if you desire

16. Click Submit

17. Scroll to the bottom of the page and select the Resources tab

18. Click New

 

  

19. Fill out the name and relative path field

    1. For simplicity, have the name field and relative path field match

20. Right Click the top grey bar and Save

 

  

 21. The Resource Path should now appear beneath the HTTP method and Relative path fields

22. Copy this field to your clipboard

23. Keep this record open as you will return to it to write the script

 

 

 

 

24. Return to your REST Data Broker record page from Step 4

25. Paste the Resource path into the API field

26. Fill out the parameter field as follows

    1. You can have many Parameters
    2. Parameters are what is appended to the end of the URL (for example: user Id)

27. Click Save

    1. You may now close this tab but remember the names of the properties. You will need to reference the name in your script
 

 

 

 

 

 28. Now return to the window where you created your  REST resource from step 18

29. The script below queries the user table by user Id and returns the user’s name and email

    1. Note: You can use REST api script for a number of functions such as retrieving, transforming, and altering data

Pasted below is the script: 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var firstName = "";
var lastName = "";
var email = "";
//New GlideRecord to query the user table
var userQuery = new GlideRecord("sys_user");
userQuery.addQuery("user_name", request.queryParams.user_Id.toString());
userQuery.query();
if(userQuery.next()){
firstName = userQuery.first_name.toString();
lastName = userQuery.last_name.toString();
email = userQuery.email.toString();
}

return {"firstName": firstName, "lastName": lastName, "email": email};

})(request, response);
 

 

























 30.Scroll down and make sure to uncheck the Requires Authentication checkbox

31. Update the record

  

32. Return to your UIB page

33. Click the data resource Icon

34. Click +Add

35. Select  the application scope your page and data resource resides in

36. Select the REST data broker we just created

37. Click  Add

 
  
Comments
Philippe Casidy
Tera Guru

Hi,

 

Looks promising but the image are broken on my side 😞

find_real_file.png

Lampros
Tera Explorer

Are we going to fix this?

Rafael Oliveir1
Tera Contributor

Can you fix the images please? 

Thanks

kriskamal48775
Tera Explorer

@Nick121  Kindly fix this issue 😞 Looks very promising but hard to follow without screenshots. 

Version history
Last update:
‎08-11-2021 02:35 PM
Updated by: