How to send User field detail via REST API for a catalog item ?

Hritik
Tera Expert

Hi Community,

 

I am creating a catalog item request via REST API. 

I used below method in REST API Explorer:

 

Namespace: sn_sc

API Name: Service Catalog API

POST Method: Buy Item

 

Request Body: 

{
"sysparm_quantity": "1",
"variables":
{
"employee_to_offboard": "David Anderson",
"offboard_type": "Retirement",
"employment_end_date": "2024-03-22",
"notice_period": "90"
}
}

 

Request is being created successfully but the field "employee_to_offboard" which is Reference field for User table is not populating via this method.

If use sys_id then it works, but I need to send this payload for third party applications who only has access to user names and not sys_ids. 

 

Could you please suggest me how can we populate user field by user name instead of sys_id ?

 

 

Thanks in Advance,

Hritik

6 REPLIES 6

Harish KM
Kilo Patron
Kilo Patron

Hi @Hritik I assume in that case you need to retrieve user name response and do the glide record on user table and match the user name with username and return the sysid to insert.

your third party tool should handle this.

Regards
Harish

Hi @Harish KM 

 

Could you please let me know what exact steps the third party needs to do to get sys_id ?

Adrian Ubeda
Mega Sage
Mega Sage

Hello Hritik, 

If you don't want to do any additional configuration/development this should be treated in the 3rd party application and then call post method using the proper sys_id. However, I think that if you follow this steps you'll be able to change that value (Although, as I said it's not the best way):

1) Create a business rule onBefore on sc_item_option table, this table stores instantiation of values from variables in RITM. Retrieve data before it goes blank when try to insert name instead of sys_id

2) Be strict with conditions for avoiding triggering anything unnecessarily, in this example I've selected the field in the table, in my case question = requested for (pay attention to item:

AdrianUbeda_0-1706880186076.png

3) I've just added a simple script in advanced for changing this value querying sys_user table:

 

var userName = current.value;
    var grUser = new GlideRecord('sys_user');
    grUser.addQuery('name', '=', userName);
    grUser.query();

    if (grUser.next()) {
        grUser.getUniqueValue();
    }

	current.value = grUser.getUniqueValue();
	

 

This is configured only for insert action, if further actions it's required try to achieve this being more restrict with conditions and filters.

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

Amit Verma
Kilo Patron
Kilo Patron

Hi @Hritik 

 

It expects you to pass sys_id instead of display name to the field "employee_to_offboard" . For getting the sys_id, you could make use of another REST API call on sys_user table as below :

 

AmitVerma_0-1707225002065.png

 

AmitVerma_1-1707225022698.png

 

This API call will return the user record sys_id which you can pass to your Buy Item API. 

 

AmitVerma_2-1707225101170.png

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.