Script where newValue < oldValue + 50

LyndseySharpe
Tera Contributor

Hi all,

 

I've got a requirement to add some audit conditions to the total cost field on a purchase order line item. Currently, our ITP department just update the cost if things like delivery costs change, but this is obviously an audit issue as the original PO for the original cost has been approved, not this new cost.

To get around this, we want to build some functionality where if the total cost on the PO line item is updated, it is only allowed to go through if the new value is less than the old, original value, + £50. If the new value is over the old value + £50, there will be an error message that is displayed, which stops them from processing the change. I've created an onChange client script for this, but I've never actually scripted before and just copied an existing one with tweaks, so it's not working. Is anyone able to help me and let me know what the new script should be? The script is included in the attachment.

 

Thanks 🙂

1 ACCEPTED SOLUTION

Hi Lyndsey,

I looked into this closer on my (mostly) OOB PDI.  In my instance Total Cost is read-only and is updated after an update is made where the Cost field changes, so the onChange would have to be triggered on the Cost field, not Total Cost.  It would also be better to do this as a Business Rule, so that there are not client ways to circumvent it.  This is what I got to work for updating the Cost field:

BradBowman_0-1671026164667.png

The Script on the Advanced tab would look like this:

(function executeRule(current, previous /*null when async*/) {
	var POTotalNew = parseFloat(current.cost);
	var POTotalOld = parseFloat(previous.cost);
	if (POTotalNew > POTotalOld + 50) {
		gs.addErrorMessage('NO!');
		current.setAbortAction(true);
	}
})(current, previous);

If you have Total Cost editable in your instance it should work the same changing the Filter Condition and field name in the script.  In my case I get the custom error message, another OOB error message 'Invalid Update', but the form appears to be updated.  If you reload you'll see that the update was not permitted, so that's something a bit odd to be aware of.

 

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

newValue is defined in the function, so use it in the script without quotes on your getDecimalValue line

Thank you! I tried this but unfortunately it's still not working. It just doesn't do anything when I try to save the form, it just actions it without any error!

Hi Lyndsey,

I looked into this closer on my (mostly) OOB PDI.  In my instance Total Cost is read-only and is updated after an update is made where the Cost field changes, so the onChange would have to be triggered on the Cost field, not Total Cost.  It would also be better to do this as a Business Rule, so that there are not client ways to circumvent it.  This is what I got to work for updating the Cost field:

BradBowman_0-1671026164667.png

The Script on the Advanced tab would look like this:

(function executeRule(current, previous /*null when async*/) {
	var POTotalNew = parseFloat(current.cost);
	var POTotalOld = parseFloat(previous.cost);
	if (POTotalNew > POTotalOld + 50) {
		gs.addErrorMessage('NO!');
		current.setAbortAction(true);
	}
})(current, previous);

If you have Total Cost editable in your instance it should work the same changing the Filter Condition and field name in the script.  In my case I get the custom error message, another OOB error message 'Invalid Update', but the form appears to be updated.  If you reload you'll see that the update was not permitted, so that's something a bit odd to be aware of.

 

Thank you so much, this has worked! Hopefully I will get on a scripting course soon and I will be able to answer these types of questions too, instead of just asking them 😂