Scripts for Attribute Configuration¶
Creating an ImportID¶
- Goal:
- Calculating an ImportID by using an employee’s first name and date of birth. Note that this ID is not forcing uniqueness!
- Prerequesite/Settings:
- Text data type and flags: Calculated, on every update, Script.
Script:
var fname = obj.get('FIRSTNAME');
var bday = obj.get('DATE_OF_BIRTH');
if (fname && bday) {
return fname + "_" + Helper.formatDate(bday, 'yyyyMMdd');
}
Calculated Username¶
- Goal:
- Calculating a unique USERNAME, which will be updated in case of a change in first and/or last name. Furthermore, special characters shall be replaced or deleted.
Prerequesite/Settings:
Simple Script without Character Handling:
var value = obj.get('FIRSTNAME') + '.' + obj.get('LASTNAME');
return Helper.ensureUnique(obj, 'USERNAME', value.toLowerCase());
Advanced Script with Replacement of Special Characters:
var fn = obj.get('FIRSTNAME');
var ln = obj.get('LASTNAME');
if (fn && ln) {
var value = fn + '.' + ln;
value = value.toLowerCase().replace(/ö/g, 'oe').replace(/ä/g, 'ae').replace(/ü/g, 'ue')
.replace(/[^0-9a-z.]/g, '');
var current = obj.get('USERNAME');
if (current && (current == value || current.indexOf(value + "_") == 0)) {
return current;
}
return Helper.ensureUnique(obj, 'USERNAME', value);
}
return null;
Two Fields Validation¶
- Goal:
- A ValidationResult field is created.
- Prerequesites/Settings:
- “Text”, “multiline”-flag
- Goal #2:
- A Validation field is created, which executes the calculation.
- Prerequesites/Settings:
- “Calculated”, “on every updates”, mandatory”
- Purpose:
- It makes the ValidationResult field display an error message. If everything goes well, then it sets itself to a certain value. In this way, the mandatory field is filled and the data set is complete. If errors occur, the Validation field will not be filled. In latter case, the data set is incomplete and will not be exported.
obj.set("ValidationResult", "");
var isValid = true;
var messages = [];
var email = obj.get("EMAIL");
if (email && !email.match(/.+@company|@pentos|@firm\..+/)) {
messages.push(email + " is not a valid e-mail address");
isValid = false;
}
var jobCode = obj.get("JOBCODE");
if (jobCode &&
jobCode.match(/^(?:DE|IT|HU|UK|XX)(?:AD|CO|CS|FA|HR|IT|LO|MD|MK|PM|PT|PU|QA|RD|SA)(\\d{4})(?:0|1).{3}$/)) {
var hayLevel = RegExp.$1;
switch (hayLevel) {
case "0515":
case "1617":
case "1821":
break;
default:
messages.push(jobCode + " is not a valid job code. Hay level " + hayLevel + " not allowed");
isValid = false;
break;
}
} else if (jobCode) {
messages.push(jobCode + " is not a valid job code.");
isValid = false;
}
if (isValid) {
return "X";
} else {
obj.set("ValidationResult", messages.join(";\\n"));
}