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

larstange
Mega Sage

Hi



You are using a REST POST message. A POST message will always create a new record.


You should have two REST messages -



  1. A POST which you run when an insert is done into your CMDB. You will have to save the sys_id from the opposite record on the CI
  2. A PUT which you run when an update is done in your CMDB. You use the sys_id if the opposite record to update the record.

Hi Lars,



Thanks for your response.



I will create the PUT method under the same REST message, bit confused about BR, do I need to incorporate the PUT code in the same Business rule or have to create separate one ?



Thanks,


Mittal Modh.


Thanks

You can do it in the same business rule that runs on both insert and update



you can do a



if (current.operation == "insert")


        callInsertFunction()


else if (current.operation == "update")


        callUpdateFunction()


GPC Sys ID (u_gpc_sys_id) is a custom field on staging table of target CMDB where I am populating the "sys_id" of the records present in source table to identify the source-destination relationship.



however to accomplish this requirement i am trying to call GET method on target table and based on the GET response I will call PUT or POST in BR.



Do you think is this the correct approach or do you have any other suggestions ?


Thanks