How to Set Mandatory Fields in a Scripted REST API without data policies

Lavanya R1
Tera Contributor

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

13 REPLIES 13

Lavanya R1
Tera Contributor

@Ankur Bawiskar Could you please answer this?

 

If you dont want to use data policies which are OOB , use ACLs.

If your requirement is that you dont want to use servicenow OOB scripts , then you will have to handle it through script.

 

Below could be one of custom solution:

1. Create a staging table which will store your payload.

2. Write an after insert BR on your staging table which will insert the record in your target table.

3. In your BR parse the JSON and check if the JSON contains the required fields.

4. If those field values exist in JSON insert the record in target table else abort the BR.


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023

@RaghavSh Thanks for your reply, I am storing the payload in a staging table. If the mandatory fields are missing in the payload, the request should be aborted and the insert shouldnt happen, it should throw appropriate error. Can I achieve this inside the Scripted REST API itself?

how are you inserting the record from staging to target table? can you share that script?


Raghav
MVP 2023