need help in business rule

sn37
Giga Contributor

I have a business rule which runs on the TASK table, conditions are when the state of the particular TASK based on the short description changes to closed complete.

In the task, there is a field with the backend value 'request_item.configuration_item', which is referenced to Computer System table. It stores the computer assigned to the requestor. So here I am querying the table and finding that system and then updating the field in that form. But it's not updating.

With the logs added, I am getting this

'[object GlideRecord] fad58863134aab405d3870146144b0a9 TEST

 

(function executeRule(current, previous /*null when async*/ ) {
    var computer = current.request_item.configuration_item;
    var computerSystem = new GlideRecord('u_cmdb_ci_computersystem');
    computerSystem.addQuery('name', computer);
    computerSystem.query();
gs.log(computerSystem + computer + 'TEST');
    while (computerSystem.next()) {
        computerSystem.setValue('install_status', 'Return');
    }
})(current, previous);

 

@Ankur Bawiskar 

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

 

If that log represents the correct record on the custom table, add a line within the while block to save the update:

 

computerSystem.update();

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@sn37 

you should query with sysId and use update method

(function executeRule(current, previous /*null when async*/ ) {
	var computer = current.request_item.configuration_item;
	var computerSystem = new GlideRecord('u_cmdb_ci_computersystem');
	computerSystem.addQuery('sys_id', computer);
	computerSystem.query();
	if (computerSystem.next()) {
		computerSystem.setValue('install_status', 'Return'); // use correct choice value for the field
		computerSystem.update();
	}
})(current, previous);

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

Amit Gujarathi
Giga Sage
Giga Sage

HI @sn37 ,
I trust you are doing great.
Please find updated script as given below

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

    // Check if the state of the task has changed to 'Closed Complete'
    if (current.state.changesTo('Closed Complete')) {

        // Get the 'configuration_item' value from the 'request_item' field
        var computer = current.request_item.configuration_item.getDisplayValue();

        // Query the 'u_cmdb_ci_computersystem' table to find the matching computer system
        var computerSystem = new GlideRecord('u_cmdb_ci_computersystem');
        computerSystem.addQuery('name', computer);
        computerSystem.query();

       

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi