Business Rules to update record on database

Pawel Barcik
Mega Guru
(function executeRule(current, previous /*null when async*/) {
var appname = current.name;
var sup = current.supported_by.toString();

var database = new GlideRecord('cmdb_ci_database');
database.query('u_application', 'name', appname);
database.query();

while(database.next()){
database.supported_by = sup;
database.update();

}

})(current, previous);
 
this sciprt not working, pleas help 
1 ACCEPTED SOLUTION

Pawel Barcik
Mega Guru
(function executeRule(current, previous /*null when async*/) {
    var appname = current.sys_id; // name string
    var sup = current.supported_by.toString(); // supported_by referenc

    var database = new GlideRecord('cmdb_ci_database');

    database.addQuery('u_application.sys_id', appname);  
    database.query();
    while(database.next()){
        database.supported_by = sup; // supported_by referenc
        database.update();
    }

})(current, previous);

View solution in original post

4 REPLIES 4

Dibyaratnam
Tera Sage

database.query('u_application''name'appname); ---> database.addQuery('u_application', <operator>, appname); change to this. This may be causing the issue.

(function executeRule(current, previous /*null when async*/) {
var appname = current.name; // name string
var sup = current.supported_by.toString(); // supported_by referenc

var database = new GlideRecord('cmdb_ci_database');
database.addQuery('u_application', 'name', appname);  
database.query();

while(database.next()){
database.supported_by = sup; // supported_by referenc
database.update();

}

})(current, previous);
 
don't work 

Ankur Bawiskar
Tera Patron
Tera Patron

@Pawel Barcik 

Please share some details

1) appname holds sysId or name

2) supported_by on cmdb_ci_database is reference type then does sup variable hold same table sysId

try this once

(function executeRule(current, previous /*null when async*/) {
	var appname = current.name; // name string
	var sup = current.supported_by.toString(); // supported_by referenc

	var database = new GlideRecord('cmdb_ci_database');
	database.addQuery('u_application', appname);  
	database.query();
	while(database.next()){
		database.supported_by = sup; // supported_by referenc
		database.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

Pawel Barcik
Mega Guru
(function executeRule(current, previous /*null when async*/) {
    var appname = current.sys_id; // name string
    var sup = current.supported_by.toString(); // supported_by referenc

    var database = new GlideRecord('cmdb_ci_database');

    database.addQuery('u_application.sys_id', appname);  
    database.query();
    while(database.next()){
        database.supported_by = sup; // supported_by referenc
        database.update();
    }

})(current, previous);