How can I set the price of a dynamic select box variable option from a Service Catalog client script?

Stephen W_
Giga Guru

I have a variable set of 'Select Box' variables attached to a service catalog item.

I use a catalog client script to set each question and populate the choice lists.


I populate the choice lists with

g_form.addOption(<var name>, <option value>, <option label>);

 

How do I now set the price for that option?

Thanks,

 

-Stephen

14 REPLIES 14

It is unclear as to how you have this built and what the expected results should be. The way I am interpreting the functionality is as follows:


The Model is selected by a user.


Once selected, a menu is generated.


Each of the choices of the menu should have a separate price associated with it.



It sounds as if you have a custom look-up table used to generate the menu choices, am I correct? If this look-up table is set up correctly, it can then be leveraged to contain a price for the given selection. That look-up record can then be used to populate a hidden reference field on the form which is then used to set the price for the selection.



If you are still having difficulty, perhaps you can e-mail me directly and I can set up a conference so we can solution accordingly.


Sent you an email.. but I'll reply here as well in-case this may be useful to someone else.



Below is the actual code I'm using.   Hopefully this will clarify.


At the end of the day though.. the problem is that I don't know how to modify ANY price on ANY choices.   Regardless whether the name, value and price were hardcoded or not.




Client Script:


function onLoad() {


   


      var ga = new GlideAjax('ScItemIntfTypes');


      ga.addParam('sysparm_name','getInterfaces');


      ga.addParam('sysparm_sc_item',gel("sysparm_id").value);


      ga.getXML(ajaxResponse);




      function ajaxResponse(serverResponse) {


           


              var arrInterfaces = [];


              var interfaces = serverResponse.responseXML.getElementsByTagName("interface");


              for(var i = 0; i < interfaces.length; i++) {


                      var label     = interfaces[i].getAttribute("label");


                      var guid       = interfaces[i].getAttribute("guid");


                      var count     = interfaces[i].getAttribute("count");


                      var order     = interfaces[i].getAttribute("order");


                      arrInterfaces.push({"label":label, "guid":guid, "count":count, "order":order});


              }




              var arrCompatibles = [];


              var compatibles = serverResponse.responseXML.getElementsByTagName("compatible");


              for(var i = 0; i < compatibles.length; i++) {



                      var model_guid   = compatibles[i].getAttribute("model_guid");


                      var model_name   = compatibles[i].getAttribute("model_name");


                      var model_cost   = compatibles[i].getAttribute("model_cost");


                      var intf_name     = compatibles[i].getAttribute("intf_name");


                      var intf_guid     = compatibles[i].getAttribute("intf_guid");


                      var intf_usage   = compatibles[i].getAttribute("intf_usage");



                      arrCompatibles.push({model_guid:model_guid, model_name:model_name, model_cost:model_cost, intf_name:intf_name, intf_guid:intf_guid, intf_usage:intf_usage});


              }


              updateChoices(arrInterfaces, arrCompatibles);


      }




      function updateChoices(arrInterfaces, arrCompatibles){



              var intCounter = 1;



              for (var i = 0; i < arrInterfaces.length; i++){


                      for (var j = 0; j < arrInterfaces[i].count; j++){


                              var strControlName = "Attrib" + ("0"+intCounter).slice(-2);



                              for (var k = 0; k < countMatches(arrInterfaces[i].guid); k++){


                                      var idxCompat = getMatch(arrInterfaces[i].guid,k+1);


                                      var labelElement = $('label_' + g_form.getControl(strControlName).id)


                                      if (labelElement){


                                              labelElement.select('label').each(function(elmt) { elmt.innerHTML = arrInterfaces[i].label; });


                                      }


                                      g_form.addOption(strControlName, "Default", "Default");   //Should be price 0


                                      g_form.addOption(strControlName, arrCompatibles[idxCompat].model_guid, arrCompatibles[idxCompat].model_name); // Should be price arrCompatibles[idxCompat].model_cost


                                      if(k === 0){intCounter++;}


                              }


                      }


              }



              function countMatches(myguid){


                      var myCount = 0;


                      for (var ic = 0; ic < arrCompatibles.length; ic++){


                              if (arrCompatibles[ic].intf_guid === myguid){


                                      myCount ++;


                              }


                      }


                      return myCount;


              }



              function getMatch( myguid, counter){


                      var myCount = 1;


                      for (var iv = 0; iv < arrCompatibles.length; iv++){


                              if (arrCompatibles[iv].intf_guid === myguid){


                                      if (myCount === counter){


                                              return iv;


                                      }


                                      myCount ++;


                              }


                      }


                      return -1;


              }


      }


}




Script Include:


var ScItemIntfTypes = Class.create();


ScItemIntfTypes.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      getInterfaces: function() {



              var strModel = "";


              var grCompats = new GlideRecord('cmdb_m2m_model_compatibility');


              var grScCatItem = new GlideRecord('sc_cat_item');


              var grModelIntf = new GlideRecord('u_model_interfaces');


              var scItem = this.getParameter('sysparm_sc_item');



              if (grScCatItem.get(scItem)){


                      strModel = grScCatItem.model;


                      grModelIntf.addQuery('u_model',strModel);


                      grModelIntf.query();




                      while (grModelIntf.next()){


                              this._addInterface(grModelIntf.getDisplayValue(), grModelIntf.sys_id, grModelIntf.u_count, grModelIntf.u_order);


                      }


              }


              grCompats.addQuery('model_1',strModel);


              grCompats.query();


              while (grCompats.next()){


                      this._addCompatible(grCompats.model_2.getDisplayValue(), grCompats.model_2.sys_id, grCompats.model_2.cost, grCompats.u_model_interface.getDisplayValue(), grCompats.u_model_interface.sys_id, grCompats.u_count);


              }


      },


   


      _addInterface : function(label, guid, count, order) {


              var intf = this.newItem("interface");


              intf.setAttribute("label", label);


              intf.setAttribute("guid", guid);


              intf.setAttribute("count", count);


              intf.setAttribute("order", order);


      },


   


      _addCompatible : function(model_name, model_guid, model_cost, intf_name, intf_guid, intf_usage) {


              var intf = this.newItem("compatible");


              intf.setAttribute("model_name", model_name);


              intf.setAttribute("model_guid", model_guid);


              intf.setAttribute("model_cost", model_cost);


              intf.setAttribute("intf_name", intf_name);


              intf.setAttribute("intf_guid", intf_guid);


              intf.setAttribute("intf_usage", intf_usage);


      }


})



**Also, welcoming actionable criticism that might improve my code here and in the future.


Adding choices to an item that are not in the choice list for the item is a REALLY bad idea... as it breaks reporting on the item..     the VALUE for the choice is stored in the record... NOT the entire choice... since your choice list doesn't have anything to reflect entries for that value you will not be able to look up information for that value if it is selected...


Stephen W_
Giga Guru

If this isn't possible..   (odd that ServiceNow would give us a simple method for adding choices, but only allow us to affect two of the six attributes.)



I suppose I might be able to add an OnChange() function to alter the cart price.. though, I'm not sure just yet how to update the cart price.


Have you determined how to alter the price via OnChange script?