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

Thank you for the assistance.  I am sorry to ask but I am having trouble with the following step:

 

"...in catalog client calculate the totalprice and create a new catalog variable and store that total price in it..."

 

I created a new "Single Line Text" variable in my variable set, called total_price (no need to hide it as the user can see it for reference).  In the variable set, I also added the Catalog Client Script to calculate the total price and store it in the total_price variable:

 

KenBerger_0-1713989211547.png

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(',');
 
	var total = 0;
	for (var i=0; i<apps.length; i++) {
		
		if (priceArray[apps[i]]) {
			total += priceArray[apps[i]];
		}		
	}
//	return total;
	g_form.setValue('total_price', total);
   
}

 

When I select the Adobe products in the form, I am not seeing the value of the total_price variable change.  It remains empty.  Can you please point out to me what I am doing wrong here?

 

Thanks,

Ken

 

 

@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

 

Ken Berger
Giga Guru

Thaaaank Youuuu!!!!

🙂

 

Ken Berger
Giga Guru

May I indulge you a bit further?  Is there a way to make the price populate on the RITM also?  The approval emails will take the price from the RITM and this is still showing $0.00.

 

KenBerger_0-1713993486966.png

 

 

Thank you,

Ken

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