How to call REST Message via Scheduled Job in scoped application?

yogeshmore
Kilo Explorer

Hi,

I have tried using following code in my Scheduled Job (SJ) to call REST Message in a scoped application.

var r = new sn_ws.RESTMessageV2('x_snc_all_time_car.All Surf SN TimeCard', 'get');

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

var parser = new JSONParser();

var parsed = parser.parse(responseBody);

It gives a warning and SJ doesn't get executed at all:

java.lang.SecurityException: JSONParser undefined, maybe missing global qualifier

Then I tried amending with global as below:

var r = new sn_ws.RESTMessageV2('x_snc_all_time_car.All Surf SN TimeCard', 'get');

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

var parser = new global.JSONParser();

var parsed = parser.parse(responseBody);

It gives a warning and SJ doesn't get executed at all:

org.mozilla.javascript.EcmaError: The undefined value has no properties.


Anyone have any idea to handle with this?


Thanks,

Yogesh More

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Yogesh,



Please follow the below steps and let me know the outcome.


Go to the script include->Search for JSONParser->Now make sure the field "accessible from" is set to "All application scopes". OOB this is set to be "this application scope only".


Please find the screenshot attached for reference.


Screen Shot 2015-06-06 at 7.40.51 PM.JPG



View solution in original post

8 REPLIES 8

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Yogesh,



I am not sure what steps you have   taken to get to the point where you are now.


You mention you want to invoke from a scheduled job a RESTMessageV2 from a scoped context.


Have you verified that the RESTMessageV2 you create works as expected in an un-scoped context?


Then moved on to check it works as expected in a scoped context?


Having done that, you are now seeking to invoke from a scheduled job?



Best Regards,




Tony




yogeshmore
Kilo Explorer

Hi Tony,



Thanks for looking into it.



Yes, it works as expected without any issue from Scripts-Background run in Global scope.


And now I want that to be scheduled.



Thanks,


Yogesh More


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Yogesh,



Please follow the below steps and let me know the outcome.


Go to the script include->Search for JSONParser->Now make sure the field "accessible from" is set to "All application scopes". OOB this is set to be "this application scope only".


Please find the screenshot attached for reference.


Screen Shot 2015-06-06 at 7.40.51 PM.JPG



Hi Pradeep,



Yes, I had checked it but I did not want to change OOB Script Include.


However, I have achieved it changing my code in following way:



gs.include("global.JSON");

      var r = new sn_ws.RESTMessageV2('x_snc_all_time_car.All Surf SN Deployments', 'get');


      var response = r.execute();


      var responseBody = response.getBody();


      var httpStatus = response.getStatusCode();


     


      var json = new global.JSON();


      var parsed = json.decode(responseBody);



This worked for me.


Thanks anyway for your response! Marking it as Correct Answer.



Regards,


Yogesh More