How to check number of Ctask created for the Change request.

Tamilvanan T
Tera Contributor

Hi, 

The requirement is to check the number of Ctask and if more than one ctask is created for a change request, then the implementation start and end date should be empty. 

 

Please let me know if there a way to check.

 

Thank you!

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Tamilvanan T 

you can query change_task and get the count

Where should this be written? Is this BR on change_request?

If yes then use this

var ga = new GlideAggregate('change_task');
ga.addQuery('change_request', current.sys_id);
ga.addAggregate('COUNT');
ga.query();
if (ga.next()) {
    var count = ga.getAggregate('COUNT');
    if (count > 1) {
        // your logic
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Tamilvanan T 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Tamilvanan T 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sandeep Rajput
Tera Patron
Tera Patron

@Tamilvanan T You can create an onAfter Insert business rule on the change_task table as follows.

 

(function executeRule(current, previous /*null when async*/) {
    // Get the associated Change Request
    var changeRequest = new GlideRecord('change_request');
    if (changeRequest.get(current.change_request)) {
        
        // Count the number of associated Change Tasks
        var ctaskCount = new GlideAggregate('change_task');
        ctaskCount.addQuery('change_request', current.change_request);
        ctaskCount.addAggregate('COUNT');
        ctaskCount.query();
        
        if (ctaskCount.next() && ctaskCount.getAggregate('COUNT') > 1) {
            // Clear Implementation Start and End Dates if more than one CTask exists
            changeRequest.start_date = '';
            changeRequest.end_date = '';
            changeRequest.update();
        }
    }
})(current, previous);