Scripted creation of request

Marcelo Pedrey1
Tera Contributor

Hello All,

 

I have a requirement to create a script that will automate the creation of a catalog request.

 

  1. When a new user is created on the sys_user table with the following conditions:
    • company = ACME
    • source = new_hire_table
    • new_hire_active = Y
  2. Kick-off the creation of a catalog item request.

I created a business rule to run after and insert

MarceloPedrey1_0-1695224031873.png

with the following advanced script

MarceloPedrey1_1-1695224089923.png

 

Just for clarity in the event that you cant read the PIC

condition:  

current.u_new_hire_active == 'Y' && current.source == 'new_hire_table' && current.company == 'ACME'

 

Script:

var catalogItemId = '5309d08c8708b11004380e15dabb3560'; // Catalog Item OKTA
var creator = new CatalogItemCreator();
creator.createCatalogItemRequest(current.sys_id, catalogItemId);
 
 
Any and all help is appreciated and I would like to add that my scripting skill set is a bit new.
 
Thank you,
C

 

 

 

 

 

4 REPLIES 4

Vishal Birajdar
Giga Sage

Hello @Marcelo Pedrey1 

 

You can use Cart API

 

Below link might help you....!!

 

YouTube link - https://youtu.be/EciIRYzSKBs?feature=shared

 

https://www.servicenow.com/community/now-platform-forum/how-to-set-requested-for-in-ritm-using-cart-...

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates
Hello, today we will learn about cart API __________________________________________________________________________________________ If you have any query please connect with us on Facebook https://www.facebook.com/ServiceNowKida/ if you find my video useful, please like and share my video and ...

Hello Vishal, 

 

This did not seem to work, I failed to mention that it is an actual order guide that it is supposed to kick off

 

Thank you,

Best

Hello Marcelo,

 

You want to raise catalog Item request (Order Guide) from business rule? Right..??

The link provided above should work.

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hello Vishal,

 

Yes, i would like to open an Order Guide from the business rule.  I have tried that and it opens the order guide as a RITM not as a request and does not trigger any of the other catalog items within the order guide.  I have updated the code and still am getting nowhere. I must be doing something wrong, hopefully a fresh set of eyes can let me know.

(function runBusinessRule(current) {
    // Check if the conditions for executing the script are met
    if (
        current.company == "ACME" &&
        current.source == "new_hire_table" &&
        current.u_new_hire_active == "Y"
    ) {
        // Create an object with JSON data
        var json = {
            "requested_by": "a982fa3047e53d50ec63fc29736d43bc"
        };

        // Continue with your existing script logic
        // ...

        // Now, open the "New Hire Onboarding" order guide
        var orderGuideName = "New Hire Onboarding";
        var nowGR = new GlideRecord("sc_cat_item_guide");

        // Find the order guide by name
        if (nowGR.get("name", orderGuideName)) {
            // Initialize a ScriptableOrderGuide object
            var sog = new SNC.ScriptableOrderGuide(nowGR.getValue("sys_id"));
            
            // Set the variables in the order guide
            sog.variables.orderGuide_requested_by = "a982fa3047e53d50ec63fc29736d43bc";
            sog.variables.orderGuide_employment_type = "employee";
            sog.variables.orderGuide_employment_subtype = "associate";
            sog.variables.orderGuide_start_date = current.u_glide_date_1;
            sog.variables.orderGuide_new_employee_name = current.name;
            sog.variables.orderGuide_company = "1fae00ed8790a11004380e15dabb3582";
            sog.variables.orderGuide_department_name = current.department;
            sog.variables.orderGuide_manager = current.manager;
            sog.variables.orderGuide_manager_email = current.manager.email;
            sog.variables.orderGuide_is_new_hire_manager = current.u_managerstatus;
            sog.variables.orderGuide_job_title = current.title;
            sog.variables.orderGuide_add_user_to_okta_ad_group = "true";

            // Process the order guide
            var result = sog.process(new JSON().encode(json));

            if (!result) {
                gs.log("Processing the scriptable order guide failed with message: " + sog.getMessage());
            } else {
                // Get the request created by the order guide
                var request = sog.getRequest();
                gs.log("Request created - " + request.sys_id);
            }
        } else {
            gs.error("Order guide 'New Hire Onboarding' not found.");
        }
    } else {
        gs.log("Script conditions not met. Skipping execution.");
    }
})(current);