sending a reference field data from script include

kundusuman328
Tera Contributor

I am working with a catalog item. I have a delivery location field which is referenced with location table. Now I want to auto populate the delivery location field based upon a requested_by field. The delivery location should be requested_by user's location. I tried to achieve this using script include, but when I am alerting the location value sent from script include it is showing "[object] [Object]".

How do I send the location value from script include?

3 ACCEPTED SOLUTIONS

In your script include can you do:

user.location.name.toString();

Make sure the user you are checking for has location populated in location field in his user record.


Raghav
MVP 2023

View solution in original post

jaheerhattiwale
Mega Sage
Mega Sage

@kundusuman328 Please try below:

 

in script include add the toString() to location as well. As reference field also is a string field in backend which stores the sys_id of the referred element. 

 

userDetails.location = user.location.toString();

 

One tip:

Please use the get function in a if block, that is best practice recommended by ServiceNow. And no need to add the 'sys_id' as first param when we are adding sys_id to the get function. if its some other field than sys_id then we need to pass the field name as first param

 

So please change the get function as below

 

if(user.get(userID)){

//Add lines to populate userDeatails object

}

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

@kundusuman328 did you check what is the display field on location table.

This issue only occurs if location value is not populated in your user table. Your code is correct, if it is fetching other values, it should fetch location as we..

 


Raghav
MVP 2023

View solution in original post

10 REPLIES 10

RaghavSh
Kilo Patron

You are alerting user.location before parsing it.

Try alerting user.loaction after the JSON.parse() line i.e. after line no. 19 in client script.

 

Also in your script include .toString() is missing from user.location, you need to add that.

 

 


Raghav
MVP 2023

I tried what you instructed but that did not worked. Now it is showing a blank alert.

 

@kundusuman328 are you getting other values in alert?

Also does that specific user have location value in user table?


Raghav
MVP 2023

Yes I am getting all the values in alert except the reference fields(I tried with other reference field also). And yes, the location value exists in user table. I am using out of the box user table with no modifications