How can I create a Request & Request item from business rule?

Dk nov
Tera Contributor

Hello,

 

I do I create a Request & Request item from the business rule?

Do I have to fill out variable as well or can you create a request & request item without variable?

 

Can I have correct sample scrip please? 

 

Thanks,

Derek

1 REPLY 1

PrashantLearnIT
Giga Sage

Hi @Dk nov 

 

Yes, you can create a Request (sc_request) and a Request Item (sc_req_item) from a Business Rule in ServiceNow. If you don't need to fill out variables, you can create the Request and Request Item without providing them. However, if your catalog items require variables, you might want to fill them out as part of the process.

Steps to Create a Request and Request Item from a Business Rule

  1. Request (sc_request): This represents the overall request.
  2. Request Item (sc_req_item): These are the individual items tied to the request (each catalog item).

Sample Script to Create Request and Request Item

Here’s an example of how you can create a Request and Request Item from a Business Rule:

// Business Rule Script (e.g., after insert or based on conditions)
(function executeRule(current, previous /*null when async*/) {

// Create a new Request (sc_request)
var request = new GlideRecord('sc_request');
request.initialize();
request.requested_for = current.requested_for; // Set the requested_for user
request.requested_by = current.requested_by || gs.getUserID(); // Set the requested_by user
request.short_description = 'Automated request for ' + current.short_description;
request.u_custom_field = current.u_custom_field; // If you have any custom fields
var requestID = request.insert(); // Insert the request and capture the sys_id

// Create a new Request Item (sc_req_item)
var requestItem = new GlideRecord('sc_req_item');
requestItem.initialize();
requestItem.request = requestID; // Link to the request
requestItem.cat_item = '<catalog_item_sys_id>'; // Set the catalog item sys_id
requestItem.quantity = 1; // Specify the quantity
requestItem.short_description = 'Item description';
requestItem.u_custom_field = current.u_custom_field; // Any custom fields if needed
var requestItemID = requestItem.insert(); // Insert the request item

// Optionally, you can fill in variables for the item (if needed)
var variables = requestItem.variables;
variables.your_variable_name = current.your_variable_value;
variables.setDisplayValue('your_variable_name', 'value_to_display');

})(current, previous);

 

Key Points:

  • Request (sc_request) creation: You create a new record in the sc_request table, setting the requested_for and requested_by fields and other relevant fields.
  • Request Item (sc_req_item) creation: This creates an item linked to the request. You must provide the catalog item's sys_id (e.g., <catalog_item_sys_id>), which you can get from the Catalog Item table (sc_cat_item).
  • Variables: If the catalog item uses variables and you need to set them, you can access the variables object of the request item (requestItem.variables). However, you don’t have to set variables if they are not required.

Handling Variables:

If the catalog item has variables and you want to set them, you can populate them as shown in the script using the variables property. If the variables are mandatory for the catalog item, they need to be filled.

If variables are optional, you can create the request and request item without setting them. However, ensure that if any variable is mandatory for that catalog item, it’s populated, or the item might not be processed correctly.

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************