Issues with Transform Map script

davilu
Mega Sage

Hello Experts, our team is having an issue with transform map executing correctly.  We currently have a table that houses course information and we need to add in a new column for Course ID.  There is a separate excel that has the Course ID information that we loaded into the system in a staging table and we want to populate that column by matching Course Number.  

We have an onBefore script in place to skip over any new records:

 

On Before:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
 
    if (action == "insert") {
        ignore = true;
    }
 
})(source, map, log, target);

 

 

We also have an onAfter script that matches records by Course Number and then populates the corresponding Course ID:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
 
    // Add your code here
   
 
    // Handle updates
    if (action == "update") {
        // Get the Course Number from the source table
        var courseNumber = source.getValue('u_course_code'); // Adjust the field name as necessary
 
        // Initialize a GlideRecord object for the target table
        var gr = new GlideRecord('learning_and_development'); // Adjust the table name as necessary
 
        // Add a query to find all records with the same Course Number
        gr.addQuery('course_number', courseNumber); // Adjust the field name as necessary
        gr.query();
 
        // Check if any records exist with the given Course Number
        if (gr.hasNext()) {
            // Loop through all matching records
            while (gr.next()) {
                // Update the ID field for each matching record
                gr.setValue('course_id', source.getValue('u_course_id')); // Adjust the field names as necessary
                gr.update();
            }
        } else {
            // Log a message if the Course Number does not exist in the target table
            log.info('Course Number ' + courseNumber + ' does not exist in the target table. Skipping update.');
        }
    }
 
})(source, map, log, target);

There can be multiple records that share the same Course Number and ID so we did not coalesce on any column.  

The issue is when we run the transform map, nothing actually happens.  No Course ID gets populated.  However, when we set Course Number as coalesce and run, Course ID actually gets populated for the first record, but none of the other Course IDs that are shared get populated.

Any ideas why this is happening and how we can fix it?

1 REPLY 1

Elijah Aromola
Mega Sage

Are field names spelled correctly? Update your log statement to gs.info(message) and see if you get log statements.