jpavanaryan
Tera Expert

Angular web app with ServiceNow backend

Github: https://github.com/pavankjadda/Angular-WebApp-ServiceNow 

What's this?

Angular 10+ web application developed to replace Service Portal on ServiceNow. It offers several benefits when compared native Service Portal

  1. Angular is a widely used framework and offers responsive single page applications (SPA)
  2. No need to wait for ServiceNow to update their instance to Latest Angular version or Bootstrap version
  3. No need to hack ServicePortal widgets and spend hours on fixing collisions between ServiceNow libraries and your custom CSS and JS libraries.
  4. Use industry standard OAuth2 protocol for securing the web app
  5. Use ServiceNow as a REST API backend

Technologies Used

  1. Angular 10.x
  2. Bootstrap 4.x
  3. ServiceNow Madrid
  4. OAuth2.0
  5. Others

Configure ServiceNow backend

  1. Get ServiceNow developer instance if you do not have one
  2. Follow the instructions from ServiceNow docs to activate the OAuth2.0 plugin
  3. Once activated, search for oauth and select Application Registries to create a new OAuth client
  4. Select Create an OAuth API endpoint for external clients and fill the client details. See ServiceNow docs for more information
  5. Make sure the user has REST API access and assigned snc_platform_rest_api_access role
  6. Test the created OAuth client with Postman or curl.
    $ curl -d "grant_type=password&client_id=<Client Id>&client_secret=<Client secret>&username=<UserId>&password=<Password>"   -H "Content-Type: application/x-www-form-urlencoded" https://devxxxxx.service-now.com/oauth_token.do
    ​
  1. If you are using Postman, select No Auth Authorization and body type as x-www-form-urlencoded. Enter the following key-value pairs in body
 grant_type=password
 client_id=<Client Id>
 client_secret=<Client secret>
 username=<Username>
 password=<Password>
  1. Response from step 6 or 7 should be the following. Use access_token in subsequent steps to access data
{
    "access_token": "z5Ewzj6KRSWXBnljlYRpn-R_5gw3JSQ8y1h71cuCIyVy546I7jwg5k9M2E0ctc2ssJC9S2ER6ZWGXs474Ext4Q",
    "refresh_token": "Hek_vW3Q3jA2qM5nurYFCEruWPI5EX6zriI8a92v4FafFOsD5el17fWkrz48ZLlw3kpjZSRJmiK9uTyjPL6rKg",
    "scope": "useraccount",
    "token_type": "Bearer",
    "expires_in": 1799
}
  1. Go to ServiceNow instance and search for Scripted Rest APIs and select it. Click on New and enter Name and other details then submit it
  2. Select created Scripted Rest API and create new Resource under resources and enter following info HTTP method: GET NameScript:
    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) 
     {
    	var userSysId=gs.getUserID();
    	var gr=new GlideRecord('sys_user_has_role');
    	gr.addQuery('user',userSysId);
    	gr.query();
    	var roles=[];
    	while(gr.next())
    		{
    			roles.push({name:gr.role.name,sysID:gr.role,description:gr.role.description});
    		}
    
    	var body=[];
    	body.push({username:gs.getUserName(),displayName:gs.getUserDisplayName(),sysId:gs.getUserID(),roles:roles});
        response.setBody(body);
    
    })(request, response);
  3. This scripted API will be used in Angular App to access user information and roles

How to Run the Angular App?

  1. Clone this repository into your local machine and open in WebStorm or VS Code
  2. Open the file src/app/app.constants.ts and change details based on your OAuth client and Scripted REST API detail
export const SERVER_API_URL = 'https://dev81909.service-now.com/';
export const USER_INFO_URL = 'api/x_19668_halo/user_info';

export const OAUTH2_CLIENT_ID= '3c76622bc581b30082098914f97ee08e';
export const OAUTH2_CLIENT_SECRET= 'password12345';
export const OAUTH2_ACCESS_TOKEN_URI = SERVER_API_URL + 'oauth_token.do';

export const INCIDENT_API_URL = 'api/now/table/x_19668_halo_incident';
  1. Download the Allow-Control-Allow-Origin extension to prevent Allow-Control-Allow-Origin errors
  2. Start the Angular web app with the following command
$ ng serve --watch
  1. Go to http://localhost:4200 and login with your credentials
  2. Click on Incidents menu to see list of options for incidents
  3. Select Incidents list to see list of incidents and click on Edit or View to see incident data
  4. Add new components and modules based on your requirements
  5. By default Angular app runs on HTTP protocol and data is not encrypted. Use Edge Encryption to encrypt data between ServiceNow instance and Angular WebApp

Note: Uploaded Update set and Angular code to this post

Comments
rani10
Kilo Contributor

Facing CORS issue after login even though the  Allow-Control-Allow-Origin extension  is installed.

jpavanaryan
Tera Expert

Do you have screenshot of the issue?

rani10
Kilo Contributor

Right now my instance is down, show I cannot show you the screenshot.

But the error was related to CORS .

 

My second question is how do I integrate angular javascript bundles with servicenow exceeding 1.3 MB?

 

I am facing issues when I am using routing in angular, splitting the bundles in less than 1.3 Mb and integrating it with servicenow.

What should be the base href value when we are trying to integrate angular 9 with servicenow.

rani10
Kilo Contributor

PFA screenshot

rani10
Kilo Contributor

PFA screenshot

Tommy Jensen
Giga Guru

I am also curious for the answer to this CORS issue. I tried also but could not even find and install the mentioned plugin.

I think a different authentication is needed because it is not possible to setup CORS to the OAuth2 authentication endpoint. Simply because a frontend should NOT rely on END users to install a plugin to make it work. For a POC test fine but not for a production frontend.

Your second question? Does that relate to this? I don't see why that should be an issue. This frontend is hosted elsewhere so the size should not matter to ServiceNow ?

rani10
Kilo Contributor
Second question is a different approach.Embedding angular javascript bundles inside servicenow
jpavanaryan
Tera Expert

You can add proxy.conf.json file to prevent this error. I will update the code

jpavanaryan
Tera Expert

Try this plugin 

https://chrome.google.com/webstore/detail/cross-domain-cors/mjhpgnbimicffchbodmgfnemoghjakai/related...

 

By the way, I may not check community often, if you have any issue, create an issue in Github project

jpavanaryan
Tera Expert

Tommy, try this plugin https://chrome.google.com/webstore/detail/cross-domain-cors/mjhpgnbimicffchbodmgfnemoghjakai/related...

 

Yes, this is for POC only. For production, you don't have to do this. ServiceNow allows you to create CORS rules and instructions are described here and also shown below

 

find_real_file.png

jpavanaryan
Tera Expert

Rani,

You can define base href in environment and keep API URLs constant file. See this file for example. I don't think bundle size affects deployment unless you configure size budgets

Version history
Last update:
‎05-03-2019 07:47 AM
Updated by: