Service Request Api

naveenreddy0871
Tera Contributor

Hi , 

Requirement: Sc request raised by using Scripted rest api script  and Variables also should be passed.

 

I am trying using to use out of the box — Buy Item(Service Catalog Api) Script in custom Scripted Rest resources but getting errors .

 

kindly help to create new custom api by using out of the box api script .

 

1 ACCEPTED SOLUTION

Hi @naveenreddy0871 ,

 

you can use script like below. You need to query the record sys id and then pass.

var mgr = new GlideRecord('sys_user');
mgr.addQuery('name', manager);
mgr.query();
if (mgr.next()) {
    var managerSysID = mgr.sys_id;
    cart.setVariable(item, "requested_for_manager", managerSysID); //here it is a reference field
}

 

 

Please accept the solution if it helped.

View solution in original post

5 REPLIES 5

palanikumar
Giga Sage

Can you share the script you have created

Thank you,
Palani

aizawaken
Tera Guru

hi @naveenreddy0871 ,

I just tried to create a new Scripted rest service and resource ("Buy Item" from "Service Catalog API") as attached.
And I could create without any errors.

Is this what you want to do? or different?
If different, what kind of error and when do you face on errors?

 

 

aizawaken_2-1731239892234.png

 

 

aizawaken_1-1731239773278.png

 

Runjay Patel
Giga Sage

Hi @naveenreddy0871 ,

 

You can follow below steps to create request.

  1. Create one scripted REST API.
  2. Create one resource with method POST.
  3. RunjayPatel_0-1731240056686.pngRunjayPatel_1-1731240097357.pngRunjayPatel_2-1731240138667.png    

     

  4. Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var obj = request.body.data;

  var id = GlideGuid.generate(null); // Generate a unique GUID for the cart
var cart = new Cart(id);
var RITM = cart.addItem('774906834fbb4200086eeed18110c737'); // sys_id of the catlog item
// Set variables for the catalog item
cart.setVariable(RITM, 'group', obj.group);       // set your variable
cart.setVariable(RITM, 'variable name', obj.variable name);     // Set your variable

// Place the order
var rq = cart.placeOrder();

var responseBody = {};
responseBody.message = "Request created";
response.setBody(responseBody);

})(request, response);

 

For Testing, Go to REST API explorer

RunjayPatel_3-1731240394390.pngRunjayPatel_4-1731240434290.png

 

RunjayPatel_5-1731240465595.pngRunjayPatel_6-1731240492338.png

 

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

 

 

 

 

 

 

@Runjay Patel 

Thanks for your detailed explaination , I am using the below script for raising the request but unfortunately refrenced type of variable values are not passing into RITM. I am getting the variable values from the third party tool so there is no option to send the sys_id.

 

Scripted Rest API :

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 var sys_id = request.pathParams.sys_id;
 var requestBody = request.body.data;
 // Set variables
 var catalogItemSysId = requestBody.item;
 var variables = requestBody.variables;

   var cartId = GlideGuid.generate(null);
   var cart = new Cart(cartId);
   // Add catalog item to cart
   var catalogItem = cart.addItem(sys_id);
   // Set variables for catalog item
 
for (var variable in variables) {
    gs.info("setting variable:" + variable +" with value "+variables[variable]);
     cart.setVariable(catalogItem, variable, variables[variable]);
   }
   // Log cart contents
   gs.info("wed"+cart.getCartItems());
   // Place order (submit RITM)
   var rc = cart.placeOrder();
   var req =rc.sys_id;
   var rit = new GlideRecord('sc_req_item');
   rit.addQuery('request',req);
   rit.query();
   if(rit.next()){
    var ritmNUmber = rit.number;
    var ritmSys_id = rit.sys_id;
   }
 
   //var ritmNumber = ritm.number;
   //gs.info("ncart"+re+"n"+rit);
   // Return response
   var responseBody ={
    table: "sc_req_item",
    RIT : ritmNUmber,
    Sys_id:ritmSys_id,
    assignment_group : rit.stage
    

   }
   responseBody.message = "Request created";
response.setBody(responseBody);
   
 })(request, response);