Require work notes on Change Request update

athavichith
Mega Sage

I want to force work notes to be required whenever any update on Change Request when in Scheduled state. I'm using a onChange client script, below is my script is not working:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        var state = g_form.getValue('state');
        if (!g_form.isNewRecord() && state == -2) {
            g_form.setMandatory('work_notes', true);
            g_form.addErrorMessage('Work note is required when in Scheduled state and record is updated');
           

        }
        return;
    }

    //Type appropriate comment here, and begin script below
1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

HI @athavichith ,

try this onSubmit client script instead of onChange

 

ChaitanyaILCR_0-1767808426564.png

 

function onSubmit() {
    var state = g_form.getValue('state');
    if (!g_form.isNewRecord() && state == '-2' && !g_form.getValue('work_notes')) {
        if (Object.keys(g_form.modifiedFields).length) {
            g_form.setMandatory('work_notes', true);
            g_form.addErrorMessage('Work note is required when in Scheduled state and record is updated');
            return false;
        }
        return true;
    }

}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

3 REPLIES 3

WillieW
Tera Expert

That will run when the field it is defined for is changed, and the logic will execute when the newValue = ''.  Seems the problem is with you "if()" statement logic.

John Gilmore
Tera Guru

@athavichith 

The way your script is written you are putting all logic inside the conditional for if(isLoading || newValue === ")

update your script with the following formatting:

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

     //insert your scripted logic here
     var state = g_form.getValue('state');

     if(!g_form.isNewRecord() && state == -2) {
          g_form.setMandatory('work_notes', true);
          g_form.addErrorMessage('Work note is required when in Scheduled state and record is updated');
     }
}

 

Without more context on where you are applying the onChange script it's hard to troubleshoot but this should work if you are applying it as an onChange script targeting the state field.

I would also recommend adding an else to your if that reverts the setting of work_notes back to not mandatory for handling instances where the user may set the state to scheduled and then change it to another state for some reason.

Chaitanya ILCR
Mega Patron

HI @athavichith ,

try this onSubmit client script instead of onChange

 

ChaitanyaILCR_0-1767808426564.png

 

function onSubmit() {
    var state = g_form.getValue('state');
    if (!g_form.isNewRecord() && state == '-2' && !g_form.getValue('work_notes')) {
        if (Object.keys(g_form.modifiedFields).length) {
            g_form.setMandatory('work_notes', true);
            g_form.addErrorMessage('Work note is required when in Scheduled state and record is updated');
            return false;
        }
        return true;
    }

}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya