var importedDataTableName = "x_221138_scooby_do_scooby_do_import_data"; // this is the import set table name - update this to match the name of the table where your data was imported var scoobyDoRecordTable = 'x_221138_scooby_do_investigative_case'; // change this to the name of the table you created var scoobyDoDataMigrationTrackerTable = 'x_221138_scooby_do_scooby_do_data_migration_tracker'; // change this to the name of the tracker table you created var numberOfRecordsToGet = 30; // get records from the data migration tracker table so we can process them to create Scooby Do records var dataMigrationTrackerRecords = getAvailableTrackerRecords('u_investigative_case_completed', numberOfRecordsToGet, scoobyDoDataMigrationTrackerTable); var count = 1; while (dataMigrationTrackerRecords.next() ) { var importedDataSysID = dataMigrationTrackerRecords.getValue('u_imported_data_sys_id'); var importedDataNumber = dataMigrationTrackerRecords.getValue('u_imported_data_number'); if (count == 4) { // this is an intentional bug (how often does one get to have an intentional bug? :D ) gs.info(thisIsAnIntentionalBug); } // now get a record from the import table var importedDataRow = getRowFromImportedData(importedDataSysID, importedDataTableName); var resultsMessage = "Dude, we didn't find an imported record with this sys_id [" + importedDataSysID + "]"; if (importedDataRow) { resultsMessage = "Zoinks--we found an imported record with this sys_id [" + importedDataSysID + "], but we had trouble creating a Scooby Do Investigative Record."; // now create a scooby do record var scoobyDoRecordSysID = createScoobyDoRecord(importedDataRow, scoobyDoRecordTable); if (scoobyDoRecordSysID !== null) { resultsMessage = "Well totally awesome Shaggy, we added an investigative case from the import record with this sys_id [" + importedDataSysID + "]"; // now update the data migration tracker // >>> set the status field to true dataMigrationTrackerRecords.setValue('u_investigative_case_completed', 'true'); // >>> update the reference field with the sys_id of the created record dataMigrationTrackerRecords.setValue('u_investigative_case', scoobyDoRecordSysID); dataMigrationTrackerRecords.update(); } } count++; gs.info("ScoobyDo-Create Cases from Data Migration Trackers >>> " + resultsMessage); } // end > while (dataMigrationTrackerRecords.next() ) /* ************************ FUNCTIONS ************************* */ // NOTE: This function would normally be in a script include function getAvailableTrackerRecords(migrationTrackerStatusField, numberOfRecordsToGet, scoobyDoDataMigrationTrackerTable){ /* In this function we pass in the migrationTrackerStatusField we're getting along with the numberOfRecordsToGet. We use the migrationTrackerStatusField to build an encodedQuery that will be used to get the applicable data migration tracker record types */ if (typeof migrationTrackerStatusField == undefined) { // no migrationTrackerStatusField value; bug out man. return false; } var encodedQuery = migrationTrackerStatusField + "=false"; // only get records where the tracker status field is false; why? because this indicates that row has not been processed yet switch (migrationTrackerStatusField){ // don't need a case statement for person because it's the first record we'll create case "u_investigative_case_completed": encodedQuery += "^u_imported_data_sys_idISNOTEMPTY"; // ensure that we have a sys_id from the import set table otherwise we have no way to get the record from the imported data break; // continue to use case statements if there are other imported data files that are being migrated } if (typeof numberOfRecordsToGet == undefined || isNaN(numberOfRecordsToGet)){ numberOfRecordsToGet = 5; } var availableTrackerRecords = new GlideRecord(scoobyDoDataMigrationTrackerTable); availableTrackerRecords.addEncodedQuery(encodedQuery); availableTrackerRecords.orderByDesc('u_imported_data_sys_id'); availableTrackerRecords.setLimit(numberOfRecordsToGet); availableTrackerRecords.query(); return availableTrackerRecords; } // NOTE: This function would normally be in a script include function getRowFromImportedData(importedDataSysID, importTableName) { var importedDataRow = new GlideRecord(importTableName); var encodedQuery = "sys_id=" + importedDataSysID; importedDataRow.addEncodedQuery(encodedQuery); importedDataRow.query(); if (importedDataRow.next()){ return importedDataRow; } else { return false; } } // NOTE: This function would normally be in a script include function createScoobyDoRecord(importedDataRow, scoobyDoRecordTable){ // Here's a little bit of data mapping folks. var importedDataSysID = importedDataRow.getValue('sys_id'); var importedDataCaseNotes = importedDataRow.getValue('u_case_notes'); gs.info("createScoobyDoRecord >> importedDataCaseNotes: " + importedDataCaseNotes); var importedDataCaseNumber = importedDataRow.getValue('u_case_number'); // let's build a work note so we know from which imported data row we created the Scooby Do Investigative Case var workNote = "This Scooby Do Investigative Case was created from the imported Record [" + importedDataCaseNumber + "] which had a sys_id of: " + importedDataSysID; if (importedDataCaseNotes != null) { workNote += "\n\n" + importedDataCaseNotes; } var importedDataDescription = importedDataRow.getValue('u_description'); var importedDataTitle= importedDataRow.getValue('u_title'); var importedDataStatus = importedDataRow.getValue('u_status'); var status2StateMap = { 'New' : '1', 'WIP' : '2', 'Done' : '3' }; var scoobyDoRecord = new GlideRecord(scoobyDoRecordTable); // Let's set the values for the new Scooby Do Record we will create, shall we? scoobyDoRecord.work_notes = workNote; // work notes can't use the .setValue() call scoobyDoRecord.setValue('short_description', importedDataTitle); scoobyDoRecord.setValue('description', importedDataDescription); scoobyDoRecord.setValue('state', status2StateMap[importedDataStatus]); var scoobyDoRecordSysID = scoobyDoRecord.insert(); return scoobyDoRecordSysID; }