JSON Request body for HR Case Creation

Pradeep63
Tera Contributor

Hi,

 

I am trying to create an HR Case through REST API using the Out of the Box Scripted REST API "HR Case Creation" available in ServiceNow HRSD.

 

I have come up with the below JSON request body for making the REST POST call:

 {
    "table":"sn_hr_core_case",
    "fields": [
    {
        "hr_service":"xxxxxxxxxxx",
        "opened_for":"xxxxxxxxxx",
        "short_description":"xxxxxxx",
        "description":"Test"
    }],
    "includeErrors":"true"
    
}
The above request body creates an empty Case without populating the required fields like HR Service, Opened For etc.
This is because the OOB scripted REST API in SNOW is using a condition "if (fields.hasOwnProperty('hr_service'))" to control the flow to the code which does all the field assignment. And for the above JSON request body this condition is evaluating to false, hence the field assignments are not happening and the case is getting created without the required fields populated.
 
I tweaked the JSON request body as shown below, this request passes the condition "if (fields.hasOwnProperty('hr_service'))" but it fails an other condition in the scripted REST API
"var serviceGr = new GlideRecord('sn_hr_core_service');
if (!serviceGr.get(fields['hr_service'].value))".
 
 {
    "table":"sn_hr_core_case",
    "fields"
    {
        "hr_service":"xxxxxxxxxxx",
        "opened_for":"xxxxxxxxxx",
        "short_description":"xxxxxxx",
        "description":"Test"
    },
    "includeErrors":"true"
    
}
 
I am looking for inputs for building JSON Request body in such a way that both the if conditions I have mentioned above are met and the case gets created correctly with all the required fields populated.
 
Thanks in advance for your inputs.
1 ACCEPTED SOLUTION

_ _ _ _
ServiceNow Employee
ServiceNow Employee

The correct format for "fields" is specified in the createTask API as "@param fields Object Map of field objects, eg { 'hr_service': { value: 'SYS_ID' } }". This should look like:

{

"table": "sn_hr_core_case",

"fields": {

   "hr_service" " { "value": "xxxxxxxx" },

   "opened_for": { "value": "xxxxxxxx" },

   "short_description": { "value": "xxxxxxx" },

   "description": { "value": "xxxxxxx" }

},

 

 

Some fields may need additional values in the object besides "value" (the API documentation mentions as "setAsDisplayValue" option), but this should work for most fields.

 

Your first example did not work as it used array notation ([]) for the "fields" property, when it should just be an object ({}).

Your second example did not work as it used a String ("xxxxx") instead of an object ({ "value": "xxxxx"}) for each field property in "fields" object.

 

An example JSON from a test instance with test data:

{

"table":"sn_hr_core_case",

"fields":{

      "opened_for":{"value":"62826bf03710200044e0bfc8bcbe5df1"},

      "subject_person":{"value":"62826bf03710200044e0bfc8bcbe5df1"},

      "work_notes":{"value":"xxxx"},

      "hr_service":{"value":"6628cde49f331200d9011977677fcf0b"}

   }

}

View solution in original post

7 REPLIES 7

Hi @Pradeep63 ,

I am not sure about OOTB one. But it will not wok for all the JSON formats.

I will suggest refer OOTB APIS but don't change any functionality on that. Understand and Implement for your format.

Based on the JSON format we need to design the Scripted REST API.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi @Pradeep63 ,

If your issue got resolved close the question by Accepting solution and hit thumb icon.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

_ _ _ _
ServiceNow Employee
ServiceNow Employee

The correct format for "fields" is specified in the createTask API as "@param fields Object Map of field objects, eg { 'hr_service': { value: 'SYS_ID' } }". This should look like:

{

"table": "sn_hr_core_case",

"fields": {

   "hr_service" " { "value": "xxxxxxxx" },

   "opened_for": { "value": "xxxxxxxx" },

   "short_description": { "value": "xxxxxxx" },

   "description": { "value": "xxxxxxx" }

},

 

 

Some fields may need additional values in the object besides "value" (the API documentation mentions as "setAsDisplayValue" option), but this should work for most fields.

 

Your first example did not work as it used array notation ([]) for the "fields" property, when it should just be an object ({}).

Your second example did not work as it used a String ("xxxxx") instead of an object ({ "value": "xxxxx"}) for each field property in "fields" object.

 

An example JSON from a test instance with test data:

{

"table":"sn_hr_core_case",

"fields":{

      "opened_for":{"value":"62826bf03710200044e0bfc8bcbe5df1"},

      "subject_person":{"value":"62826bf03710200044e0bfc8bcbe5df1"},

      "work_notes":{"value":"xxxx"},

      "hr_service":{"value":"6628cde49f331200d9011977677fcf0b"}

   }

}