How to check catalog client side validation on catalog API

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 04:14 AM
Using Buy Item (POST) catalog API to submit a catalog item. This item has start date & end date. I have written an onsubmit catalog client script to validate the dates not be in the past. But the validation is not being evaluated through catalog API request. Any idea how to enforce this validation on API side.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 04:42 AM
Hello @Community Alums
When catalog items are submitted through ServiceNow’s Catalog API (for instance, using the /api/sn_sc/v1/servicecatalog/items/{sys_id}/order_now endpoint for ordering a catalog item), client-side scripts, including onSubmit catalog client scripts, are not executed. This is because the API request bypasses the client-side processing and interacts directly with the server-side components. Therefore, any validation that occurs on the client-side through JavaScript won’t be evaluated when an item is ordered through the API.
To enforce validation on the API side, you will need to implement validation logic on the server side. This can be achieved using Business Rules or Script Includes that are invoked as part of the API process. Given that you’re dealing with date validation for a catalog item, you might want to use a Business Rule that triggers before the record is inserted or updated (a ‘before’ Business Rule).
Configure the Business Rule:
- Name: Descriptive name
- Table: 'sc_req_item' for requested items, but could vary based on your setup.
- When to run: Before/ Insert
// Assuming ‘start_date’ and ‘end_date’ are the field names
var startDate = new GlideDateTime(current.getValue('start_date'));
var endDate = new GlideDateTime(current.getValue('end_date'));
var now = GlideDateTime();
if (startDate < now || endDate < now) {
// Cancel the current operation
current.setAbortAction(true);
// Optionally, you can add an error message (though this may not always be supported/seen by API clients)
gs.addErrorMessage('Start date and end date cannot be in the past.');
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards
Deepak Sharma