Asking for assistance with BR script

adrianh
Mega Guru

Greetings, 
I'm slowly learning but I seem to be at an impass on a BR Script I need to pull value from one form and copy it over to another form where the names are the same. 

Criteria: 
Backup Testing form 
When a person creates a backup testing record they enter the name of the application, some details and then the Backup Execution Date. I need the date to be transferred to the Application record where the application name is the same as the app name on the backup test record. 

For example: 
Backup Test Record on the Backup Test Table is completed for Active Directory
User enters the date it was done 
the BR script then will look at the cmdb_ci_appl table for an app with the "Active Directory" name and update the Backup Tested Date field on the app record to match the Backup Test Record. 

i have attempted to write the following script which seems to be updating the field but on records OTHER than the Active Directory record. Ugh

Hoping someone may be able to push me in the correct direction and teach me along the way 🙂 

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

        //Purpose - Upd CMDB Appl Rec "Backup Tested Date" when Backup Exec Record "Backup Exec Date" created
     
  //Find records on CMDB Application where Appl Name same as Backup Record Application Name
    var name = new GlideRecord('cmdb_ci_appl');
    //where App.Name (cmdb_ci_appl.name) = Backup Exececution App Name (u_application_name)
    name.addQuery("cmdb_ci_appl.name", current.getValue('u_backup_name'));
    name.setLimit(100); //Comment out after testing
    name.query();

    while (name.next()) {
        //For match, Set Appl BU Tst Date (u_backup_tested_date) = BU Exec Rec BU Tst Date (u_backup_tested_date)
        name.setValue('u_backup_tested_date', current.getValue('u_backup_tested_date'));
        //Update the record.
        name.update();
    }
   
})(current, previous);
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@adrianh Please see if the following works.

 

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

        //Purpose - Upd CMDB Appl Rec "Backup Tested Date" when Backup Exec Record "Backup Exec Date" created
     
  //Find records on CMDB Application where Appl Name same as Backup Record Application Name
    var name = new GlideRecord('cmdb_ci_appl');
    //where App.Name (cmdb_ci_appl.name) = Backup Exececution App Name (u_application_name)
    name.addQuery("name", current.getValue('u_backup_name'));
    name.setLimit(100); //Comment out after testing
    name.query();

    while (name.next()) {
        //For match, Set Appl BU Tst Date (u_backup_tested_date) = BU Exec Rec BU Tst Date (u_backup_tested_date)
        name.setValue('u_backup_tested_date', current.getValue('u_backup_tested_date'));
        //Update the record.
        name.update();
    }
   
})(current, previous);

View solution in original post

5 REPLIES 5

Bidduam
Tera Guru

@adrianh I'd likely not use a business rule to do this - I'd suggest creating a flow with a trigger of when the Backup Execution Date is updated simply use the data pill of the trigger record and do an update record on the other record and update the new field with the other value.

EG:

Bidduam_0-1740709619931.png

 

 

Sandeep Rajput
Tera Patron
Tera Patron

@adrianh Please see if the following works.

 

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

        //Purpose - Upd CMDB Appl Rec "Backup Tested Date" when Backup Exec Record "Backup Exec Date" created
     
  //Find records on CMDB Application where Appl Name same as Backup Record Application Name
    var name = new GlideRecord('cmdb_ci_appl');
    //where App.Name (cmdb_ci_appl.name) = Backup Exececution App Name (u_application_name)
    name.addQuery("name", current.getValue('u_backup_name'));
    name.setLimit(100); //Comment out after testing
    name.query();

    while (name.next()) {
        //For match, Set Appl BU Tst Date (u_backup_tested_date) = BU Exec Rec BU Tst Date (u_backup_tested_date)
        name.setValue('u_backup_tested_date', current.getValue('u_backup_tested_date'));
        //Update the record.
        name.update();
    }
   
})(current, previous);

Ankur Bawiskar
Tera Patron
Tera Patron

@adrianh 

try this if "u_backup_name" if string and holds name

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

    // Purpose - Update CMDB Application Record "Backup Tested Date" when Backup Execution Record "Backup Execution Date" is created

    // Find records on CMDB Application where Application Name is the same as Backup Record Application Name
    var appRecord = new GlideRecord('cmdb_ci_appl');
    // where App.Name (cmdb_ci_appl.name) = Backup Execution App Name (u_backup_name)
    appRecord.addQuery('name', current.getValue('u_backup_name'));
    appRecord.query();

    while (appRecord.next()) {
        // For match, set Application Backup Tested Date (u_backup_tested_date) = Backup Execution Record Backup Tested Date (u_backup_tested_date)
        appRecord.setValue('u_backup_tested_date', current.getValue('u_backup_tested_date'));
        // Update the record
        appRecord.update();
    }

})(current, previous);

OR try this is"u_backup_name" is reference to cmdb_ci_appl table

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

    // Purpose - Update CMDB Application Record "Backup Tested Date" when Backup Execution Record "Backup Execution Date" is created

    // Find records on CMDB Application where Application Name is the same as Backup Record Application Name
    var appRecord = new GlideRecord('cmdb_ci_appl');
    // where App.Name (cmdb_ci_appl.name) = Backup Execution App Name (u_backup_name)
    appRecord.addQuery('sys_id', current.getValue('u_backup_name'));
    appRecord.query();

    while (appRecord.next()) {
        // For match, set Application Backup Tested Date (u_backup_tested_date) = Backup Execution Record Backup Tested Date (u_backup_tested_date)
        appRecord.setValue('u_backup_tested_date', current.getValue('u_backup_tested_date'));
        // Update the record
        appRecord.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

adrianh
Mega Guru

Thank you everyone for the quick responses. I am going to play with these and see what comes out