how can i calculate two currency fields?

Sachin65
Tera Expert

 i need to calculate two currency fields. 

field name: u_currency1 (type currency)   

field two name:  u_currency2 (type currency) 

  field 3 name: u_store : (type choice ) 

my req is

u_currency1 /  u_currency2 *100 basced on this value set the choice field 

5 REPLIES 5

ScienceSoft
Tera Guru

Hi,  Sachin

You may create 2 Client Scripts onChange of "u_currency1" and  "u_currency2" fields to react on changes of these currency fields. 

And to set field3 value - use in each Client Script g_form.setValue('u_store','field3NewValue') to set your result. 

For Example:

assuming you new value for 'u_store' should be Label:"Macy's King of Prussia" value:'macy_kp': 

then you onChange Script whould look like:

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

var cur1 = parseInt(g_form.getValue('u_currency_1'));
var cur2 = parseInt(g_form.getValue('u_currency_2'));

var result = cur1/cur2*100;

if (result > 50) {
    g_form.setValue('u_store','macy_kp');
}
}
}

 however, you may need to use GlideAjax to do proper calculations

Client Script:

find_real_file.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	   var currency1 = g_form.getValue('u_currency1');
	    var currency2 = g_form.getValue('u_currency2');
	   var ga = new GlideAjax('SetCurrency');
	   ga.addParam('sysparm_name', SetCurrencyFunc);
	   ga.addParam('sysparm_currency1', newValue);
	   ga.addParam('sysparm_currency2', currency2);
	   ga.getXML(set_u_store);
   

}
function set_u_store(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('u_store', answer);
	return answer;
}

Script Include(Client-callable):find_real_file.png

var SetCurrency = Class.create();
SetCurrency.prototype = Object.extendsObject(AbstractAjaxProcessor,{
	SetCurrencyFunc:function(){
		var result = '';
		/*
		currency calculation according following documentation and set result variable
		https://docs.servicenow.com/bundle/jakarta-platform-administration/page/administer/currency/concept/currency-values-scripts.html
		*/
		
		return result;
	}
});

 

Omkar Mone
Mega Sage

Hi 

Please write a client script to accomplish this.

Please find below the Client Script.

function onLoad() 
{
	var price3=0;
   var price1=parseInt(g_form.getValue('u_currency_1'));
   var price2=parseInt(g_form.getValue('u_currency_2'));
   price3 = price1 / price2 * 100;


   g_form.addOption('your_choice_field_name',price3,'label')
}

 

Mark Correct if it helps.

Warm Regards,

Omkar Mone

find_real_file.png

www.dxsherpa.com

price3 is Choice List - not currency type