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

Pavankumar_1
Mega Patron

Hi @Pradeep63 ,

ServiceNow is the target system(Inbound) are you getting the data from the source system which is outbound to that system?

is this your Final JSON?

 {
    "table":"sn_hr_core_case",
    "fields": 
    {
        "hr_service":"xxxxxxxxxxx",
        "opened_for":"xxxxxxxxxx",
        "short_description":"xxxxxxx",
        "description":"Test"
    },
    "includeErrors":"true"
}

 

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

Yes, ServiceNow is the target system. Source system is a third party system which will initiate the REST call to create HR Case in SNOW.

 

This is not the final JSON. I am trying to create a HR case with these four fields populated on the case.

Hi @Pradeep63 ,

Okay.

If you are trying with array json which 1st one use below to get parameters and based on your requirement you can add conditions on scripted REST API using Glide record.

var parameters = request.body.data;
var tablename = parameters.table.toString();
var fieldnames = this.tablename.fields[0];
var hr_service=fieldnames.hr_service.toString();
var opened_for=fieldnames.opened_for.toString();
var short_description=fieldnames.short_description.toString();
var description=fieldnames.description.toString();

2. If you are using 2nd JSON with out array use below on script REST API to get JSON values and Glide based on requirement.

var parameters = request.body.data;
var tablename = parameters.table.toString();
var fieldnames = this.tablename.fields;
var hr_service=fieldnames.hr_service.toString();
var opened_for=fieldnames.opened_for.toString();
var short_description=fieldnames.short_description.toString();
var description=fieldnames.description.toString();
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi @Pavankumar_1 

 

Thanks for your inputs.

 

Since this is an OOB Scripted REST API from ServiceNow, I dont want to make any customizations. I am checking to see if I can build a JSON Request body such that it will satisfy both the conditions I have mentioned in my question above.

 

Thanks again for your inputs.