'IF' Statement in Workflow Editor Cannot Find Function in Object

appstorm
Tera Contributor

I have a reference field call student_information that points to the sn_uni_req_larf_banner_stu table.  It's display value is the C# field that is dot-walked from the sys_user table.

appstorm_0-1742407291443.png

 

Since C# (employee_number) on the sys_user table is the same as C# (u_cnumber) on the sn_uni_req_larf_banner_stu table, the idea is when a match is found between the two to return the value (T/ F) for Hold IND (u_hold_ind), via an 'IF' Statement in Workflow Editor.

appstorm_2-1742407641582.png

 

// 1. Get the student_information reference field (which references sn_uni_req_larf_banner_stu)
var studentInfo = current.student_information;

if (studentInfo) {
    // Log to verify the student information reference is valid
    gs.info('Student information found, u_cnumber: ' + studentInfo.u_cnumber);

    // 2. Access the u_cnumber and u_hold_ind fields directly from the sn_uni_req_larf_banner_stu record
    var studentCnumber = studentInfo.u_cnumber; // Directly accessing u_cnumber from sn_uni_req_larf_banner_stu
    var holdInd = studentInfo.u_hold_ind; // Directly accessing u_hold_ind from sn_uni_req_larf_banner_stu

    // 3. Log to check the values
    gs.info('Found u_hold_ind: ' + holdInd + ' for u_cnumber: ' + studentCnumber);

    // 4. Route based on the value of u_hold_ind
    if (holdInd == 'T') {
        gs.info('Student has a hold (Yes). Routing to Yes.');

        // Use the transition defined in the workflow to go to the "Yes" path
        current.setTransition('yes_transition'); // Replace with your actual transition name from the workflow
    } else if (holdInd == 'F') {
        gs.info('Student does not have a hold (No). Routing to No.');

        // Use the transition defined in the workflow to go to the "No" path
        current.setTransition('no_transition'); // Replace with your actual transition name from the workflow
    } else {
        // Handle unexpected values for u_hold_ind
        gs.info('Unexpected value for u_hold_ind: ' + holdInd);

        // If the value is unexpected, route to the failure path
        current.setTransition('failure_outcome'); // Replace with your actual failure outcome name from the workflow
    }

} else {
    // Log if student_information is not set or valid
    gs.info('student_information is not set or valid.');

    // Route to the failure path if student_information is missing
    current.setTransition('failure_outcome'); // Replace with your actual failure outcome name from the workflow
}

 

But so far, the only thing I receive is an error, "Student Hold(5d89e0873b102650f467b18a25e45a20): org.mozilla.javascript.WrappedException: Wrapped TypeError: Cannot find function suspend in object [object com.glideapp.workflow.queue.activity.Workflow]."

appstorm_3-1742407776647.png

 

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

You should start with the basic structure that is presented in a new If activity, making sure to return 'yes' or return 'no'.  You can add your function to the ifScript function, then troubleshoot if there is a syntax or logic error within that.

// This script needs to set answer to 'yes' or 'no' to indicate the state of the activity.
//
// For example,
//
//   answer = ifScript();
//
//   function ifScript() {
//      if (condition is true) {
//         return 'yes';
//      }
//      return 'no';
//   }

Do not use current.setTransition.  If you need more than the two results, use a Run Script activity instead, setting an activity.result value for each condition, then replace the Always condition with one matching each scripted result.

Thank you!  I made the changes you suggested (see script, below) but still get this Error: "Student Hold(86225d8b3bd02650f467b18a25e45a34): org.mozilla.javascript.WrappedException: Wrapped SyntaxError: invalid return (// 1. Get the student_information reference field (which references sn_uni_req_larf_banner_stu)"

 

// Access the student_information reference field
var studentInfo = current.student_information;

// Check if student_information is valid
if (studentInfo) {
    // Log the student information reference
    gs.info('Student information reference found: ' + studentInfo);

    // Query the sn_uni_req_larf_banner_stu table using student_information (the reference field)
    var gr = new GlideRecord('sn_uni_req_larf_banner_stu');
    if (gr.get(studentInfo)) {  // Get the record from the sn_uni_req_larf_banner_stu table
        // Log to verify we have found the record
        gs.info('Found sn_uni_req_larf_banner_stu record for u_cnumber: ' + gr.u_cnumber);

        // Access the u_guest_ind field directly from the sn_uni_req_larf_banner_stu record
        var guestInd = gr.u_guest_ind;

        // Declare a flag to control flow
        var continueFlow = false;

        // Check if the u_guest_ind field value is 'T' (guest)
        if (guestInd == 'T') {
            // Student is a guest, continue the flow
            continueFlow = true;
            gs.info('Student is a guest, continuing the workflow.');
        } else {
            // Student is not a guest, stop the flow
            gs.info('Student is not a guest, stopping the workflow.');
        }

        // Set the result for the activity (this will be used by the workflow)
        if (continueFlow) {
            activity.result = 'yes';  // Set the result to 'yes' if the flow continues
        } else {
            activity.result = 'no';   // Set the result to 'no' if the flow does not continue
        }

    } else {
        // Log if the sn_uni_req_larf_banner_stu record was not found
        gs.info('No matching sn_uni_req_larf_banner_stu record found for student information.');
        activity.result = 'no';  // Default to 'no' if the student information record doesn't exist
    }
} else {
    // Log if student_information is not set or valid
    gs.info('student_information is not set or valid.');
    activity.result = 'no';   // Default to 'no' if student information is missing or invalid
}

student_information is a reference field to the sn_uni_req_larf_banner_stu table, but its display value comes from the sys_user table.  It is populated with a displayValue, but returns this Error: "student_information is not set or valid."

 

 

After further research, I've discovered the value for student_information isn't being passed to the RITM.  I'm guessing this has something to do with the way it is being auto-populated?

 

Regardless, I am attempting to populate that side with an after insert/ update business rule

(function executeRule(current, previous /*null when async*/) {
    // Ensure student_information is a valid sys_id reference and not just the display value
    if (current.request_item.student_information) {
        // Transfer the sys_id of the student_information field to the RITM record
        current.student_information = current.request_item.student_information;
        
        // Update the RITM record
        current.update();
        
        gs.info('Transferred student_information (sys_id) to RITM: ' + current.student_information);
    } else {
        gs.info('student_information not found in the catalog item for RITM: ' + current.sys_id);
    }
})(current, previous);