How to insert records through a scheduled server script within a custom application

wolfgangb
Giga Contributor

I am in the process of converting a global scheduled job script into a custom scoped application. The job should fetch some data from an external service, fill the application scope local table with data.

Inserting records into global tables from a global scheduled script works well with following script:

var srv = new GlideRecord("cmdb_ci_server");

srv.get("correlation_id", hKey);

srv.correlation_id = hKey;

srv.host_name = "testhost";

srv.updateWithReferences();

When I try to run this script within a custom application on a custom application table I am not able to insert anything.

What am i doing wrong?

1 ACCEPTED SOLUTION

Ok finally i got it working. The problem was that i was not using the newer 'scoped scripted API' that can be found here:


ServiceNow Developers



then this script actually works for me without problems:



var srv = new GlideRecord('cmdb_ci_server');


srv.initialize();


srv.setValue('host_name', 'testhost');


srv.insert();


View solution in original post

7 REPLIES 7

srinivasthelu
Tera Guru

Hi Wolfgang,



updateWithReferences() function is not available for scoped apps. use update() for simple direct updates, Else you need to query related tables and do explicit update.



var srv = new GlideRecord("cmdb_ci_server");


srv.get("correlation_id", hKey);


srv.correlation_id = hKey;


srv.host_name = "testhost";


srv.update();




Thanks


Srinivas



Make this answer as helpful/correct if it does so


Mike Allen
Mega Sage

var srv = new GlideRecord("cmdb_ci_server");


srv.get("correlation_id", hKey);


srv.host_name = "testhost";


srv.update();



Try that.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hey Wolfgang,



The below thread should answer your question.


error Function updateWithReferences is not allowed in scope


No does not work still, even if i use the update() method.



It does not work on the apps own scope tables as well as it does not work on global scope tables.



What am i doing wrong? Outside a custom app this scheduled job works, inside an app nothing