Restrict closing sctask from the list view only for 1 catalog item

payalnkunte
Kilo Contributor
I have created an on celledit client script to restrict sctask to close from list view
but I cannot make it catalog item specific, It is working globally.
 
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    var saveAndClose = true;
    var closedStates = ['3', '4', '7'];
    //Type appropriate comment here, and begin script below


    var gr = new GlideRecord('sc_task');
     gr.addQuery('cat_item', 'a02b27ab3b033210a546bffe23e45a8e');
    gr.query();
    while (gr.next()) {
        var reqItem = gr.cat_item;
    }
// "a02b27ab3b033210a546bffe23e45a8e" is sys_id of catalog item
 if (reqItem == 'a02b27ab3b033210a546bffe23e45a8e') {
  if (closedStates.indexOf(newValue) > -1) {
            alert("You cannot close a Task from the list view. Please open the Task record to close.");
            saveAndClose = false; // prevents closure of sctask on list view
        }
    }

    callback(saveAndClose);
}
 
 
for every catalog it is working or it is not working pleas help me in this 
 
 
 
 

 

2 REPLIES 2

Tanushree Maiti
Mega Sage

Hi @payalnkunte 

 

Create a  List Edit ACL on sc_task:
 
  1. Navigate to ACLs: Go to System Security > Access Control (ACL) and click New.
  2. Set up the ACL:
    • Type: list_edit
    • Operation: write
    • Name: sc_task table, specifically the state field (sc_task.state).
  3. Define the Condition:
    • Add a condition to target the specific catalog item:
      • Request Item.Item | is | [Select your Catalog Item]
  4. Define the Script:
    • Check "Advanced" and use the script below to prevent the update:
      // Allow admins to override
      if (gs.hasRole('admin')) {
          answer = true;
      } else {
          // Prevent changing state to Closed Complete ( 3 )or Closed Incomplete (4 )
          if (newValue == '3' || newValue == '4') {
              answer = false;
              gs.addErrorMessage('Cannot close this specific catalog task from the list view.');
          } else {
              answer = true;
          }
      }
  5. Save the ACL
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

SumanthDosapati
Mega Sage

@payalnkunte 

There's a logical mistake in your script and also Do not use GlideRecord in client scripts. You can try approach shared by @Tanushree Maiti  which will work server side also.

 

Regards,
Sumanth