Script to add item to cart (Service Catalog) not working in Workspace (Next Experience UI)

mfhaciahmetoglu
Mega Sage

Hello everyone,

 

I'm facing an issue with a script that works perfectly in the Core UI but fails when executed within the Configurable Workspace / Next Experience UI.

 

Goal: The script's purpose is to programmatically add a specific Service Catalog item to the cart and populate its variables when a UI Action (button click) is performed.

 

Context: I have a UI Action on a form that, when clicked, executes a script. This script utilizes GlideRecord and CartJS (or similar logic) to add a catalog item.

 

Current Behavior (Core UI): In the Core UI, the script successfully adds the item to the cart, populates the variables, and proceeds as expected.

 

Problem (Workspace / Next Experience UI): When the same UI Action is triggered in the Configurable Workspace (Next Experience UI), the script does not function as intended.

 

  • Behavior: The button is clickable, but nothing happens. The item is not added to the cart, and no variables are populated.

Script Snippet:

function onClick(g_form) {

    if (confirm("Do you want to order a temporary PC? (OK = Yes, Cancel = No)")) {
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        //add your requested item to the cart by sys_id of the catalog item
        var item = cart.addItem('1acb570593e802d40c7d72f48aba10cb', 1);

        //fill in the variables on the request item form
        cart.setVariable(item, "i_need_a_pc_for_a1", "new_hire");
        cart.setVariable(item, "hiring_manager", current.parent.variables.hiring_manager);
        cart.setVariable(item, "delivery_location", current.parent.variables.delivery_location);
        cart.setVariable(item, "new_employee_name_s", current.parent.variables.new_employee_name_s);
        cart.setVariable(item, "cost_center", current.parent.variables.cost_center);
        cart.setVariable(item, "cost_center_responsible", current.parent.variables.cost_center_responsible);
        cart.setVariable(item, "request_motivation", current.parent.variables.request_motivation);
        cart.setVariable(item, "i_need_a2", "temp_laptop");
        cart.setVariable(item, "keyboard_type", current.parent.variables.keyboard_type);
        cart.setVariable(item, "barco_id_current_pc", current.parent.variables.barco_id_current_pc);
        cart.setVariable(item, "operating_system", current.parent.variables.operating_system);
        cart.setVariable(item, "start_date", current.parent.variables.start_date);
        var rc = cart.placeOrder();
    } 

}
 

My Question: Has anyone encountered a similar issue with CartJS or similar catalog scripting in the ServiceNow Workspace / Next Experience UI? Are there specific considerations, new APIs, or different approaches required for this functionality in the Workspace?

 

Any guidance or best practices for handling Service Catalog item additions via script in the Workspace would be greatly appreciated!

 

Thanks in advance!

Best,

Firat

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@mfhaciahmetoglu 

I believe the reason it's working in Native UI is because you might have added the code to server side using gsftSubmit()

But these lines are server side so won't work in workspace client script

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);

So move your entire code to GlideAjax and call that from workspace client script

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@mfhaciahmetoglu 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@mfhaciahmetoglu 

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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