Is there a way to create an Order Guide with just two steps - first page, then checkout, no 'choose Options?

rlegters
Mega Expert

I'm building a New Hire Order Guide. We're collecting all the information we need to generate the requests on the first screen. Right now, users have to click 'Choose Options', and click on each tab (there are 9 of them), even though they don't need to choose any options, then click Checkout. I'd like them to be able to just answer the questions on the first screen, then click 'Checkout' and get to the order confirmation screen. I'm using Cascading variables from my Order Guide, but 5 of the catalog items have the same variables, so I've created catalog scripts that move the information from the cascaded-to variables that match the Order Guide variables. These don't run unless the tab is clicked on. I've also set 'Ignore Mandatory Evaluation' to True for all of the items, but that doesn't help if the variables aren't getting set correctly without clicking on the tabs.

My users would really like to not see the tabs at all, just click 'Checkout' from the first screen, and go right to the checkout screen.

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

I would agree with Abhiram and you can use a catalog item and then you can automatically add other items into the user's cart via client script on the submit of the cart via an AJAX call.   Below is some sample code:



On Submit Client Script:


var ajax = new GlideAjax('AJAXAddCatItems');


ajax.addParam('sysparm_name', 'addCatItems');


ajax.addParam('sysparm_variable1', g_form.getValue('variable1'));


ajax.addParam('sysparm_variable2', g_form.getValue('variable2'));


ajax.getXMLWait();



Then create an AJAX script include to add the catalog items.   You will notice that I did not use the script in the Wiki article that Abhiram included and that is because calling new Cart() clears the user's existing cart which is not what I wanted.   So I took code from the out of the box Script Included named Cart, which is the code that it was calling.   Below is example code for your new AJAX Script Include based on the above example


var AJAXAddCatItems = Class.create();


AJAXValidateInstance.prototype = Object.extendsObject(AbstractAjaxProcessor, {


     


      addCatItems: function() {


              var scCart = new GlideRecord("sc_cart");


              scCart.addQuery("user", gs.getUserID());


              scCart.query();


              if (scCart.next()) {


                      var scCartID = scCart.sys_id;


                      var catItemSysID = 'SYS_ID_OF_CAT_ITEM';


                      var scCartItem = new GlideRecord("sc_cart_item");


                      scCartItem.addQuery("cart", scCartID);


                      scCartItem.addQuery("cat_item", catItemSysID);


                      scCartItem.query();


                      if (!scCartItem.next()) {


                              var variable1 = this.getParameter('sysparm_variable1');


                              var variable2 = this.getParameter('sysparm_variable2');


                              var itemID = catItemSysID;


                              scCartItem.initialize();


                              scCartItem.cart = scCartID;


                              scCartItem.cat_item = itemID;


                              scCartItem.quantity = 1;


                              var rc = scCartItem.insert();


                              var ci = GlideappCatalogItem.get(itemID);


                              var gr = ci.getStandaloneVariables();


                              var seq = ci.getKeySequence();


                              while (gr.next()) {


                                      var question = new GlideappQuestion(gr.sys_id);


                                      var variable = new GlideRecord('sc_item_option');


                                      variable.initialize();


                                      variable.item_option_new = gr.sys_id;


                                      var variableValue = "";


                                      var variableName = variable.item_option_new.name.toString();


                                      switch (variableName) {


                                              case 'variable1':


                                              variableValue = variable1;


                                              break;


                                              case 'variable2':


                                              variableValue = variable2;


                                              break;


                                              default:


                                              variableValue = question.getValue();


                                              break;


                                      }


                                      variable.value.setValue(variableValue);


                                      variable.cart_item = rc;


                                      for (var i = 0; i < seq.size(); i++) {


                                              if (seq.get(i) + "" == gr.sys_id + "")


                                                      variable.order = i + "";


                                      }


                                      variable.insert();


                              }


                      }


              }


              return 'proceed';


      },


     


      type: 'AJAXAddCatItems'


});



You can repeat lines 11-53 to add other items into the cart or better yet create a function that is called so you don't repeat the code.   But hopefully this gets you started.


View solution in original post

27 REPLIES 27

Hi,


I have a few question about the code you provided, I will say that coding is not my strong side, so maybe the things that i'm asking are obvious.


  1. var catItemSysID = 'SYS_ID_OF_CAT_ITEM'; does this refer to the cat_item that will generate the other requested items?
  2. Where in the code do I add the sys_id of the items I want to add? how does it know which cat_item I want to add?

Michal, Question 1 actually answers question 2.   SYS_ID_OF_CAT_ITEM needs to be the SysID of the catalog item that you want to add to the cart.   So pull up the catalog item as if you are configuring it and then click Additional Actions, Copy sys_id and that is what you need.   You may need to repeat this code for multiple items.


Hello,

 

this has been very helpful - thank you. I'm using it to bundle items. when you add the 'bundle' item to your cart it adds the 4 individual items. I have one issue though. on submit the 4 items in my bundle are added to the cart, however the bundle item itself is also added to the cart. I've tried to delete it from within the client scrip AJAX call, however this only works half the time. The other time, it can't find the item to delete. I'm assuming this is an order issue, and in some cases the client script is executing before the item is added. Any ideas on how I can resolve this?

 

thanks in advance for any help.

rlegters
Mega Expert

Michael - One more question: Is there any way that I can control the order in which the individual Requested Items are created? I've coded them in them in my script include in the order I'd like them to be created, but they're being assigned RITM numbers in alphabetical order by Description. Not a big deal, but it makes the Cart at checkout and the approval request message, which lists all the items included, look kind of random.


Ron, unfortunately I don't believe there is.   I looked at the sc_cart table hoping to see an "order" field but there isn't.   I also looked at the cart script include (this is the code you are calling in your script) to see if there are any options there and unfortunately came up short.   The cart script include is calling server side Java code that only ServiceNow developers can touch so based on your testing it appears that code is ordering the items by name during the processing of the order.



You could edit the approval request email to your liking and get the items in the order you want, but the cart you cannot.