- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2022 03:42 AM
Hi,
I have created a Scripted REST API to insert data from 3rd party to ServiceNow Vendor Contact table.
I got another requirement to check for the "first name", "last name" and "email" which should not be empty. If these fields are empty then that record should not be inserted. How can I check for empty values of above fields and stop inserting the record if these fields are empty.
Here is the script I am using for inserting and updating the record into Vendor Contact table. Where I need to use the condition for filtering empty field values?
Thanks in advance!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2022 11:12 PM
You can try this code. This check whether mandatory fields are present. Else return error message and skip the operation
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var reqdata = request.body.data;
var parsedData = reqdata;
// -- Below lines check whether mandatory fields has value. Else throw error and ignore operation
var missingFields = [];
if (!parsedData.hasOwnProperty('first_name') || parsedData.first_name == "") {
missingFields.push("first_name");
}
if (!parsedData.hasOwnProperty('last_name') || parsedData.last_name == "") {
missingFields.push("last_name");
}
if (!parsedData.hasOwnProperty('email') || parsedData.email == "") {
missingFields.push("email");
}
if (missingFields.length > 0) {
return new sn_ws_err.BadRequestError("Missing mandatory fields : " + missingFields.join(","));
}
// -- End of customization
gs.info('parseddata ' + parsedData);
var vendorGr = new GlideRecord('vm_vdr_contact');
vendorGr.addQuery('name', parsedData.name);
vendorGr.query();
gs.info('vendor name', +parsedData.name);
if (vendorGr.next()) {
vendorGr.name = parsedData.name;
vendorGr.first_name = parsedData.first_name;
vendorGr.last_name = parsedData.last_name;
vendorGr.email = parsedData.email;
vendorGr.update();
} else {
vendorGr.initialize();
vendorGr.name = parsedData.name;
vendorGr.first_name = parsedData.first_name;
vendorGr.last_name = parsedData.last_name;
vendorGr.email = parsedData.email;
vendorGr.insert();
}
var body = {};
body.sys_id = vendorGr.sys_id;
body.success = "SUCCESS";
response.setBody(body);
})(request, response);
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2022 03:00 AM
I got it Palani Kumar. Thank for your answer!