The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Help with BR script

Community Alums
Not applicable

Hi all,

I want to add a new functionality to this Business Rule script.

The script runs on the "x_cawa_metering_se_fsm_raw_meter_reads" table, which is set to run async on insert. This table has a reference field called "u_work_order_task" that links to the "wm_task" table, where there is a field named "close_notes". I would like to append the value from the "close_notes" field to another field called "readercomment" on the "x_cawa_metering_se_fsm_raw_meter_reads" table. Can anyone help? Thanks

 

 

This is the current script: 

(function executeRule(current, previous /*null when async*/ ) {
    var contractor = '1db4bc981b8fa5103c81ecade54bcb3d'; // Field team

    var spid = '';
    var code = '';
    var skip_code = '';
    var contractorComments = '';

    if (!gs.nil(current.readercomment.toString().trim())) {
        contractorComments += "Reader comments: " + current.readercomment.toString().trim();
    }

    if (!gs.nil(current.meter)) {
        spid = current.meter.u_spid;
    }

    var skipSysIds = current.skip_code;
    if (!gs.nil(skipSysIds)) {
        var sysIdArr = skipSysIds.split(',');

        for (var i = 0; i < sysIdArr.length; i++) {
            var codeGr = new GlideRecord('u_skip_code');
            if (codeGr.get(sysIdArr[i])) {
                if (i == 0) {
                    // First skip - we need to use this as the code
                    code = codeGr.getValue('u_code');
                } else if (i == 1) {
                    if (!gs.nil(contractorComments)) {
                        contractorComments += ". ";
                    }
                    contractorComments += "Additional skip codes: " + codeGr.getValue('u_code');
                } else {
                    contractorComments += ", " + codeGr.getValue('u_code');
                }
            }
        }
    }
    var grOnSkipCode = new GlideRecord('x_cawa_metering_se_skip_code_mapping');
    grOnSkipCode.addQuery('supplier', contractor);
    grOnSkipCode.addQuery('code', code);
    grOnSkipCode.setLimit(1);
    grOnSkipCode.query();
    if (grOnSkipCode.next()) {
        skip_code = grOnSkipCode.getValue('sys_id');
    }

    var grOnSkip = new GlideRecord('x_cawa_metering_se_meter_reading_skips');
    grOnSkip.initialize();
    grOnSkip.setValue('spid', spid);
    grOnSkip.setValue('meter', current.meter);
    grOnSkip.setValue('raw_meter_read', current.sys_id);
    grOnSkip.setValue('contractor_comment', contractorComments);
    grOnSkip.setValue('skip_date', current.meter_read_date);
    grOnSkip.setValue('skip_code', skip_code);
    grOnSkip.setValue('contractor', contractor);
    if(current.unformattedread){
        grOnSkip.setValue('skip_type', "trouble");
    }
    grOnSkip.insert();

})(current, previous);
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

I wasn't clear if the posted script was an attempt to do this, or just existing functionality, so that aside this line will append the close notes of the related work order task to whatever may already be in the readercomment field:

current.readercomment += current.u_work_order_task.close_notes;

 

View solution in original post

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

I wasn't clear if the posted script was an attempt to do this, or just existing functionality, so that aside this line will append the close notes of the related work order task to whatever may already be in the readercomment field:

current.readercomment += current.u_work_order_task.close_notes;