Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Cab agenda item form making read only

ServiceNow10sun
Giga Guru

Hi All , 

 

I am trying to make from of table cab_agenda_item as editable only for cab manager and delegates  and for rest all it should be read only . 

i have tried script include and onload client script but its not working. 

 

in cab_agenda_item table meeting is a reference field reference to cab_meeting table which has these two fields cab manager and delegates.

 

CLient sctipt:

 

function onLoad() {
   //Type appropriate comment here, and begin script below
  var meetingSysId = g_form.getValue('cab_meeting');  // Reference field for the cab_meeting table

    if (meetingSysId) {
        var ga = new GlideAjax('cabagendaitem1');
        ga.addParam('sysparm_name', 'isUserAllowed');
        ga.addParam('sys_id', meetingSysId);  // Pass the meetingSysId to the Script Include

        ga.getXMLAnswer(function(response) {
            var isAllowed = response;

            if (isAllowed == 'false') {
                g_form.setReadonly(true);  // Make the form read-only for non-allowed users
            }
        });
    }
}
 
=============================================================
Script include :
===============================================================
var cabagendaitem1 = Class.create();
cabagendaitem1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    initialize: function() {},

    isUserAllowed: function() {
        // Get the current user ID
        var userId = gs.getUserID();

        // Get the meetingSysId from the client-side (passed via GlideAjax)
        var meetingSysId = this.getParameter('sys_id');

        // Query the cab_meeting table
        var meetingRecord = new GlideRecord('cab_meeting');
        if (meetingRecord.get(meetingSysId)) {
            var cabManager = meetingRecord.getValue('manager');
            var delegates = meetingRecord.getValue('delegates');  // List Collector field
           
            // Check if the user is the CAB Manager
            if (cabManager == userId) {
                return true;
            }

            // Check if the user is a Delegate
            if (delegates) {
                var delegateList = delegates.split(',').map(function(item) {
                    return item.trim();
                });

                if (delegateList.indexOf(userId) !== -1) {
                    return true;
                }
            }
        }

        return false;  // Default to false if no match
    },

    type: 'cabagendaitem1'
});

 

 

1 ACCEPTED SOLUTION

siddharth26
Tera Guru

try something like the below  . 

write a write ACL and script include and call that script include in that write ACL but make sure you do changes to the out of the box ACLS as well. 

 

ACL

// Instantiate the Script Include

var smartFlowDefinitionUtils = new cabtest1();

 

// Check if the logged-in user is in the Contributor List of the current Smart Approval Definition record

answer = smartFlowDefinitionUtils.isUserInContributorList(current);

 

 

Script include 
 
var cabtest1 = Class.create();
 
cabtest1.prototype = {
 
 
 
    initialize: function() {},
 
 
 
    // Function to check if the logged-in user is in the Contributor List field
 
    isUserInContributorList: function(approvalDefinitionSysId) {
 
        var approvalDefinition = new GlideRecord('cab_meeting');
 
        if (approvalDefinition.get(approvalDefinitionSysId)) {
 
            var contributorList = approvalDefinition.getValue('delegates').toString();
var cabmgr = approvalDefinition.manager;
 
if((cabmgr==gs.getUserID()||(contributorList.indexOf(gs.getUserID()) !== -1)))
 
return true ;
 
            //return contributorList.indexOf(gs.getUserID()) !== -1;
 
        }
 
        return false;
 
    },
 
 
 
    type: 'cabtest1'
 
};

View solution in original post

2 REPLIES 2

siddharth26
Tera Guru

try something like the below  . 

write a write ACL and script include and call that script include in that write ACL but make sure you do changes to the out of the box ACLS as well. 

 

ACL

// Instantiate the Script Include

var smartFlowDefinitionUtils = new cabtest1();

 

// Check if the logged-in user is in the Contributor List of the current Smart Approval Definition record

answer = smartFlowDefinitionUtils.isUserInContributorList(current);

 

 

Script include 
 
var cabtest1 = Class.create();
 
cabtest1.prototype = {
 
 
 
    initialize: function() {},
 
 
 
    // Function to check if the logged-in user is in the Contributor List field
 
    isUserInContributorList: function(approvalDefinitionSysId) {
 
        var approvalDefinition = new GlideRecord('cab_meeting');
 
        if (approvalDefinition.get(approvalDefinitionSysId)) {
 
            var contributorList = approvalDefinition.getValue('delegates').toString();
var cabmgr = approvalDefinition.manager;
 
if((cabmgr==gs.getUserID()||(contributorList.indexOf(gs.getUserID()) !== -1)))
 
return true ;
 
            //return contributorList.indexOf(gs.getUserID()) !== -1;
 
        }
 
        return false;
 
    },
 
 
 
    type: 'cabtest1'
 
};

ServiceNow10sun
Giga Guru

Thank you it worked