POST Rest API to populate and update the target table

Mittal_M
Giga Guru

Hi Everyone,

I have a requirement of populating the data from one servicenow CMDB to the staging table of another servicenow CMDB whenever a new record is created or record is updated.

The staging table of another CMDB has two custom fields based on which another team will identify below things and will take care of inserting the data in their CMDB.

1. from which class the data is coming (for ex. cmdb_ci_server) - This is hard coded in Business Rule shown in the Script Below.

2. what is the sys_id of the current record.

Based on this requirement I am using Rest API and I have created the POST message from my instance and also created the Business rule to execute/call the POST. As per the requirement it is working fine and creating the records in the staging table of another CMDB but the only issue which I have is that whenever I update the record in my CMDB a new record / duplicate record is created in staging table instead of updating the record.

Kindly let me know what can be done so that I can achieve the requirement.

Below is the After Business Rule on Insert and Update

(function executeRule(current, previous /*null when async*/) {

try {

var r = new sn_ws.RESTMessageV2('Internal Server to Staging table Test', 'POST');

r.setStringParameterNoEscape('u_environment', current.u_environment); //Choice

r.setStringParameterNoEscape('u_priority', current.u_priority); //Choice

r.setStringParameterNoEscape('u_operatingsystems', current.u_operatingsystems); //String

r.setStringParameterNoEscape('u_owner', current.u_owner); //String

r.setStringParameterNoEscape('name', current.name); //String

r.setStringParameterNoEscape('short_description', current.short_description); //String

r.setStringParameterNoEscape('u_gpc_class', 'cmdb_ci_server'); //String

r.setStringParameterNoEscape('u_gpc_sys_id', current.sys_id); //String

//override authentication profile

//authentication type ='basic'/ 'oauth2'

//r.setAuthentication(authentication type, profile name);

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

}

catch(ex) {

var message = ex.getMessage();

}

})(current, previous);

Below are the screenshots

1. Staging Table and its fields,

find_real_file.png

2. Duplicate record entries in Staging Table.

find_real_file.png

Thanks
11 REPLIES 11

Yes, You can say that its table to table integration but between two different instance and environment, where the target CMDB data is populated by our CMDB data.


From the source side there are many CMDB tables like Server, NetGear, Software, Etc. which will be synced to only one table in target side which is the Staging table, and GPC Sys ID is a field of the Staging table which is capturing the "sysid" of the record coming from Source table.


Thanks

OK. Go via ImportSet API way.


1. Create a Import Set/Transform Map at Target Instance and that ImportSet table can be accessed via ImportSet Table/Import API REST or SOAP.


2. The same ImportSet table can be used as staging as it holds all the records before transforming. No need to manage other staging table.


3. Your transform map transforms this data into the Server/NetGear   etc.. tables.


4.   You can even import/transform SysID(source) to SysID (target). This field can be COALSCEd so that it will be inserted or updated based on existence.



Lets know if you need more clear points on solution.