REST API - Contact created without mandatory field

sahilkhanna
Tera Expert

Below is the API request I use to create a Contact. 

curl --location 'https://{instanceUrl}.service-now.com/api/now/table/customer_contact' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer DiJ8YZG3S60-...A9FUxg' \
--data-raw '{
        "first_name": "Ophelia",
        "last_name": "Kulas"
}'

The contact is created. However, the request should have resulted in an error as the mandatory "email" and "account" fields aren't in the API request.

 

The fields are blank when I open the created contact in the ServiceNow UI.

Screenshot 2025-01-17 at 16.22.48.png

 

When I Update the contact from the UI without filling in the mandatory fields, I get the error "The following mandatory fields are not filled in: Account, Email".

Screenshot 2025-01-17 at 16.26.11.png

 

 

Could you help me with the questions below?

  • Is there a reason why the API ad UI behave differently?
  • When the mandatory fields are missing, can the API return an error instead of creating the contact?
2 ACCEPTED SOLUTIONS

Amit Verma
Kilo Patron
Kilo Patron

Hi @sahilkhanna 

 

REST API will only give you the mandatory fields missing error if those fields are marked as mandatory in the dictionary record or a data policy is being enforced on them. In your case, probably, the Account and Email fields are being made mandatory via UI Policy that's why no error being thrown. You can refer below discussion to understand it better and derive a solution for your problem:

https://www.servicenow.com/community/developer-forum/required-field-not-throwing-error-in-rest-api/m...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

@sahilkhanna 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@sahilkhanna 

API will give error if fields are marked as via data policy. create data policy and check the REST/SOAP checkbox as true

UI will stop you because it's mandatory in front end

when Table API is hit, it means a GlideRecord insert happens and doesn't check for mandatory fields.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Amit Verma
Kilo Patron
Kilo Patron

Hi @sahilkhanna 

 

REST API will only give you the mandatory fields missing error if those fields are marked as mandatory in the dictionary record or a data policy is being enforced on them. In your case, probably, the Account and Email fields are being made mandatory via UI Policy that's why no error being thrown. You can refer below discussion to understand it better and derive a solution for your problem:

https://www.servicenow.com/community/developer-forum/required-field-not-throwing-error-in-rest-api/m...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

@Amit Verma, I followed the thread and created a Data Policy by giving the fields to validate.

Screenshot 2025-01-17 at 18.20.25.png

 

 

Below are a few questions that I have. These are not answered in the thread.

  1. If new mandatory field(s) are added to the customer_contact table using the Form Builder, will those mandatory fields have to be added to the Data Policy as well so they can be validated in the REST API?
  2. For the email field, the sys_dictionary table returns the "max_length" as 100. When I enter a 104-character email in the API, a new record is created (IT SHOULD FAIL). But in the UI, I'm not allowed to type more than 100 chars. How do I handle this and other such validations in the Data Policy?
  3. I also plan to create Cases, Incidents, etc. using the REST API. Will Data Policies need to be created for all these?

Can you help with these questions, please?

@sahilkhanna 

responses inline

 

  • If new mandatory field(s) are added to the customer_contact table using the Form Builder, will those mandatory fields have to be added to the Data Policy as well so they can be validated in the REST API? -> yes correct, add those
  • For the email field, the sys_dictionary table returns the "max_length" as 100. When I enter a 104-character email in the API, a new record is created (IT SHOULD FAIL). But in the UI, I'm not allowed to type more than 100 chars. How do I handle this and other such validations in the Data Policy? - you need to use before insert BR on contact table and stop the record insertion
  • I also plan to create Cases, Incidents, etc. using the REST API. Will Data Policies need to be created for all these? - data policies should be used for similar requirement on other tables

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader