Create a request from script

brumiou
Mega Guru

Hi,

we have the Service Catalog, where users can go to create a request for something.
A request is then created with a workflow linked to this request depending on the type of item they order.

We have a tool for booking a room, and we want that this tool creates a request in snow.

I can of course create a request record, but the workflow is not associated with this request.

Is there a way to create a request, with variables and a workflow, from script, like if it was created by a user from the service catalog?

Thanks a lot

rgds

Xavier

8 REPLIES 8

Joshua Chapman
Tera Contributor

I know this thread is old (like really old) but you can do what is being asked here using the Service Catalog script API

https://docs.servicenow.com/bundle/orlando-application-development/page/script/server-scripting/refe...

Use the Cart class.


Use cart.addItem() and pass the sys id for the catalog item you want to generate.
Then add values to your variables using cart.setVariable() just like you would set the values using the UI. Keep in mind any UI policies or client scripts that might manipulate fields based on supplied values.

Then cart.placeOrder() will generate the request and return the id.

This will kick off any workflow associated with the catalog item and the ticket should be well formed, displaying the variables as it would if you completed the catalog item using the UI.

This is why it's nice to comment on old threads... I was searching for the very same problem today and found the answer thanks to your reply. So thank you!

ZM
Tera Contributor

The Cart script include is set to private and can be used in the global scope only. I have to create a Service Request from a scoped app and can't use it.

Setting the Cart script include to be public isn't an option, as we'd be loosing support.

Is there another, similar way to create a Service Request?

You would use the CartJS API which is scope friendly.

 

I haven't had an occasion to use it yet so I can't speak to how well it works. Looks a fair bit more complicated than the old Cart API.