The CreatorCon Call for Content is officially open! Get started here.

Change request should be Auto closed when Changed task is closed

viswak
Tera Contributor

why my code not working.

 

 

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

// Add your code here
var inc = new GlideRecord('change_task');
inc.addQuery('change_request',current.sys_id);
inc.query();
if(inc.next()){
inc.state = 3;
inc.update();
current.setAbortAction(true);
}
})(current, previous);
4 REPLIES 4

Satishkumar B
Giga Sage
Giga Sage

Hi @viswak 
please refer the updated code:

 

 

(function executeRule(current, previous /*null when async*/) {
    // Query for open change tasks related to the current change request
    var taskGR = new GlideRecord('change_task');
    taskGR.addQuery('change_request', current.sys_id);
    taskGR.addQuery('state', '!=', '3'); // Assuming '3' is the closed state for tasks
    taskGR.query();
    
    // If no open tasks exist, close the change request
    if (!taskGR.hasNext()) {
        current.state = 3; // Assuming '3' is the closed state for the change request
        current.update();
    }
})(current, previous);

 

 

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you to understand. 

Bert_c1
Kilo Patron

Hi @viswak 

 

The subject here has "Change request should be Auto closed when Changed task is closed", for that change your BR definition to run on the change_task table, and for Update. Add conditions:

'state', changes to', 'Closed' AND 'Change Request', 'Is not empty' on the "When to run" section.  Use script logic:

 

 

 

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

	// Add your code here
	var chgRec = new GlideRecord('change_request');
	chgRec.addQuery('sys_id', current.change_request.toString());
	chgRec.query();
	gs.info('Auto-close change: Found ' + chgRec.getRowCount() + " Chg records");
	if (chgRec.next()) {
		chgRec.state = 3;	// Closed choice value
		gs.info('Auto-close change: Closing ' + chgRec.number);
		// set madatory fields when chg is closed
		chgRec.close_notes = current.close_notes;
		chgRec.close_code = current.close_code;
		chgRec.update();
	}
	
})(current, previous);

 

 

Testing the above didn't work, as the OOB logic found other related change_task records that were not closed. So logic needs to be added to check that. The above may work if change_task begin close is the only one change_task record related the the change_request record, but there is other OOB behavior that is affecting closing the change request.

viswak
Tera Contributor

not working

viswak
Tera Contributor