Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business rule not working

Nishitha Yedala
Tera Contributor

Hi Everyone,

 

I am trying to create Incident for the failed ATF tests and I am using business rule to create them. I am using the below business rule and the incident is getting created but with undefined values in the description field.

 

Please let me know where I am doing wrong or is any other way.

 

When to Run:

 

when : after

Insert: checked

Update: checked

Filter condition: Status is Failure

 

 

Script:

 

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

    //var testName = current.test;
    var testName = current.testt;

    gs.info('ATF failure detected for test: ' + testName);


    var inc = new GlideRecord('incident');
    inc.initialize();

    inc.short_description = 'ATF Test Failed: ' + testName;
    inc.description =
        'An ATF test has failed.\n\n' +
        'Test Name: ' + testName + '\n' +
        'Failure Message: ' + current.first_failing_step.summary + '\n' +
        'Executed By: ' + current.sys_created_by + '\n' +
        'Execution Time: ' + current.start_time + '\n\n';

    // Additional fields
    inc.contact_type = 'self-service';
    inc.category = 'incident';
    inc.u_choice_issue = 'other'; // Adjust this if "other" is not a valid choice value
   // inc.caller_id = gs.getUserID(current.sys_created_by) || gs.getUserID(); // fallback to system user if not found

    // Optional categorization
    inc.impact = 3;
    inc.urgency = 2;

    var incSysId = inc.insert(); // Pending (optional, if using same values as RITMs)
    if (incSysId) {
        gs.info('Incident created for failed ATF test: ' + testName + ' (Sys ID: ' + incSysId + ')');
    } else {
        gs.warn('Failed to insert incident for ATF test: ' + testName);
    }

   
})();

 

 

Output:

 

Description: 

An ATF test has failed.

 

Test Name: undefined

Failure Message: undefined

Executed By: undefined

Execution Time: undefined

 

 

Thanks in advance!

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Nishitha Yedala 

Seems your field names etc are wrong, check below

BR should be on "sys_atf_test_result"

Condition: Status [Changes To] Error

Script:

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

    // Add your code here(function executeRule(current, previous) {

    var testName = current.test.name || current.test; // Get test name
    gs.info('ATF failure detected for test: ' + testName);

    var failureMessage = current.first_failing_step ? current.first_failing_step.summary : 'N/A';

    // Get user display name from sys_created_by reference
    var executedBy = 'Unknown';
    if (current.sys_created_by) {
        var userGR = new GlideRecord('sys_user');
        if (userGR.get('user_name', current.sys_created_by)) {
            executedBy = userGR.getDisplayValue();
        }
    }

    var executionTime = current.getValue('start_time');

    var inc = new GlideRecord('incident');
    inc.initialize();

    inc.short_description = 'ATF Test Failed: ' + testName;
    inc.description =
        'An ATF test has failed.\n\n' +
        'Test Name: ' + testName + '\n' +
        'Failure Message: ' + failureMessage + '\n' +
        'Executed By: ' + executedBy + '\n' +
        'Execution Time: ' + executionTime + '\n\n';

    inc.contact_type = 'self-service';
    inc.category = 'incident';
    inc.u_choice_issue = 'other'; // Adjust if needed

    inc.impact = 3;
    inc.urgency = 2;

    var incSysId = inc.insert();
    if (incSysId) {
        gs.info('Incident created for failed ATF test: ' + testName + ' (Sys ID: ' + incSysId + ')');
    } else {
        gs.warn('Failed to insert incident for ATF test: ' + testName);
    }


})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader