Is there a way to catch data policies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 05:45 AM
Hi all,
I did follow the course to create Record Producers (https://developer.servicenow.com/dev.do#!/learn/courses/sandiego/app_store_learnv2_aescreateappfromscratch_sandiego_create_an_app_from_scratch_with_app_engine_studio/CIT_Module_CatalogItem_sandiego/CIT_Objectives_CatalogItem_sandiego) and wanted to use it with a scripted REST API resource. I did apply a data policy on the "To Do" table to make the project mandatory :
I did copy the code from https://community.servicenow.com/community?id=community_question&sys_id=c5440f29dbd8dbc01dcaf3231f961942 to create a Record Producer class and call it from my script on the resource with following code :
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var rpUtil = new global.Base_RecordProducer('66d0579897211110cd9e7b771153afd5');
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(1);
rpUtil.setVariable('to_do_item', 'Complete my work');
rpUtil.setVariable('project', 'a68b471497211110cd9e7b771153aff4');
rpUtil.setVariable('importance', 'not_important');
rpUtil.setVariable('due_date', gdt);
rpUtil.setVariable('send_reminder', true);
var newRecordId = rpUtil.submit();
var OKbody = {};
OKbody.todo = newRecordId;
response.setBody(OKbody);
} catch (err) {
gs.info('REST API Error ' + err.toString());
response.setError(new sn_ws_err.BadRequestError('Error : ' + err));
}
})(request, response);
But when I try to call this script by commenting out the line setting the project variable I don't go in the catch part of the script, meaning that the exception is not thrown (and newRecordId is null).
When I try the same script in a background, I get :
Background message, type:error, message: Data Policy Exception: The following fields are mandatory: Project
Note that when I use the REST API, after going into the UI I have the exception message appearing :
My question is : is there a way to catch the Data Policy Exception ?
Thanks,
Cédric
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2022 08:01 AM
Hi,
So when you are using Cart API then it's not showing data policy exception?
Can you share the complete script include for "Base_RecordProducer"
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2022 12:53 PM
Hi Ankur,
In attach the "Base_RecordProducer" script include and the background script I used to test it. In the background script the line to set the project project is in comment. When you run it it will state that a record is added but this is not the case.
Regards,
Cédric
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2022 08:02 AM
Hi,
As per my understanding data policy works for fields but it seems you are trying to set variable of record producer
Can you share complete background script as well?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2022 01:08 PM
Note that the record producer is not the problem here I think. If you have a data policy to make a field mandatory and try to insert a record the problem remains the same.
Here is a background I tried and have the same problem :
try {
var todo = new GlideRecord('x_792818_to_do_to_do');
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(1);
todo.to_do_item = 'Complete my work';
//todo.project = 'a68b471497211110cd9e7b771153aff4';
todo.importance = 'not_important';
todo.due_date = gdt;
todo.send_reminder = true;
todo.insert();
gs.info('New record ' + todo.sys_id);
} catch (err) {
gs.info('Error ' + err.toString());
}
This returns :
Background message, type:error, message: Data Policy Exception: The following fields are mandatory: Project
*** Script: New record e0eeaaff97bd9110cd9e7b771153af16
But should go in the catch section IMHO.
Regards,
Cédric