Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Reference Fields Showing Undefined in Scripted REST API

Lucky1
Tera Guru

Hello all,

 

I have created a SCRIPTED RESI API, and when clicked on "Explore REST API" related link from it, and when I give the sys_id of the group and Customer, in the logs it is giving undefined.

 

Lucky1_0-1747716605420.png

 

So, can you please help me in getting the values as the RITM is also not populating with the reference fields.

 

 

Regards,

Lucky

 

1 ACCEPTED SOLUTION

@Lucky1 

sorry but you didn't update the script as per what I shared

now that I know how request body looks, update as this and it will give you the value from JSON

var reqbody = request.body.dataString;

var parsedData = JSON.parse(reqbody);

var companyValue = parsedData.company_CM.toString();

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

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

how are you sending the json body? share that request body image or JSON here

also use this to parse, line 6 not required

var reqbody = request.body.dataString;

var parsedData = JSON.parse(reqbody);

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

Hello Ankur,

 

I am currently passing only one reference variable for testing purpose.

But when I open the RITM, in the variables section, the Customer (company_CM) variable is empty.

 

Lucky1_0-1747720730977.jpeg

 

Lucky1_1-1747720754749.jpeg

 

 

Regards,

Lucky

 

 

@Lucky1 

sorry but you didn't update the script as per what I shared

now that I know how request body looks, update as this and it will give you the value from JSON

var reqbody = request.body.dataString;

var parsedData = JSON.parse(reqbody);

var companyValue = parsedData.company_CM.toString();

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

Yes, great,

 

Need to add .toString()

 

Thanks a lot

 

 

 

Regards,

Lucky