The Zurich release has arrived! Interested in new features and functionalities? Click here for more

how to make related list readonly using script

hoathong99
Tera Contributor

my requirement is to make the change request readonly if the current user isn't a change manager or directly associated
here is the requirements:
"
The following restrictions are enforced:
- Change request Type field is Normal or Emergency and State field is New or Assess:     Any user directly associated with the change can edit all fields that are not read-only.
 - Change request Type field is Normal or Emergency and State field is anything except Closed or Canceled: The change_manager role can edit all fields that are not read-only.
-  Change request Type field is Normal or Emergency and State field is New or Assess: Any user directly associated with the change can edit all related lists. 
- Change request Type field is Normal or Emergency and State field is  anything except Closed or Canceled:  The change_manager role can edit all related lists.
- Change task State field is New, In Progress or On Hold:  Any user directly associated with the change or change task, or who has the change_manager role, can edit all fields that are not read-only. 
"
I made a onload script to disable fields but don't know how to do that to the related lists:

function onLoad() {
    var type = g_form.getValue('type');
    var state = g_form.getValue('state');
    var userHasRole = g_user.hasRole('change_manager');

    isUserAssociated(function(userIsAssociated) {
        var allowEdit = false;
        // Case 1: Normal or Emergency, State = New/Assess, associated user
        if ((type == 'normal' || type == 'emergency') &&
            (state == '-5' || state == '-4') && userIsAssociated) {
            allowEdit = true;
        }

        // Case 2: Normal or Emergency, State != Closed/Canceled, change_manager role
        if ((type == 'normal' || type == 'emergency') &&
            (state != '4' && state != '3') && userHasRole) {
            allowEdit = true;
        }

        // If conditions NOT met → lock everything
        if (!allowEdit) {
            setAllReadOnly();
        }
    });
}
function setAllReadOnly() {
    var fields = g_form.getEditableFields();
    for (var i = 0; i < fields.length; i++) {
        g_form.setMandatory(fields[i], false); // remove mandatory first
        g_form.setReadOnly(fields[i], true);
    }
}

 
function isUserAssociated(callback) {
    var currentUser = g_user.userID;
    var requestedBy = g_form.getValue('requested_by');
    var assignedTo = g_form.getValue('assigned_to');

    // Direct check
    if (requestedBy == currentUser || assignedTo == currentUser) {
        callback(true);
        return;
    }

 
    var assignmentGroup = g_form.getValue('assignment_group');
    if (assignmentGroup) {
        var ga = new GlideAjax('ChangeUserUtils');
        ga.addParam('sysparm_name', 'isUserInGroup');
        ga.addParam('groupSysId', assignmentGroup);
        ga.getXMLAnswer(function(answer) {
            callback(answer == 'true');
        });
    } else {
        callback(false);
    }
}
 
0 REPLIES 0