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

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

briannice
Kilo Sage

Hi @Reinaldo Vieira 

 

Maybe you should try to parse the information to integers. Also make sure that both variable are mandatory, because the function parseInt will not work with an empty string.

 

For example:

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

    amout = parseInt(amount);
    approvedAmount = parseInt(approvedAmount);

    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;
    }
}

 

Let me know if this works.

 

Kind regards,

Brian

It seems a good solution. However, the value can be broken in cents.

ReinaldoVieira_1-1723489895561.png

 



Can we use a parseFloat as well? If so, how?

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;
    }
}

Great! It worked for me with me, but with the parseFloat instead of parseInt.

 

Thank you very much 🙂