How to restrict the assignee to close one of the sc tasks in catalog item without an attachment

Avee90
Tera Contributor

Hello Folks,

    I've a catalog item which will generate 6 SC tasks. For one of the SC task, user should not be able to close the SC task without an attachment. How can I proceed with the solution on this. Help me to provide any scrips.

Thanks 

5 REPLIES 5

Dhananjay Pawar
Kilo Sage

Maddysunil
Kilo Sage

@Avee90 

If that one sc task has uniques short description then you can write before update business rule on sc task below is the sample script:

 

(function executeRule(current, previous /*null when async*/) {

    // Define the unique short description for the SC task that requires an attachment
    var uniqueShortDescription = "Unique Description"; // Replace with the actual unique short description

    // Check if the current SC task is in the "Closed" state
    if (current.state == '6') { // '6' represents the 'Closed' state in ServiceNow
        // Check if the SC task has the unique short description
        if (current.short_description == uniqueShortDescription) {
            // Check if the SC task has an attachment
            if (current.attachment.nil()) {
                // If there is no attachment, prevent the SC task from being closed
                gs.addErrorMessage('You cannot close this task without attaching a file.');
                current.state = previous.state; // Rollback the state change
                current.setAbortAction(true); // Prevent the record from being saved
            }
        }
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Community Alums
Not applicable

Hi @Avee90 ,

I tried your problem in my PDI and it works for me please refer below steps

1. Create Before Business rule, add table - Catalog Task (sc_task) and add appropriate filter condition 

SarthakKashya2_0-1713165249258.png

 

In Advance Section 

SarthakKashya2_1-1713165299403.png

Add below script

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
	gs.log('BR Called = ' + current.getTableName());
    var att = new GlideAggregate('sys_attachment');
    att.addAggregate('COUNT');
    att.addQuery('table_name', current.getTableName());
    att.addQuery('table_sys_id', current.sys_id);
    att.query();
    var count = 0;
    if (att.next()) {
        count = att.getAggregate('COUNT');
        if (count < 1) {
            gs.addInfoMessage("Please attach Attachment");
            current.setAbortAction(true);
        }
    }
})(current, previous);

 

Here's Result 

SarthakKashya2_3-1713165364195.png

 

Please mark my answer correct and helpful if it works for you

 

Thanks and Regards 

Sarthak

 

 

 

Hello Sarthak,

   Thanks for your response. I tried BR, I'm getting the error message but the sc task is closing.