Workflow Script Not Populating Reference Field on RITIM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 05:41 AM
In Workflow Editor, I am using a script that performs the following key functions:
// Construct the JSON object dynamically
var courseInfo = {
"CRN": crn,
"TERM_CODE": termCode,
"TERM_DESC": termDesc,
"PTRM_CODE": ptrmCode,
"SUBJ_CODE": subjCode,
"CRSE_NUMB": crseNumb,
"SEQ_NUMB": seqNumb,
"CAMP_CODE": campCode,
"EMPLOYEE_NUMBER": employeeNumber // Replacing "INSTRUCTOR" with "EMPLOYEE_NUMBER"
};
// Stringify the JSON object to simulate the value in the course_information field
var jsonString = JSON.stringify(courseInfo);
// Log the stringified JSON for debugging purposes
gs.info('Stringified JSON: ' + jsonString);
// Set the stringified JSON to the course_information field (to simulate the field being populated)
current.course_information = jsonString;
// Now, we will process the course_information field to extract the EMPLOYEE_NUMBER
var courseInformation = current.course_information; // Get the value from the course_information field
var employeeNumberExtracted = ''; // Variable to store extracted employee number
// Step 1: Check if the course_information is null, empty, or undefined
if (!courseInformation || courseInformation === 'null' || courseInformation === 'undefined' || courseInformation === '') {
gs.error('course_information is empty, null, or undefined. Cannot proceed with processing.');
current.setAbortAction(true); // Abort further actions
return;
}
// Step 2: Parse the JSON string from the course_information field
try {
var courseInfoParsed = JSON.parse(courseInformation); // Parse the JSON string
if (courseInfoParsed && courseInfoParsed.EMPLOYEE_NUMBER) {
employeeNumberExtracted = courseInfoParsed.EMPLOYEE_NUMBER;
} else {
gs.error('EMPLOYEE_NUMBER not found in course_information.');
current.setAbortAction(true); // Abort further actions if EMPLOYEE_NUMBER is missing
return;
}
} catch (e) {
gs.error('Error parsing course_information JSON: ' + e.message);
current.setAbortAction(true); // Abort further actions if JSON is invalid
return;
}
// Step 3: Query the sys_user table to find the user based on the employee_number
var userGr = new GlideRecord('sys_user');
userGr.addQuery('employee_number', employeeNumberExtracted);
userGr.query();
// Step 4: Check if a matching user is found
if (userGr.next()) {
// Set the instructor field with the sys_id of the found user
current.instructor = userGr.sys_id;
// Log for debugging purposes
gs.info('Instructor sys_id set: ' + current.instructor);
// Step 5: Save the changes to the catalog item (RITM)
current.update(); // This ensures the reference field gets saved
gs.info('Instructor field updated successfully.');
} else {
gs.error('No user found with employee_number: ' + employeeNumberExtracted);
current.setAbortAction(true); // Abort further actions if no user is found
}
// --- Optional: Update Instructor on the Request Record (sc_request) ---
// This assumes you want to set the instructor on the Request record as well
var req = current.request.getRefRecord(); // Get the parent Request record
// Ensure the name variable exists and then set the instructor on the Request
if (current.variables.name) {
req.instructor = current.variables.name; // Set the instructor based on the catalog variable
req.update(); // Save the changes to the Request record
gs.info('Instructor field updated on the Request record successfully.');
} else {
gs.error('No name variable found on the catalog item.');
}
Data Collection and Processing:
- The script begins by defining dynamic values for course-related fields and constructs a JSON object.
- This JSON object is stringified and then assigned to the course_information field on the current RITM record.
Error Checking:
- The script checks if the course_information field is empty or invalid, and aborts the script execution if so.
- It also ensures that the EMPLOYEE_NUMBER exists in the parsed JSON, aborting if not.
Extracting and Using Employee Number:
- The EMPLOYEE_NUMBER is extracted from the parsed JSON, and a query is made to the sys_user table to find the user with that employee number.
- If the user is found, the instructor field on the RITM is updated with the user's sys_id.
Optional Update to Request Record:
- If the name catalog variable exists, the script also updates the instructor field on the parent Request record (sc_request).
Error Handling and Logging:
- Throughout the script, error messages are logged via gs.error() if issues are encountered (e.g., missing data, parsing errors, no matching user).
- Successful operations are logged using gs.info() for debugging purposes.
After all this, the script executes as needed. The sys_id of the instructor is located from the sys_user table, based-on employee_number and returns a display name. However, the instructor field - which is a reference field that looks at the sys_user table on the catalog item, does not populate on the record. It's crucial we populate this field after the ritm is submitted, because other workflow fields (i.e. approval) are dependent on its value.
Any help is appreciated!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 05:53 AM
If you are setting a reference field, you need to set it to the sys_id, not the display name. If you are wanting to use display name, you'll have to use setDisplayValue instead of assigning it a value.