
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-10-2021 12:44 AM
About this Article
API - https://<yourinstance>.service-now.com/api/sn_sc/servicecatalog/items/<cat_item_sysid>/order_now
When using Service Catalog REST API (OOTB) for creating a request, it only checks variable definition for a mandatory field and returns error stating that "400 Bad request" when an empty value passed to a mandatory variable as below. But it will not say which variable is required/mandatory in the response. It also will not check the UI Policies to determine what else are required fields based on given request body.
OOTB Ordernow API will not check
- The API will not give you the name of the variable while checking the mandatory field from the definition.
- The API will not check the UI Policies to determine when a variable is mandatory based on other variable value.
Use Case
I have a catalog item called "Ordernow Sample Catalog" that looks like this. Here when I select "Select your option" as "Business Justification", Business Justification field is mandatory and when I select "Additional Comments", Additional Comments field is mandatory as shown below.
Note: "Select Your Option" has 2 Options i.e.
- Label = Business Justfication - Value = bj
- Label = Additional Comments - Value = ac
So, now if I use the OOTB API and try to create a request without Additional Comments / Business Justification, it definitely creates a request like this. But actually, it needs to throw an error stating when "Select your option" is Business Justification, then Business Justification field value is required.
API - https://<yourinstance>.service-now.com/api/sn_sc/servicecatalog/items/<cat_item_sysid>/order_now
Payload
{
"sysparm_quantity": "1",
"variables": {
"select_your_option": "bj", //Business Justification background value for this selectbox
"business_justification": "",
"additional_comments": ""
}
}
Now, we will see how the Ordernow Wrapper Utility will work and overcome the above issues with OOTB.
Ordernow Wrapper Utility
This article explains a way on how you can overcome the issues with OOTB Ordernow api to check mandatory fields including its UI Policies.
How it works
- Once you submit the payload/request body via this utility/api, it checks all the mandatory variables that are required by its definition. It reports in its response on which variable is missing the value.
- It also checks the UI Policies and determine what other variables are required based on the input provided in the request body.
It will not create the request if the above 2 parameters are not met. This will be very useful when you do any automation activities to create the request via API.
Examples
Wrapper API -
https://<instance name>.service-now.com/api/40225/ordernow_wrapper/submit_order
Scenario#1 - Submitting without providing any variable value
Request Body
{
"sysparm_quantity": "1",
"cat_item": "ae0c0c092fd530102dab2aa62799b69c",
"variables": {
"select_your_option": "",
"business_justification": "",
"additional_comments": ""
}
}
Response
{
"result": {
"Missing_RequiredFields": [
{
"Field_Name": "select_your_option",
"Field_Label": "Select Your Option",
"Condition": ""
},
{
"Field_Name": "additional_comments",
"Field_Label": "Additional Comments",
"Condition": " And When \"Select Your Option\" = ac Then"
},
{
"Field_Name": "business_justification",
"Field_Label": "Business Justification",
"Condition": " And When \"Select Your Option\" = bj Then"
}
]
}
}
If you observe the above response, it says that
Select your option is a mandatory field by definition
Additional comments are required when Select your option is "ac"
Business Justification is required when Select your option is "bj"
Scenario#2 - Submitting without providing a value for "Additional Comments" field
Request Body
{
"sysparm_quantity": "1",
"cat_item": "ae0c0c092fd530102dab2aa62799b69c",
"variables": {
"select_your_option": "ac",
"business_justification": "",
"additional_comments": ""
}
}
Response
{
"result": {
"Missing_RequiredFields": [
{
"Field_Name": "additional_comments",
"Field_Label": "Additional Comments",
"Condition": " And When \"Select Your Option\" = ac Then"
}
]
}
}
If you observe the above response, it says that when Select your option value is "ac",
then Additional comments are required. In this case also, it will not generate/create the request.
Scenario#3 - Submitting with providing a value for "Additional Comments" field
Request
{
"sysparm_quantity": "1",
"cat_item": "ae0c0c092fd530102dab2aa62799b69c",
"variables": {
"select_your_option": "ac",
"business_justification": "",
"additional_comments": "via API"
}
}
Response
{
"result": "{\"result\":{\"sys_id\":\"148dce492f1d301053cca55df699b6f7\",\"number\":\"REQ0010002\",\"request_number\":\"REQ0010002\",\"request_id\":\"148dce492f1d301053cca55df699b6f7\",\"table\":\"sc_request\"}}"
}
In the above scenario, it passes all the required fields validation and given the control to the OOTB api
to create the request.
Note: This utility is just a wrapper on top of the OOTB api to check all mandatory fields required
before creating a service request.
Download from the below link and provide your feedback to improve this utility.
Please mark it as helpful & bookmark the article if it works for you.
Link to download
Thanks,
Narsing
- 3,357 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Narsing
I tried your scripts and it worked well with your example. I was very excited about it. Then I tried 2 things.
1) I added a new variable named "Test" that is NOT mandatory. Then I created a new UI Policy named "Mandatory Fields" and I set a new Policy UI Action to mark "test" variable as mandatory (with no conditions).
The API response body did not return "test" as a mandatory field.
2) I disabled all UI Policies and unchecked the mandatory field for "Select Your Option" which essentially makes all the variables on the catalog item optional. Then I ran the call and it returned "result": "Method failed: (/api/sn_sc/servicecatalog/items/ae0c0c092fd530102dab2aa62799b69c/order_now) with code: 500"
Do you think we can fix #1 and #2? Thanks