Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Error when calculating difference between fields

Reinaldo Vieira
Tera Contributor

Hello everyone.

 

I am involved in a task of calculating the difference between fields in a SCTASK as follows: the value of the reimbursement for financial loss ('reimbursement_request_amount') CANNOT BE GREATER than the value of the approved reimbursement amount ('reimbursement_approved_amount').

 

If the value of the second field is greater than the first, the form must not allow Update/Save. I've created an onSubmit catalog client script for this, but it's only working after one or two attempts and then block the submission of the form and generates the error message - no matter how lower is the value on 'reimbursement_approved_amount' field.

 

Could anyone help me with this?

 

Please find attached the catalog client script:

 

 

 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var amount = g_form.getValue('reimbursement_request_amount');
	var approvedAmount = g_form.getValue('reimbursement_approved_amount');

    if (approvedAmount > amount) {
		g_form.addErrorMessage('O valor aprovado de reembolso não pode ser maior que o valor do ressarcimento. Por favor, tente novamente');
        return false;
    }

}

 

 

 

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Yes, parseFloat is also a builtin function in Javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

 

Just replace parseInt with parseFloat. I can be that there are difficulties with the format of the strings. Make sure you are using a '.' as separator.

 

For example:

function onSubmit() {
    var amount = g_form.getValue('reimbursement_request_amount');
    var approvedAmount = g_form.getValue('reimbursement_approved_amount');

    // If necessary, first remove al " . "   , then replace all " , " with " . " 
    amout = parseInt(amount.replace(/\./g, "").replace(/,/g, "."));
    approvedAmount = parseInt(approvedAmount.replace(/\./g, "").replace(/,/g, "."));

    if (approvedAmount > amount) {
		g_form.addErrorMessage('O valor aprovado de reembolso não pode ser maior que o valor do ressarcimento. Por favor, tente novamente');
        return false;
    }
}

View solution in original post

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

Trying clearing messages at the beginning of the script.

g_form.clearMessages();

Also consider an alternate approach with a script UI Policy

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-... 

Brian Lancaster
Kilo Patron

You have to use parseFloat. Also shouldn't you be checking to if the amount is greater then the approvedAmount?

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var amount = g_form.getValue('reimbursement_request_amount');
	var approvedAmount = g_form.getValue('reimbursement_approved_amount');

    if (parseFloat(amount) > parseFloat(approvedAmount)) {
		g_form.addErrorMessage('O valor aprovado de reembolso não pode ser maior que o valor do ressarcimento. Por favor, tente novamente');
        return false;
    }

}