How can I adjust a catalog item price based on list collector selection?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2019 03:13 PM
I found an article that talks about adjusting the price of a catalog item based on how a user completes the catalog item, but the article only talks about the variable types you can use and not the process to configure them: https://docs.servicenow.com/product/service_catalog_management/concept/c_ServiceCatalogVariablePrici...
With a checkbox you can modify the price in the 'Type Specifications', but with a list collector, I'm not seeing an option to add a price based on the selection.
The goal here is that the list collector will look at our filtered consumable table and an end user will be able to select the items they need from the list and see the total for their order. The fulfiller will have access to add/remove their own items by accessing the consumable table so that someone doesn't need to modify the catalog item every time.
So the question is, how do I add the prices based on the selections in the list collector, or is there a better way to do this?
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2019 09:54 AM
To expand further on my last comment, here is the code that I have so far and what I'm still struggling with.
Client Side Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var listValuename = g_form.getValue('list_collector'); //Getting the list collector values.
var ga = new GlideAjax('u_listcollectorvalues'); //Calling Script Include.
ga.addParam('sysparm_name', 'listcollector'); // Function name of the script include.
ga.addParam('sysparm_list_collector', listValuename); // Passing selected list collector value as parameter.
ga.getXML(listcolleValues);
function listcolleValues(response){
var val = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('multirowtestmultirowtest',val); //Set multi row variable set with the selected list collector values.
var obj = JSON.parse(val); // Get the cost variable
g_form.setValue("order_total", obj[0]["cost"]); //Currently this is getting only one the cost of one item
}
}
Script Include
var u_listcollectorvalues = Class.create();
u_listcollectorvalues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
listcollector:function() {
var val='';
var listValuename = [];
var ast = this.getParameter('sysparm_list_collector'); //Passing the selected list collector asset value.
var query = 'sys_idIN'+ast; // Set a query to run only for assets from selected in the list collector.
if(ast)
{
var gr = new GlideRecord('alm_consumable'); //Glide recording the asset table
gr.addEncodedQuery(query);
gr.query();
while (gr.next()){
//Push selected list collector values into multi row variable set variables.
listValuename.push({
"display_name": gr.getDisplayValue('model.display_name'),
"cost": gr.getDisplayValue('cost'),
"quantity": gr.getDisplayValue('quantity')
//"dis_name": gr.getValue('sys_id'), //Pushing the display name of the selected asset.
//"serial_num": gr.getValue('serial_number'), //Pushing the serial number of the selected asset.
//"model_cat": gr.getDisplayValue('model_category')
//"model": gr.getDisplayValue('model'),
//"state": gr.getDisplayValue('install_status')
});
}
}
val = JSON.stringify(listValuename); //Storing display name & cost values and stringfy it.
return val;
},
});
It seems that I can parse a single cost from the client script and populate the order_total variable with that cost, but what I'm really trying to do is calculate cost * quantity and then add the total of all items to get the total cost. My pseudo code for that would be something like:
var itemcost = [];
var itemquantity = [];
var order_total = 0;
for(var i=0; i< itemcost.length; i++) {
order_total += itemcost[i]*itemquantity[i];
}
How can I accomplish that?
Another concern is that when I remove an item from the selected side of the list collector, and then remove it, all of my quantities get wiped and go back to one. Is there a way to stop that from happening?
I know that I am currently grabbing the quantity from the alm_consumable table, but I'm allowing the user to update the quantity once it's on the form. If I stop bringing the quantity over from the table and allow the user to select the quantity manually, the same issue occurs.