Business Rule not working

SanthoshKumar R
Tera Contributor

Hi Community, I have written a BR to change state of related incidents to resolved when problem is resolved and to copy the fix notes field from problem to resolution notes in associated incidents.  But it is not working can anyone please give the solution for this?

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

    // Add your code here
    var problem=current.sys_id;
    var inc=new GlideRecord('incident');
    inc.addQuery('problem_id',problem);
    inc.query();
    while(inc.next())
    {
         inc.setValue('state','6');
        inc.setValue('close_notes',current.fix_notes);
       
    }

})(current, previous);
1 ACCEPTED SOLUTION

Hi @SanthoshKumar R,

 

Try this updated scripts and your business rule condition should state changes to resolved:

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

	var problem = current.getUniqueValue();
	
	var inc = new GlideRecord('incident');
	inc.addQuery('problem_id', problem);
	inc.query();
	while (inc.next()) {
		inc.setValue('state', '6');
		inc.setValue('close_notes', current.description);
		inc.setValue('close_code', 'Resolved by problem: ' + current.number);
		inc.setWorkflow(false);
		inc.autoSysFields(false);
		inc.update();
		
	}

})(current, previous);

 


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

7 REPLIES 7

AnubhavRitolia
Mega Sage
Mega Sage

Hi @SanthoshKumar R 

 

inc.update(); is missing.

 

Try below code:

 

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

    // Add your code here
    var problem=current.sys_id;
    var inc=new GlideRecord('incident');
    inc.addQuery('problem_id',problem);
    inc.query();
    while(inc.next())
    {
         inc.setValue('state','6');
        inc.setValue('close_notes',current.fix_notes);
        inc.update();
       
    }

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

SanthoshKumar R
Tera Contributor

But it doesn't update related incidents, it is creating a copy of same incident with state as resolved

Eswar Chappa
Mega Sage
Mega Sage

Hi @SanthoshKumar R  Can you please try with the below script.

 

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

    // Add your code here
    var problem = current.sys_id;
    var inc = new GlideRecord('incident');
    inc.addQuery('problem_id', problem);
    inc.query();
    while (inc.next()) {
        inc.state = '6'; // Update the state using direct assignment
        inc.close_notes = current.fix_notes;
	inc.close_code='Resolved by problem';// Update the close notes
        inc.update(); // Update the incident record
    }

})(current, previous);

 

But the issue here in the above script is that Fix notes you are copying from Problem might cause an issue due the HTML data type when updating same to the Incident close notes it will be updated in HTML format.

 

 

Cheers, hope that helps

Eswar Chappa

*** Please mark as "Correct" or "Helpful" as appropriate ***

Same issue is repeated when trying to copy description field instead of ix_notes