Population HR Case -opened for User name to Short description of HR Task

Bhavani3
Tera Contributor

I have a requirement include Opened for user name in Hr Task form short description field 

i created business rule but not working as expected .can somebody help me on this to sort out ?

Advance Thanks

 

Here below short description field  have to append Opened for user name

Bhavani3_0-1721662536746.png

 

6 REPLIES 6

Bhavani3
Tera Contributor

Added business rule but not working

(function executeRule(current, previous /*null when async*/) {
         // Get the parent HR Case
         var hrCase = current.case;
 
         if (hrCase) {
             // Fetch the HR Case record
             var hrCaseRecord = new GlideRecord('sn_hr_core_case');
             if (hrCaseRecord.get(hrCase)) {
                 // Get the "opened for" user's name
                 var userName = hrCaseRecord.opened_for.getDisplayValue();
                 
                 // Combine HR Case short description and user name into HR Task short description
                 current.short_description = hrCaseRecord.short_description + ' - Opened for ' + userName;
             }
         }
     })(current, previous);

Hi @Bhavani3 try below code 

(function executeRule(current, previous /*null when async*/) {
        var hrCase = current.case;

    if (hrCase) {
        var hrCaseRecord = new GlideRecord('sn_hr_core_case');
        if (hrCaseRecord.get(hrCase)) 
         var openedForUser = hrCaseRecord.opened_for;
            if (openedForUser) {
                var userName = openedForUser.getDisplayValue();
                current.short_description = hrCaseRecord.short_description + ' - Opened for ' + userName;
            } else {
                gs.log('Opened for user not found for HR Case: ' + hrCase);
            }
        } else {
            gs.log('HR Case record not found for sys_id: ' + hrCase);
        }
    } else {
        gs.log('HR Case sys_id is empty or undefined');
    }
})(current, previous);

The below code is running each time and in short description field of hr task displays the user name duplicates name

BR-table-hr case condition-before-insert,update

(function executeRule(current, previous /*null when async*/) {
    // Get the opened for user name
    var openedForUser = current.opened_for.getDisplayValue();
 
    // Get the HR Task record
    var hrTask = new GlideRecord('sn_hr_core_task');
    hrTask.addQuery('parent', current.sys_id);  // Assuming the HR Task is linked to the HR Case via the 'case' field
    hrTask.query();
 
   while (hrTask.next()) {
        // Append the opened for user name to the short description
        hrTask.short_description = hrTask.short_description + ' - ' + openedForUser;
       hrTask.update();
    }
   
})(current, previous);

Sandeep Rajput
Tera Patron
Tera Patron

@Bhavani3 Simply use the following script on an onBefore insert business rule on sn_hr_core_case table.

current.short_description = current.short_description + ' - Opened for ' + current.opened_for.name;

Hope this helps.