add items to order guide dynamically

speterson
Mega Contributor

I am looking for a good way to add catalog items to an order guide using custom logic -   without having to specify all of the possible items in the rule-base.

For example, lets say there are 15 mobile phone models/plans that are in the catalog and could be added to the order guide.   Additionally, more models come and go regularly.

The logic exists elsewhere in the system using script include to indicate who is allowed to request which model.   These rules are too complicated to easily be put directly into the order guide rules (e.g. not as simple as 'Department == IT' or 'Title == Director').  

I would like to see if/where I can intercept the logic after 'Describe Needs' and add the correct Catalog items into the Order Guide.

 

psuedo code idea:

 

//on 'Describe Needs' Submit action

 

var orderGuide = current;

var itemFinder = new CatalogItemChooser();

var availableItems = itemFinder.FetchAvailableCatalogItems(orderGuide.variables.requested_for);

//this will consult custom provision logic based on the requested_for user and bring back a list of items to add to the order guide.

orderGuide.items = availableItems;

 

Am I missing something simple here?

 

I don't think the rule base is complex enough to handle this easily.  

I know I could use the rules, add every item individually with a rule - and then make a call to the custom logic to return a true/false answer to determine if it should be included, but that seems like more maintenance than should be required.

 

Thanks for any help or ideas.

Scott.

8 REPLIES 8

tltoulson
Kilo Sage

Modifying the Order Guide behavior is darn near impossible.   The logic for it appears to be located in the UI Page com.glideapp.servicecatalog_cat_item_guide_view.   As you read the UI Page you will notice that much of the logic is hidden in inline templates (read: stored server side and not accessible) and Glide API function calls that are not Script Includes.   That said, near impossible means we might be able to use existing Order Guide behavior to achieve the desired outcome.



Here is a possible solution, its a bit of work up front but dramatically reduces maintenance:



1.   Create a hidden string variable.   You will use this to store a comma separated list of models to include which will be consumed by the Order Guide Rules


2.   Encompass your business logic in a Script Include that is accessible via GlideAjax.   The logic of this script should generate a comma separated string of either the names, sys_ids, or some id for the models that can easily be identified from a script.   If a script can figure out the id, we can automate the rule creation.


3.   Create a Catalog Client Script that uses the AJAX Script Include from #2 to populate the variable created in #1.


4.   Rule creation is the maintenance headache but this can be scripted.   In this case the rules are easy enough for a script to automatically create.   The rule for each model is simply: Include when the variable created in #1 contains the model id.   A well designed Script Include in #2 will have a list of models to include, add a refreshOrderGuideRules function that iterates over the list of models and creates an Order Guide rule for each.   This script can be called from a Business Rule or a UI Action.



Pseudo Script Include:



var DynamicOrderGuide = Class.create();


DynamicOrderGuide.prototype = {


      initialize: function() {


      },



      models: {


                  'modelid': function() { //model logic here },


                  'modelid2': function() { //model2 logic here }


      },    



      getModelsToInclude: function() {


        var modelList =[]';


          for (var model in this.models) {


                  if (this.models[model]()) {


                            modelList.push(model);


                  }


          }


        return modelList.join(','); // May need to change this for AJAX return


      },



      refreshOrderGuideRules: function() {


                  for (var model in this.models) {


                            var gr = new GlideRecord('sc_cat_item_guide_items');


                            // logic to check if a rule exists here


                            // If a rule doesn't exist for the model create it, may need to add a check also if a rule exists for models that have been removed


                            gr.condition = 'varnamehereLIKE' + model;


                            gr.insert();


                  }


      },


      type: 'DynamicOrderGuide'


}



In this way, you add your models and the logic in one place, the script include.   Then either a Business Rule or UI Action could apply those changes using the refreshOrderGuideRules function.   Maintenance averted.



Another option is a Wizard but you may run into similar maintenance issues there.


Hi Travis,


We have a similar scenario..


We are creating an order guide for New Employee.


Order guide contains 2 items. (Desktop,Mobile).


While clicking on the Check out button, an automatic request should raise for the 3 rd item (Email Account) with out giving any inputs. (3rd item should not be added in the order guide.)



Please help


Hi Dedeepya,



In your situation, I would recommend a Wizard instead of an Order Guide.   You will want direct control over the Cart API so that you can get all 3 items on the same request.  


tltoulson
Kilo Sage

And I should also add, there may be a simpler solution, but we would need to know more about the specific rules you are dealing with.   There may be a way to simplify the rules themselves or provide a simpler way to implement the rules.