How to create a request on behalf of another user - Service Catalog

Faiz Firaz
Kilo Contributor

Hi,

I am using the Service Catalog API (namespace : "sn_sc") to create a service request (REQ#) with a request item (RITM#) attached to it using REST API explorer. Below is the request body used ( only mandatory variables for this item is used) ;

{ "sysparm_quantity" : 1,

"variables" : {
"requestor" : "test user",
"project_details" : "test project",
"week_to_unfreeze_time" : "08-03-2021",
"reason_for_unfreeze" : "reason comes here"
}
}

From the above, the request gets created for the user who is sending the request. However, I want to raise this request on behalf of another user. How can this be done?

What I tried :

  • Passing the "requested_for" variable both inside and outside of the "variables" field in the request. 
  • Tried passing the user name and also, the user id in requested_for field.

How can this be done please.

Thanks,

Faiz.

 

1 ACCEPTED SOLUTION

@Faiz Firaz 

Please remember this point

1) for reference variable you should always send the sys_id

2) for choice variable always send the choice value

3) for checkbox send true/false

To set the OOB requested_for field on REQ with your user add sysparm_requested_for

{ "sysparm_quantity" : 1,

"sysparm_requested_for" : "4ae3ab0753101200ed11da86a11c0881"

"variables" : {
"requestor" : "UserSysIdHere",
"project_details" : "test project",
"week_to_unfreeze_time" : "08-03-2021",
"reason_for_unfreeze" : "reason comes here"
}
}

Regards
Ankur

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

26 REPLIES 26

Hi Faiz,

Sorry, there was a typo in script.

The first line you have use as a normal script before you JSON and use remaining script as JSON Object.

I have corrected the last line in JSON. Please try to use below code.

 

var user = GlideUser.getUserByID('sys_id of user');   ///replace sys_id here

{
"variables" : {
"requestor" : "test user",
"project_details" : "test project",
"week_to_unfreeze_time" : "08-03-2021",
"reason_for_unfreeze" : "reason comes here"
},
"sysparm_quantity" : 1,
"requested_for" : user.sys_id.toString(),
"requested_for_display_name" : user.getDisplayValue() 
};

 

Thanks,

Anil

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Appli
Mega Sage
Mega Sage

Hi, may be you can consider adding a new reference variable (pointing to User table) to your catalog item with "requested_for" name, and specify this variable in API call as per described before

variables" : {
"requestor" : "test user",
"project_details" : "test project",
"week_to_unfreeze_time" : "08-03-2021",
"reason_for_unfreeze" : "reason comes here"
"requested_for" : "<sys_id>"
}

Hope it helps

 

Hope it helps

Faiz Firaz
Kilo Contributor

Hi,

Thanks for the support everyone. Checking the XML made me understand what's wrong. Passing the sys_id of the user in the requestor field resolved the issue.

{ "sysparm_quantity" : 1,

"variables" : {
"requestor" : "4ae3ab0753101200ed11da86a11c0881",
"project_details" : "test project",
"week_to_unfreeze_time" : "08-03-2021",
"reason_for_unfreeze" : "reason comes here"
}
}

Thanks for helping,

Faiz.

@Faiz Firaz 

Please remember this point

1) for reference variable you should always send the sys_id

2) for choice variable always send the choice value

3) for checkbox send true/false

To set the OOB requested_for field on REQ with your user add sysparm_requested_for

{ "sysparm_quantity" : 1,

"sysparm_requested_for" : "4ae3ab0753101200ed11da86a11c0881"

"variables" : {
"requestor" : "UserSysIdHere",
"project_details" : "test project",
"week_to_unfreeze_time" : "08-03-2021",
"reason_for_unfreeze" : "reason comes here"
}
}

Regards
Ankur

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Faiz Firaz 

Thank you for marking my response as helpful.

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader