Available and Not Available for in Record Producer is not working

DileepK14831342
Tera Contributor

Hi All,

I have a record producer and I need to write Available for and Not Available for Conditions as per the screenshots attached.

Available for: 

Full-Time/Part-Time - Full-Time AND  Regular/Temporary - Regular

OR

Full-Time/Part-Time - Part-Time AND Regular/Temporary - Regular AND Working hours - Greater than or equal to 15 hours

 

Not Available for:

Regular/Temporary - Temporary

OR

Full-Time/Part-Time - Part-Time AND Regular/Temporary - Regular AND Working hours - less than 15 hours

 

Please note that

1. Regular/Temporary and Full time/Part time are fields from HR profile extension table

2. Working hours are from HR profile table and Working hours is string field.

 

I have written code as below for both Available For and Not Available for in user criteria, but its not working.

 

Not Available For:

(function () {
var userSysId = gs.getUserID();
if (!userSysId) return true; // safer: block if user not found

// 1) HR Profile: get working hours
var prof = new GlideRecord('sn_hr_core_profile');
prof.addQuery('user', userSysId);
prof.setLimit(1);
prof.query();
if (!prof.next()) return true; // no HR Profile -> block

var whStr = (prof.getValue('u_working_hours') || '').trim();
var m = whStr.match(/(\d+(\.\d+)?)/);
if (!m) return true;

var hours = parseFloat(m[1]);
if (isNaN(hours)) return true;

// 2) HR Profile Extension: get FT/PT and Regular/Temporary
var ext = new GlideRecord('sn_hr_core_hr_profile_extension');

var refField = ext.isValidField('hr_profile') ? 'hr_profile'
: ext.isValidField('profile') ? 'profile'
: ext.isValidField('u_hr_profile') ? 'u_hr_profile'
: '';

if (!refField) return true;

ext.addQuery(refField, prof.getUniqueValue());
ext.setLimit(1);
ext.query();
if (!ext.next()) return true;

var ftpt = (ext.getValue('u_ftpt') || '').toString(); // F or P
var rtemp = (ext.getValue('u_rtemp') || '').toString(); // R or T

// Not Available For rules:
// 1) Temporary -> always block
if (rtemp === 'T')
return true;

// 2) Regular Part Time AND Working hours < 15 -> block
if (rtemp === 'R' && ftpt === 'P' && hours < 15)
return true;

return false;
})();

 

Available For:

(function () {
var userSysId = gs.getUserID();
if (!userSysId) return false;

// 1) HR Profile: get working hours
var prof = new GlideRecord('sn_hr_core_profile');
prof.addQuery('user', userSysId);
prof.setLimit(1);
prof.query();
if (!prof.next()) return false;

// Working hours is stored as string, ex: "16.0"
var whStr = (prof.getValue('u_working_hours') || '').trim();
var m = whStr.match(/(\d+(\.\d+)?)/);
if (!m) return false;

var hours = parseFloat(m[1]);
if (isNaN(hours)) return false;

// 2) HR Profile Extension: get FT/PT and Regular/Temporary
var ext = new GlideRecord('sn_hr_core_hr_profile_extension');
ext.addQuery('u_hr_profile', prof.getUniqueValue());
ext.setLimit(1);
ext.query();
if (!ext.next()) return false;

var ftpt = (ext.getValue('u_ftpt') || '').toString(); // F or P
var rtemp = (ext.getValue('u_rtemp') || '').toString(); // R or T

// Available For rules:
// 1) Regular Full Time
if (rtemp === 'R' && ftpt === 'F')
return true;

// 2) Regular Part Time AND Working hours > =15
if (rtemp === 'R' && ftpt === 'P' && hours >= 15)
return true;

return false;
})();

 Kindly suggest.

 

1 REPLY 1

Brad Bowman
Kilo Patron

I would recommend testing with only one criteria in place at a time - available for separate from not available for, to be sure they're not conflicting, and don't rely on the user criteria diagnostics - impersonate or login as the user after each change and test availability directly.  Each of your GlideRecords are using a bad form.  You should always use a if (...next()) or while(...next()) block to return a record after the .query() command, then you can still handle the case where no record was found:

var prof = new GlideRecord('sn_hr_core_profile');
prof.addQuery('user', userSysId);
prof.setLimit(1);
prof.query();
if (prof.next()) {
    var whStr = (prof.getValue('u_working_hours') || '').trim();
    var m = whStr.match(/(\d+(\.\d+)?)/);
    if (!m) return true;

    var hours = parseFloat(m[1]);
    if (isNaN(hours)) return true;
} else {
    return true; // no HR Profile -> block
}

Beyond this with your custom tables and complex requirements no one is going to be able to re-create this, so you'll have to add logging to confirm the if statements that are satisfied, GlideRecords returned, values of script variables, etc to see where it is going wrong.