On Change client script not working on integer like I'd expect.

phaug
Tera Contributor

I'm working on the OnChange client script below, expecting the error message to appear if a user tries to increase the value above 100 from the current value. I'd suspect since it's an integer my If statement would work, but it's not capturing the data as I'd expect. Any help is appreciated.

 

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

    //Type appropriate comment here, and begin script below
    var newVal = parseInt(newValue);
   
    if (newVal > oldValue + 100){
        g_form.addErrorMessage("Cannot increment the priority sequence by more than 100.");
        g_form.setValue(oldValue);
    }
       
}
9 REPLIES 9

Hi @phaug ,

In 10 th line change u_integer with priority sequence field backend name then the field value will update with old value.

After line 10 add below script because when you change priority sequence the error message is fixed after you have done change to the field still error message exit.

g_form.clearMessages();

Thanks,

Anand

 

I figured it out,  I had to do this because the commas were breaking it

 

  //Type appropriate comment here, and begin script below
       var newVal = parseInt(newValue.toString().replace(",", ""));
    if (newVal > (parseInt(oldValue.toString().replace(",", "")) + 100)) {
        g_form.addErrorMessage("Cannot increment the priority sequence by more than 100.");
        //g_form.clearMessages();
        g_form.setValue('u_priority_sequence',oldValue.toString());
    }
   

@phaug Please mark this as helpful if its serves your purpose.

Will this stop a user from changing it from the list view as well? It doesn't look like it does.

 

Hi @phaug ,

Onchange client script will not work on list view so you have to write OnCellEdit client script but it will give alert only so use Before update or insert  business rule condition priority changes to achieve your requirement.

AnandKumarP_0-1699338455393.png

 

AnandKumarP_1-1699338501310.png

 

(function executeRule(current, previous /*, gs */) {
    var newVal = parseInt(current.u_integer);
    var oldVal = parseInt(previous.u_integer);

    if (newVal > (oldVal + 100)) {
        gs.addErrorMessage("Cannot increment the priority sequence by more than 100.");
        current.setAbortAction(true); // Prevent the record from being saved
    }
})(current, previous);
Please mark it as solution proposed and helpful it its serves your purpose.

Thanks,

Anand