Auto add request to cart option

v-paulp
Tera Contributor

Hi All,

We have a catalog item with three variables—Environment, Project, and Role—where the Project choices depend on the selected Environment, and the Role choices depend on the selected Project.
Now  requirement is If the Environment is set to "env1" and the user does not choose either the pro11–role111 or pro12–role121 combinations, the system must automatically add two requests to the cart using these valid combinations.
 How to achieve this requirement?

Thanks
Pradip  
4 REPLIES 4

VaishnaviK43271
Tera Contributor

Hi @v-paulp !!

This requirement must be handled server-side using a Flow or Business Rule with the Cart API.

 

Solution Approach:-

1) Client-side (optional validation)

Use a Catalog Client Script (onSubmit) only to read values (do not add items here).

 

2)Server-side (Main logic – Recommended)

Create an After Insert Business Rule on sc_req_item
(or Flow triggered on RITM creation)

Condition:

  • Environment = env1

  • Selected combination is NOT:

    • pro11 – role111

    • pro12 – role121

(function executeRule(current, previous) {

    // Prevent recursion
    if (current.u_auto_created == true) {
        return;
    }

    var env = current.variables.environment;
    var project = current.variables.project;
    var role = current.variables.role;

    if (env == 'env1' &&
        !((project == 'pro11' && role == 'role111') ||
          (project == 'pro12' && role == 'role121'))) {

        var cart = new Cart();

        // First required combination
        cart.addItem('CATALOG_ITEM_SYS_ID', {
            environment: 'env1',
            project: 'pro11',
            role: 'role111',
            u_auto_created: true
        });

        // Second required combination
        cart.addItem('CATALOG_ITEM_SYS_ID', {
            environment: 'env1',
            project: 'pro12',
            role: 'role121',
            u_auto_created: true
        });

        cart.placeOrder();
    }

})(current, previous);

 

Why this works

  • Client scripts cannot add multiple requests

  • Cart API ensures clean request creation

  • Works for Portal, UI, and integrations

  • Recursion is avoided using a flag field

 

Mark this as Helpful if it clarifies the issue.
Accept the solution if this answers your question.

Regards,
Vaishnavi
Associate Technical Consultant

Ankur Bawiskar
Tera Patron

@v-paulp 

what do you mean by add 2 Requests to cart?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Two requests must be automatically added to the cart, populated with the following variable values:

  1. Environment: env1, Project: pro11, Role: role111
  2. Environment: env1, Project: pro12, Role: role121

@v-paulp 

I will suggest let your flow or workflow on your current catalog item handle the new REQ/RITM creation based on variable value

If you are using flow then you can use "Submit Catalog Item Request" flow action

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader