FX Currency returning incorrect value

ceraulo
Mega Guru

Hello!

There is a Cost currency field in the alm_hardware table.
I created another currency field called Cost (USD). 
The objective is to display the USD equivalent to this custom field.

The USD equivalent is taken from the Reference amount field in the fx_currency_instance table.
I have created a BR that is triggered when the Cost field changes.

var gr = new GlideRecord('fx_currency_instance');
    gr.addQuery('id', current.sys_id);
    gr.addEncodedQuery('field=cost');
    gr.query();

    if (gr.next()) {
        var usd = gr.getValue('reference_amount');
    }

    gs.addInfoMessage(usd);
    current.u_cost_usd = usd;

The issue is the 'usd' value is not saved to the u_cost_usd field.
Any idea why the field is not updated?

Please help!

Thank you.

 

1 ACCEPTED SOLUTION

Hi,

Please create after BR Like below:

find_real_file.png

and use below script:

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

	var gr = new GlideRecord('fx_currency_instance');
    gr.addQuery('id', current.sys_id);
    gr.addEncodedQuery('field=cost');
    gr.query();

    if (gr.next()) {
        var usd = gr.getValue('reference_amount');
    }
    gs.addInfoMessage(usd);
    current.u_cost_usd = usd;
	current.setWorkflow(false);
	current.update();

})(current, previous);

 

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

6 REPLIES 6

Anil Lande
Kilo Patron

Hi,

I hope you have cerate before update/insert BR. If not then please change it to before.

 

 

Thanks,
Anil Lande

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi,

Please create after BR Like below:

find_real_file.png

and use below script:

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

	var gr = new GlideRecord('fx_currency_instance');
    gr.addQuery('id', current.sys_id);
    gr.addEncodedQuery('field=cost');
    gr.query();

    if (gr.next()) {
        var usd = gr.getValue('reference_amount');
    }
    gs.addInfoMessage(usd);
    current.u_cost_usd = usd;
	current.setWorkflow(false);
	current.update();

})(current, previous);

 

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

@Anil Lande 

I had to use an After BR because when the Cost field is updated, the value that is saved in the Cost (USD) field is the previous value. Is there a reason why this should be a Before BR?

The missing line in my script was the current.update().

Thank you.

@Anil Lande 

This worked! Thank you.

A couple of questons though.

1. Why does it have to be a Before BR?
2. Why the need to setWorkflow to false?