POST Scripted REST API not inserting Empty record

JRY
Mega Guru

Hi,

 

I'm trying to insert a record into a ServiceNow via a third-party application using the POST method using a scripted REST API. I'm testing with the POSTMAN application, and I'm finding an issue where the records are being inserted but the fields are empty. I checked the field names, and they appear to be correct. I also attempted to verify the JSON file data, and it appears to be correct. I'm not sure what the problem is, but it's not updating the data on the corresponding fields once the record is created. I've included a script I wrote for a scripted REST API, as well as a JSON file for testing. Can you tell me what the mistake is in the below script?

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        // Get data from the request body
        var requestData = request.body.data;

        // Insert a record into the target table using GlideRecord
        var targetTable = 'sn_hr_core_hr_field_changes';
        var newRecord = new GlideRecord(targetTable);
        newRecord.initialize(); // Initialize a new record
        newRecord.u_target_cost_center = requestData.u_target_cost_center;
        newRecord.u_first_name = requestData.u_first_name;
        newRecord.u_last_name = requestData.u_last_name;
        newRecord.u_current_pay_rate = requestData.u_current_pay_rate;
        newRecord.u_current_store_number = requestData.u_current_store_number;
        newRecord.u_userid = requestData.u_userid;
        newRecord.u_effective_date = requestData.u_effective_date;
        newRecord.u_target_pay_rate = requestData.u_target_pay_rate;
        newRecord.u_employee_id = requestData.u_employee_id;
        newRecord.u_target_job_code = requestData.u_target_job_code;
        newRecord.u_current_job_code = requestData.u_current_job_code;

        // Save the new record
        var recordSysID = newRecord.insert();

        // Check if the record was created successfully
        if (recordSysID) {
            // Respond with the newly created record's Sys ID
            response.setStatus(201);
            response.setBody({
                sys_id: recordSysID.toString(),
                message: 'Record created successfully'
            });
        } else {
            response.setStatus(500); // Internal Server Error
            response.setBody({
                error: {
                    message: 'Failed to create the record.',
                    detail: 'The record could not be inserted into the table. Check system logs for more details.',
                },
                status: 'failure'
            });
        }
    } catch (e) {
        gs.error('An error occurred while processing the request: ' + e);
        response.setStatus(500); // Internal Server Error
        response.setBody({
            error: {
                message: 'An error occurred while processing the request.',
                detail: e.toString(),
            },
            status: 'failure'
        });
    }

})(request, response);

 

 

 

{
  "data": {
    "u_target_cost_center": "95671",
    "u_first_name": "Test",
    "u_last_name": "RecordInserted",
    "u_current_pay_rate": "60640",
    "u_current_store_number": "06093",
    "u_userid": "lbrantle",
    "u_effective_date": "2024-01-05",
    "u_target_pay_rate": "92200",
    "u_employee_id": "422745",
    "u_target_job_code": "1843",
    "u_current_job_code": "431"
  }
}

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@JRY 

try this json request body and it will work

It should be a json object but in your above json request you are embedding object within object

{
    "u_target_cost_center": "95671",
    "u_first_name": "Test",
    "u_last_name": "RecordInserted",
    "u_current_pay_rate": "60640",
    "u_current_store_number": "06093",
    "u_userid": "lbrantle",
    "u_effective_date": "2024-01-05",
    "u_target_pay_rate": "92200",
    "u_employee_id": "422745",
    "u_target_job_code": "1843",
    "u_current_job_code": "431"
  }

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @JRY 
Can you apply the log in the variable.

requestData.u_target_cost_center;
requestData 

 


Thanks and Regards,

Saurabh Gupta

I have tried getting undefined.

JRY_0-1704816242153.png

 

Thanks,

JRY

 

Hi,
Can you please print 

requestData 

Thanks and Regards,

Saurabh Gupta

Hi Saurabh,

 

I have tried adding logs as you suggested

gs.info('u_target_cost_center: ' + requestData.u_target_cost_center);
gs.info('u_first_name: ' + requestData.u_first_name);
gs.info('RequestDATA: ' + requestData);

 

JRY_0-1704816504029.png

 



Thanks,

JRY