JDBC Probe to insert/update records into sql database table from ServiceNow

Service Manager
Kilo Guru

Hi,

I was trying to insert/update the record via JDBC probe into SQL database table from ServiceNow. Below script was working as expected 

var mid = new GlideRecord("ecc_agent");

mid.addQuery('name','MID_Serverxxx');

mid.query();

if(mid.next()){

}

var data = new GlideRecord("sys_data_source");

data.addQuery('name','xxname of datasourcexx');

data.query();

if(data.next()){

}

var r1 = new JDBCProbe(mid.name);

r1.setDataSource(ds.sys_id);

jr1.setFunction("INSERT");

r1.setTable("xxdatabase tablexx");

r1.addParameter("skip_sensor","true");

r1.addField("Number", "INC7845129");

r1.addField("Contact", "david");

r1.create();

 But when I updated to below script to insert the records from target table "Incident", tickets assigned to Service desk group. It throws me an error

r1.addField("Number", current.number);

r1.addField("Contact", current.caller);

r1.addField("opened",current.opened_at);

r1.create();

Thanks

1 ACCEPTED SOLUTION

Your old script was working because you were not using current object there. You had static values (INC7845129 and david).

Now it's not working because current object is not available on scheduled jobs or background scripts.

current object is a GlideRecord object representing records in table and is available in server side scripts directly acting on tables (business rule, ACL, workflows).
For more information check-
https://community.servicenow.com/community?id=community_question&sys_id=a6d62986db7d1b005322f4621f9619dd#:~:text=Current%20object%20refers%20to%20current%20table.,refers%20to%20an%20incident%20table.

So you need to do a GlideRecord on incident table and then use that gliderecord object to dot walk to incidents fields.

For e.g. following script will give you all active incidents. gr is the gliderecord object-
var gr = new GlideRecord("incident");
gr.addQuery("active","true");
gr.query();
while(gr.next()) {
// Here you have gr as GlideRecord object representing each record
// So gr.caller_id.name will give you callers name on incident
}

For more information of GlideRecord check-
https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

-Tanaji
Please mark reply correct/helpful if applicable

View solution in original post

15 REPLIES 15

Yes 

gr is gliderecord object of incident and cmdb_ci, assgned_to, assignment_group us on task table.

Unfortunately

r1.addField("cmdb_ci", gr.cmdb_ci.getDisplayValue());

it didn't help and throw below error(FYI- my r1 is J1 now)

 

find_real_file.png

Thanks

Second bullet in my previous comment was to confirm columns of your database table.
May I ask how you came to a conclusion that this error is because of cmdb_ci line in your code? Error doesn't say anything about it.

-Tanaji
Please mark reply correct/helpful if applicable

Yes, cmdb_ci, assignment_group, assigned_to, resolved_by, close_code and close_notes are present on the SQL DB table.

I didn't conclude anything but this line of code is an example which is causing issue, we can switch it to assigned_to or assignment_group and I am getting same issue.

r1.addField("cmdb_ci", gr.cmdb_ci.getDisplayValue());
Error:

createElement - null: java.lang.reflect.InvocationTargetException: sun.reflect.GeneratedMethodAccessor37.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) com.glide.util.GlideDocument.invoke(GlideDocument.java:39) com.sun.proxy.$Proxy31.createElement(Unknown Source).

Evaluator: java.lang.NullPointerException
   Caused by error in script at line 113
org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source)
org.apache.xerces.dom.ParentNode.insertBefore(Unknown Source)
org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
com.glide.util.XMLDocument.createElement(XMLDocument.java:114)
com.glide.util.XMLDocument.createElement(XMLDocument.java:71)

Thanks for clarifying.
Is it possible for you to share your code here?

-Tanaji
Please mark reply correct/helpful if applicable

Community Alums
Not applicable

Hello
        I am also have same requirement.Can you please share your sample code.