How to store Rest API Endpoint url , method and Authentication Key and values in system property ? how can we call this property in scheduled jobs/

navya4
Tera Contributor

How to store  Rest API Endpoint url , method and Authentication Key and values in system property ? how can we call this property in scheduled jobs.

can you please share examples. Thanks in advance.

8 REPLIES 8

Vikas-Malhotra
Mega Guru

Is there any specific reason for not storing these details in the REST Message, and Auth Profile tables?

for security key and values should not be open for everyone to check

navya4
Tera Contributor

i created sys_properties for 3 key values key1, key2, key3.

How can we call this sys_property in rest message

i am using this below code but it is not working throwing 400 error, could you please guide

try{

var r = new sn_ws.RESTMessageV2('Name of rest message', 'Default GET');

var API0 = gs.getProperty('KEY1');
var API1 = gs.getProperty('kEY2');
var API2 = gs.getProperty('KEY3');

var response = r.execute();
var httpStatus = response.getStatusCode();
var responseBody = response.getBody();

}

catch(ex)

{

var message = ex.message;

}

 

 

Ed13
Tera Contributor

To set endpoint :

var sm = new sn_ws.RESTMessageV2();
sm.setEndpoint("http://web.service.endpoint");

To set  method : 

var sm = new sn_ws.RESTMessageV2();
sm.setHttpMethod("post");

To set authentication :

var sm = new sn_ws.RESTMessageV2("<REST_message_record>","get"); //Might throw exception if message doesn't exist or not visible due to scope.
sm.setBasicAuth("username","password");

 

Your code will look something similar with: 

var API0 = gs.getProperty('KEY1'); // holds the endpoint which should already be part of the rest message
var API1 = gs.getProperty('kEY2'); // holds the httpmethod which should already be part of the rest message
var API2 = gs.getProperty('KEY3'); //holds the password of the profile


try{

var r = new sn_ws.RESTMessageV2('Name of rest message', 'Default GET');

r.setEndpoint(API0);
r.setHttpMethod(API1);
r.setBasicAuth('username',API2);

var response = r.execute();
var httpStatus = response.getStatusCode();
var responseBody = response.getBody();

}

catch(ex)

{

var message = ex.message;

}

I would advise you to use the REST messages settings and also check the variable substitutions : 

https://docs.servicenow.com/bundle/rome-application-development/page/integrate/outbound-rest/concept...

 

Here is more of the REST API scoped documentation : 

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/sn_ws-namespace/c_RESTAPIReq...

 

You could either use the messages inside the scheduled scripts or use Async Business Rules which helps a lot with the trigger conditions and does not put any pressure on the performance of the system. 

If you stay with the scheduled scripts you could also try to add a function on it,this way you can use some conditions like.. if the company is X then use this API.