Create a request from script - ServiceNow Community

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

jim pisello
Giga Expert

Hi Xavier,

If your room booking tool supports web services you can do something like this:

1. Create a service catalog item for booking a room and note its sys_id (you'll need it later)
2. Create the appropriate workflow and associate it to the catalog item

Then, using SOAP or another web service, do the following:

3. Insert a new Request [sc_request] record and get its sys_id
4. Insert a new Requested item [sc_req_item] record, passing the sys_id of the Request you created in step 3 into the "request" field on this new record
5. Update the Requested item record from Step 4 by passing the sys_id for the catalog item you created in Step 1 into the Requested item's "cat_item" field.

Step 5 is the important one. If you insert the Requested item record with the Catalog Item already specified, the workflow will not start. If, however, you leave the cat_item field blank when you insert the Request Item record, then update that record by specifying a catalog item, the update will kick off the item's workflow.

We had a need to create a service request from JIRA, so we created a Java app that performs steps 3-5 above using SOAP.

If you don't want to use SOAP/web services and your booking tool can send emails, you could write an inbound action script to submit a request using the Shopping Cart API.
http://wiki.servicenow.com/index.php?title=Inbound_Email_Actions
https://wiki.servicenow.com/index.php?title=Service_Catalog_Script_API

Whichever path you choose, you will most likely need to create a service catalog item and workflow.


Hi Jim,

thanks for your help!
I also had to set the stage of the requested item to "request_approved", because I saw in the business rule "Start Workflow" that it was in the conditions.

Now it works great.

Thanks a lot!

rgds

Xavier


Simm
Tera Contributor

Thank you so much,

this makes code script working setting the stage=“request_approved".

NateC
Kilo Explorer

Would there be a particular reason I wouldn't see any of the form variables after--when I go and open the RITM after I have taken these steps? Is there something else in code I need to do to associate the form vars with the RITM?

I do see that the correct cat item is associated with the RITM only because the correct cat item name is in the text box on the RITM.

But other than that the workflow nor the variables seem to be there.

I did note that I have to add the cat item to the RITM in an update() after the RITM is created as you say in order to "fire off" the workflow but that didn't seem to help.

var catItmID = ''
var rec = new GlideRecord('sc_cat_item');
rec.addQuery('name', 'Employee Onboarding');
rec.query();
if(rec.next()){
catItmID = rec.getValue('sys_id');
}
var grRequest = new GlideRecord ("sc_request");
grRequest.initialize();
grRequest.u_requested_by = 'myuserID';
grRequest.requested_for = 'myuserID';
grRequest.short_description = 'test';
grRequest.description = 'test';
var requestSysId = grRequest.insert();
var grRequestItem = new GlideRecord ("sc_req_item");
grRequestItem.requested_for='myuserID';
grRequestItem.short_description='test';
grRequestItem.description='test';
grRequestItem.parent = requestSysId;
grRequestItem.request = requestSysId;
var reqItmSysID = grRequestItem.insert();
gs.print(reqItmSysID);
var RITMrec = new GlideRecord('sc_req_item');
 RITMrec.addQuery('sys_id', reqItmSysID);
 RITMrec.query();
 if(RITMrec.next()){
 RITMrec.cat_item=catItmID;
 RITMrec.update(); 
 }