How to set requested for in RITM using Cart APi is it possible?

Gowtham Kodali
Tera Contributor

I am using Cart api to set Requested_for field for different users,but still the requested for is showing my name only

is there any way to set the requested_for by using Cart API

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Gowtham Kodali 

Unfortunately you cannot set any fields of REQ or RITM using Cart API

the only way is to get the REQ sys_id and then set the value

sample script below

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
 //add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('4054428fdb151f0097679ec6db9619c0', 1);

//fill in the variables on the request item form
cart.setVariable(item,"u_requested_for", "e80edd6edbec6bc097275e25ca9619a4");
cart.setVariable(item,"contact_number", "0");
cart.setVariable(item,"assignment_group", "87ec1c342b2e71406c487fb5a8da1524");
cart.setVariable(item,"application_service", "cdedfbcedb0f9744c291c170ba9619a7");

var rc = cart.placeOrder();

var reqRecord = new GlideRecord('sc_request');

reqRecord.get(rc.sys_id);

reqRecord.requested_for = 'user sys_id you want here';

reqRecord.update();

Regards
Ankur

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

View solution in original post

16 REPLIES 16

codycotulla
Tera Guru

Hi,

I have this working. When you create the a cart object by calling the Cart API, the user sys_id you pass in becomes the request's "Opened by" user.

When the cart object is instantiated, it creates cart property, which is a GlideRecord. You can change the properties of this cart GlideRecord, but it doesn't have an impact when the request is created.

Instead, you need to recreate the cart property, and set the requested_for user using the setRequestedFor() command.

var requestedForUser = "user sys_id";
var openedByUser = "user sys_id";

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId, openedByUser); // 
// In the cart object there is a cart property
// We need to recreate this cart property to set the 
// Requested for user

var tempCart = GlideappCart.getCartForRhino(cart.cartName, cart.userID);

//Now we can set the requested_for user on the temp cart
tempCart.setRequestedFor(requestUser);

// Make the tempcart the cart's cart
cart.cart = tempCart.getGlideRecord();

Hope this helps someone.

Thanks,

Cody

Tim Grindlay
Mega Sage

Take a look at the script includes SPCart. I mentioned it's use in this post years ago, but I discovered a better way to set the opened_by and requested_for fields independently, after revisiting a requirement in our instance.

var cat_item = ""; //Sys ID of the catalogue item you're adding
var opened_by = ""; //Sys ID of your opened by user
var requested_for = ""; //Sys ID of the requested for user



var cartId = GlideGuid.generate(null); //Generate the sys ID for the cart
var cart = new SPCart(cartId, opened_by); //Create the cart with the opened_by user
var item = cart.addItem(cat_item); //Add the item to the cart

//Define your variables 
//cart.setVariable(item, <variable name>, <variable value>);

cart.setRequestedFor(requested_for);

var SCrequest = cart.placeOrder();