Add recruiter to watchlist

HiranamyeeM
Tera Contributor

Hi All !

 

Add the recruiter in the watchlist in HR case table automatically.

 

Wrote a BR, but not working. kindly help !

 

TIA

 

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

var recruiterID = current.u_recruiter.toString();

if (!recruiterID) {
return;
}

var watchList = current.watch_list.toString();

// 3. Add the recruiter to the watchlist if they aren't already on it
if (watchList.indexOf(recruiterID) === -1) {
if (watchList.length > 0) {
current.watch_list = watchList + ',' + recruiterID;
} else {
current.watch_list = recruiterID;
}
}
})(current, previous);

 

 

5 REPLIES 5

Tanushree Maiti
Tera Patron

Hi @HiranamyeeM 

 

Try with Before Update Business Rule with Proper trigger condition.

Script:

 

(function executeRule(current, previous /*null when async*/) {
var recruiterID = current.u_recruiter.toString();
if (recruiterID)

 {
  var watchlist = current.watch_list ? current.watch_list.split(',') : [];

            if (watchlist.indexOf(recruiterID) == -1)

            {

                watchlist.push(recruiterID);

                current.watch_list = watchlist.join(',');

            }

}
})(current, previous);

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Thanks @Tanushree Maiti , for your help !

Juhi Poddar
Kilo Patron

Hello @HiranamyeeM 

Try this:

Business Rule Configuration

  • Name: Add Recruiter to Watch List
  • Table: HR Case [sn_hr_core_case]
  • When: before
  • Insert: Checked
  • Update: Checked
  • Condition: Recruiter | changes  OR  Recruiter | is not empty
(function executeRule(current, previous /*null when async*/) {
    
    try {
        // Get the recruiter sys_id
        var recruiterID = current.getValue('u_recruiter');
        
        // Exit if no recruiter
        if (!recruiterID) {
            gs.debug('AddRecruiterToWatchList: No recruiter specified');
            return;
        }
        
        // Get current watch list
        var watchList = current.getValue('watch_list');
        var watchListArray = watchList ? watchList.split(',') : [];
        
        // Log current state
        gs.debug('AddRecruiterToWatchList: Current watch list: ' + watchList);
        gs.debug('AddRecruiterToWatchList: Recruiter ID: ' + recruiterID);
        
        // Check if recruiter already exists in watch list
        if (watchListArray.indexOf(recruiterID) > -1) {
            gs.debug('AddRecruiterToWatchList: Recruiter already in watch list');
            return;
        }
        
        // Add recruiter to watch list
        watchListArray.push(recruiterID);
        current.setValue('watch_list', watchListArray.join(','));
        
        gs.info('AddRecruiterToWatchList: Added recruiter ' + recruiterID + ' to watch list');
        
    } catch (e) {
        gs.error('AddRecruiterToWatchList: Error - ' + e.message);
    }
    
})(current, previous);

Hope this helps!

 

Thank You!

Juhi Poddar

Thank You!
Juhi Poddar

Thanks @Juhi Poddar , for your help !