The CreatorCon Call for Content is officially open! Get started here.

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

 

 

1 ACCEPTED SOLUTION

Wasdom_Kung
Tera Guru

This was fixed by using a business rule:

 

(function executeRule(current, previous /*null when async*/) {
    
    var nhsLothianCompanySysID = 'ad0459981b7c0c100375ca217e4bcbb0';
    var changeManagementGroupSysID = '61808333dba8c45089618a5039961911';

    // Check if the request company is NHS Lothian
    if (current.company == nhsLothianCompanySysID) {
        // Check if the state is being changed to Closed (value 3)
        if (current.state == 3) {
            // Check if the assignment group is not NHSL-SN-ChangeManagement
            if (current.assignment_group != changeManagementGroupSysID) {
                // Revert the state to Review
                current.state = 0;
                // Add an error message
                gs.addErrorMessage('Change requests can only be closed by change management');

                // Prevent the record from being saved in the current (closed) state
                current.setAbortAction(true);
            }
        }
    }
})(current, previous);

View solution in original post

12 REPLIES 12

Hi Sandeep,

I had a similar version to this before, but would prevent closing the change request even if the group was set to NHSL-SN-ChangeManagement, that version didn't account for the company value though (I also tried impersonating a member and had the same issue), however with the below version, it saves with no issues as an incorrect group.

 

Wasdom_Kung_0-1722598161109.png

 

@Wasdom_Kung Try with following. I have replaced alert with an addErrorMessage(). This should prevent the form submission.

 

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');

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

 

Still not working, I have a feeling that it isn't parsing the conditions for company, state and assignment group correctly

@Wasdom_Kung Are you see the error message on form? or the form is getting submitted despite having incorrect values?

So the form was being submitted without any errors, so it is saying that the assignment group value is correct when it is not.

 

I have updated the script a little bit to the below, which now says that the assignment group is never NHSL-SN-ChangeManagement, I have tested with both the sys_id and the name of the group and have the same issue

 

 

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(assignmentGroup != '35f1f914dbf00c50731381cc0b961980' && state == '3' && company == 'ad0459981b7c0c100375ca217e4bcbb0' ){// (company == 'ad0459981b7c0c100375ca217e4bcbb0' && state == '3' && assignmentGroup != '35f1f914dbf00c50731381cc0b961980'){

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

        g_form.addErrorMessage('Change requests can only be closed by NHSL-SN-ChangeManagement');
		
        return false;

    } else {

        return true;
    }
}

 

 

Outcome:

Wasdom_Kung_1-1722601777809.png