Trying to set catalog item price via client script

Ken Berger
Giga Guru

Hi folks,

 

I have seen this question asked on the community, but nothing seems to fit my particular situation.  I have inherited a catalog item request form with a list collector where users can select multiple Adobe products to request.  The reference for the list collector is the sys_user_group table. I cannot see any pricing info on it or any related fields.  I have the following client script which contains an associative array for the pricing, and I am trying to loop through the user selections; look up the prices in the associative array; sum up the prices for all the selected items and update the catalog item price with the total.

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Calculates the total price of the selected Adobe products
	
	var priceArray = [{"AdobeEnt.AdobeCCE.AcrobatPro": 127.44, "AdobeEnt.AdobeCCE.AfterEffects": 381.60, "AdobeEnt.AdobeCCE.Audition": 381.60, "AdobeEnt.AdobeCCE.Dreamweaver": 381.60, "AdobeEnt.AdobeCCE.FullSuite": 835.44, "AdobeEnt.AdobeCCE.Photoshop": 381.60, "AdobeEnt.AdobeCCE.Premiere": 381.60, "AdobeEnt.AdobeCCE.Stock": 381.60}];
	var adobeApps = g_form.getValue('which_product_are_you_requesting');
	var apps = adobeApps.split(',');
 
	var total = 0;
	for (var i=0; i<apps.length; i++) {
		
		if (priceArray.get(apps[i])) {
			total += priceArray.get(apps[i])[1];
		}		
	}	
	return total;
	g_form.setValue('price', total);
}

 

 

 

 

I am not sure if it is my array, loop or something else, but the price is not being set when I test out the form and always coming back as $0.00.  As always, any help is greatly appreciated.

 

Thanks,

Ken 

2 ACCEPTED SOLUTIONS

@Ken Berger  in client script on change of total_price field is selected change it to onchange of "which_product_are_you_requesting" then it should work.

swathisarang98_0-1713990599830.png

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

View solution in original post

Hi @Ken Berger  yeah sure, you can update the existing display business rule as below,

 

 

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('request', current.sys_id);
    gr.query();
    if (gr.next()) {

        var totalPrice = gr.variables.price;
		current.price = totalPrice;
		gr.price = totalPrice;
		gr.update();
    }

})(current, previous);

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

View solution in original post

11 REPLIES 11

Ken Berger
Giga Guru

I figured out that the selections being made are saved in the system as sysID's and not group names.  I modified my priceArray and made a couple other changes and now my code is totaling the price properly:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Calculates the total price of the selected Adobe products
	
	var priceArray = {"2b9bc7331b839c90248377761a4bcbf2":127.44, "3dab8f331b839c90248377761a4bcb83":381.60, "c6abcf331b839c90248377761a4bcb62":381.60, "0eabcf331b839c90248377761a4bcb95":381.60, "64cc87771b839c90248377761a4bcbf8":835.44, "7fcc43b71b839c90248377761a4bcb11":381.60, "5ddc07b71b839c90248377761a4bcb85":381.60, "8cd0e243db16a850994ca015ca96199f":381.60};
	var adobeApps = g_form.getValue('which_product_are_you_requesting').toString();
	var apps = adobeApps.split(',');
 
for (var i=0; i<apps.length; i++) {
	
	if (priceArray[apps[i]]) {
		total += priceArray[apps[i]];
	}		
}
	g_form.setValue('price', total);
}

 

However, I am still having trouble updating the Price field on the request with the total.

 

g_form.setValue('price', total);

 

Does not appear to be working.  Can someone please help me figure out how to write the total price that is calculated in the client script to the Price field on the request form?

 

Thanks,

Ken

Hi @Ken Berger ,

 

On which table you have written your client script ? is it on request table ?

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Hi swathisarang98,

 

Thanks for your reply.  I have it written on the catalog item (see below):

KenBerger_0-1713979797899.png

BTW, I changed it to onSubmit but this did not help 😞

 

Regards,

Ken

@Ken Berger you cannot directly set the request table value from catalog client script so you have to write a display business rule to set the price value,

 

So what you can do is in catalog client calculate the totalprice and create a new catalog variable and store that total price in it and hide it using ui policy,

 

then on request table create a display business rule and get the variable totalprice value from Requested item and paste it in Request table price field 

 

you can copy the below Display business rule i have tried in my pdi it is working,

swathisarang98_0-1713983863116.png

 

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('request', current.sys_id);
    gr.query();
    if (gr.next()) {

        var totalPrice = gr.variables.price; // price is the catlog item variable which stores the total price.
		current.price = totalPrice;
    }

})(current, previous);

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang