How to Set Mandatory Fields in a Scripted REST API without data policies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2023 10:44 PM
Hello,
I wanted to know how to set mandatory fields through a scripted REST API without enforcing a data policy. I am creating a record in a table based on the request of the payload. I want to make some fields mandatory, only then it should accept the request, else it should throw an error.
Please let me know how to achieve this.
Thanks in Advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2023 11:23 PM
I am creating a glideRecord of that table, initializing it, assigning the values I get from payload and inserting!
var gr = new GlideRecord('Staging_table');
gr.initialize();
gr.requester = requester;
//Other values
gr.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2023 11:06 PM
if not using data policy then in script you can check if the incoming request has those fields with value or not
check for empty string
can you share script which you tried?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2023 11:08 PM
if (!domain || !intent || !requester) {
message = 'Invalid Request. No Value Received for ';
status = 'Bad Request';
I am trying something like this. But in the REST API Explorer the response is showing 200 OK but in the body it is showing Bad Request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2023 11:09 PM
@Ankur Bawiskar Thanks for your reply!
Can I use current.setAbortaction(true) to stop it from getting updated? Can I throw error for each and every mandatory fields as well?
Your help is highly appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2023 11:28 PM
it looks like you are inserting record into staging table using scripted rest API and that staging table has transform map etc to map with target table
you can use onBefore transform on that staging table and check for empty fields
It will run for each row you insert into staging table
something like this
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
if(source.u_name.nil() || source.u_email.nil()){
ignore = true;
log.info("Row ignored as fields are not filled");
}
})(source, map, log, target);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader