How to create records in table using postman data

Emp 53
Tera Contributor

Hi All,

Below is the sample data that i am trying to send through postman to create record in table

{

subnet : '{"subnet1":"subnet-004834", "subnet2":"subnet-04f6908", "subnet3":"subnet-0f8345", "subnet4":"subnet-08198a4"}';

above data if not there in table i need to create 4 new records in table. if any record is there i need to skip that record. and some times the count is more than 4 as well. so how can i consider dynamically.

i am considering id (bold data) is an unique record for creating new record.

Can any one help me on this?

 

2 REPLIES 2

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Emp 53 ,

 

Please try this scripted rest code below:

 

The sample data mentioned in the question is not a valid JSON, it should look like:
{ "subnet": { "subnet1": "subnet-004834", "subnet2": "subnet-04f6908", "subnet3": "subnet-0f8345","subnet4": "subnet-08198a4" } }

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body;
    var requestData = requestBody.data;

    var reqData = JSON.stringify(requestData);
    var parser = new JSONParser();

    var parsedData = parser.parse(reqData);

    var subnets = parsedData.subnet;

    for (var key in subnets) {

        var subnetNumber = subnets[key];
        var subnetTableGR = new GlideRecord('subnet_table');
        subnetTableGR.addQuery('number', subnets[key])
        subnetTableGR.query();
        if (!subnetTableGR.next()) {

            subnetTableGR.newRecord();
            subnetTableGR.setValue('number', subnets[key]);
            subnetTableGR.insert();

        }

    }
})(request, response);

 

If my answer has helped with your question, please mark it as correct and acccepted solution.

 

Thanks,
Karan

 

Rahul Kumar17
Tera Guru

Hi Emp,

 

Assuming that the table has a field named "id" which is used as the unique identifier for each record, you can use the REST API "GET" method to query the table and check if a record with the provided "id" value already exists in the table. If it exists, you can skip creating a new record for that data. If it doesn't exist, you can use the "POST" method to create a new record.

Here is an example of how you can achieve this in Postman:

1. Send a "GET" request to the table API endpoint with the filter condition based on the "id" field to check if the record exists:

https://<instance_name>.service-now.com/api/now/table/<table_name>?sysparm_query=id=<id_value>

 

Make sure to replace the "<instance_name>", "<table_name>", and "<id_value>" with the appropriate values for your ServiceNow instance, table name, and id value respectively. This will return a response with the record details if the record exists, or an empty response if the record doesn't exist

2. Use the response status code to check if the record exists. If the status code is 200, it means the record exists and you can skip creating a new record. If the status code is 404, it means the record doesn't exist and you can create a new record.

3. To create a new record, send a "POST" request to the table API endpoint with the data for the new record in the request body:

https://<instance_name>.service-now.com/api/now/table/<table_name>

 

In the request body, include the data for the new record in JSON format, similar to what you have provided in your example. Make sure to replace the "<instance_name>" and "<table_name>" with the appropriate values for your ServiceNow instance and table name respectively.

You can use a loop to iterate through all the data and perform the above steps to create or skip records dynamically.

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar