Change Request Client Script Issue

Wasdom_Kung
Tera Guru

Hello,

 

I am currently working on a client script within change requests to achieve the following:

 

If all of these are true - 
Request company value is NHS Lothian (sys id, ad0459981b7c0c100375ca217e4bcbb0)

State changes to Closed (value, 3)

Assignment group is not NHSL-SN-ChangeManagement (sys id, 35f1f914dbf00c50731381cc0b961980)

 

Then revert state to Review (value, 0) and alert user that change requests can only be closed by change management.

 

I've tried with both onSubmit and onChange scripts but can't seem to get it working, below is my current working example, which executes when the state field changes however I am unsure if this is the best option to use as it would run on every state change and not just at the Review>Closed state which is what I am looking for, any help would be much appreciated.

 

 

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

    //Type appropriate comment here, and begin script below
    var state = g_form.getValue('state');
    var assignmentGroup = g_form.getValue('assignment_group');
    var company = g_form.getValue('company');

    // alert ('State: ' + state + 'AG: ' + assignmentGroup + 'Company: ' + company);

    if (company == 'ad0459981b7c0c100375ca217e4bcbb0' && state == '3' && assignmentGroup != '35f1f914dbf00c50731381cc0b961980') {

        g_form.setValue('state', '0');

        alert('Change requests can only be closed by NHSL-SN-ChangeManagement');
    }
}

 

 

Sid_Takali
Kilo Patron

Hi @Wasdom_Kung Try below  onChange client script 

 

 

 

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

    //Type appropriate comment here, and begin script below
    var state = g_form.getValue('state');
    var assignmentGroup = g_form.getValue('assignment_group');
    var company = g_form.getValue('company');

    var CLOSED_STATE = '3';
    var REVIEW_STATE = '0';
    var TARGET_COMPANY_ID = 'ad0459981b7c0c100375ca217e4bcbb0';
    var TARGET_ASSIGNMENT_GROUP_ID = '35f1f914dbf00c50731381cc0b961980';

    if (oldValue == REVIEW_STATE && newValue == CLOSED_STATE) {
        if (company == TARGET_COMPANY_ID && assignmentGroup != TARGET_ASSIGNMENT_GROUP_ID) {

            g_form.setValue('state', REVIEW_STATE);
            alert('Change requests can only be closed by NHSL-SN-ChangeManagement');
        }
    }

}

 

 

 

  

Thanks for your response,

I've implemented it as below, but when changing from review to closed, nothing happens and it can be set to closed state.

Wasdom_Kung_0-1722597249199.png

 



Sandeep Rajput
Tera Patron

@Wasdom_Kung Ideally, you should validate the state change using an onSubmit script and not via an onChange script. User can always submit the form even if they chose an incorrect value using an onChange script. However, you have an option to stop the form submission using an onSubmit script. 

 

You can try the following onSubmit client script.

 

function onSubmit(){

    //Type appropriate comment here, and begin script below
    var state = g_form.getValue('state');
    var assignmentGroup = g_form.getValue('assignment_group');
    var company = g_form.getValue('company');

    // alert ('State: ' + state + 'AG: ' + assignmentGroup + 'Company: ' + company);

    if (company == 'ad0459981b7c0c100375ca217e4bcbb0' && state == '3' && assignmentGroup != '35f1f914dbf00c50731381cc0b961980') {

        g_form.setValue('state', '0');

        alert('Change requests can only be closed by NHSL-SN-ChangeManagement');
     return false;
    }
else{
return true;
}
}

 

Hope this helps.

@Wasdom_Kung Try this onSubmit script.