From 072e48191ef94d8179d36173d1b91c25ab196b5f Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 12:39:52 +0530 Subject: [PATCH 1/3] Create script.js --- .../script.js | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/script.js diff --git a/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/script.js b/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/script.js new file mode 100644 index 0000000000..f4b86ddc49 --- /dev/null +++ b/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/script.js @@ -0,0 +1,46 @@ +/** + * Copies specified fields from a source record to a newly created target record, + * validating all fields before performing the insert operation. + * + * @param {string} sourceTable - The name of the source table. + * @param {string} sourceId - The sys_id of the source record. + * @param {string} targetTable - The name of the target table. + * @param {string[]} fields - An array of field names to copy. + * @returns {string|null} The sys_id of the newly created target record, or null on failure. + */ +function copyFieldsValidated(sourceTable, sourceId, targetTable, fields) { + var src = new GlideRecord(sourceTable); + if (!src.get(sourceId)) { + gs.error("Source record not found in " + sourceTable + " with sys_id: " + sourceId); + return null; + } + + var tgt = new GlideRecord(targetTable); + tgt.initialize(); + var allFieldsAreValid = true; + // First, validate all fields before doing any work + fields.forEach(function(f) { + if (!src.isValidField(f) || !tgt.isValidField(f)) { + gs.warn("Field [" + f + "] is not valid in both " + sourceTable + " and " + targetTable + ". Aborting insert."); + allFieldsAreValid = false; + } + }); + + // Proceed with copying and inserting only if all fields are valid + if (allFieldsAreValid) { + fields.forEach(function(f) { + tgt.setValue(f, src.getValue(f)); + }); + var newId = tgt.insert(); + if (newId) { + gs.info("Record copied from " + sourceTable + " → " + targetTable + ". New sys_id: " + newId); + return newId; + } else { + gs.error("Failed to insert new record into " + targetTable); + return null; + } + } else { + gs.error("Aborting record insert due to invalid fields."); + return null; + } +} From 2dd9b6c25c84f9a1c3d057a763a32072e83e0c5c Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 12:49:47 +0530 Subject: [PATCH 2/3] Create README.md --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md diff --git a/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md b/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md new file mode 100644 index 0000000000..1366452861 --- /dev/null +++ b/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md @@ -0,0 +1,23 @@ + +Script Usage : + +The function tables the parameters such as source table, source record sys_id, target table, fields that needs to be copied to target table. + +As a validation check, the fields from source table should be similar to target else abort inserting the record. + + +Same Code to invoke the function: +copyFieldsValidated( + 'dmn_demand', + '8c10306edbc00810f777526adc961976', + 'pm_project', + ['name', 'short_description'] //will throw error since name field not common in both tables +); + + +copyFieldsValidated( + 'dmn_demand', + '8c10306edbc00810f777526adc961976', + 'pm_project', + ['short_description'] //Insert the record since short_description is common in both tables +); From 0044770745266807893a4183280d7e968bf26da0 Mon Sep 17 00:00:00 2001 From: Naveen Kumar <103413520+naveensnow@users.noreply.github.com> Date: Fri, 3 Oct 2025 12:59:24 +0530 Subject: [PATCH 3/3] Update README.md --- .../Copy Field Values and Insert in Target Record/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md b/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md index 1366452861..030cebb637 100644 --- a/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md +++ b/Server-Side Components/Background Scripts/Copy Field Values and Insert in Target Record/README.md @@ -1,7 +1,7 @@ Script Usage : -The function tables the parameters such as source table, source record sys_id, target table, fields that needs to be copied to target table. +This function takes the parameters such as source table, source record sys_id, target table, fields that needs to be copied to target table. As a validation check, the fields from source table should be similar to target else abort inserting the record.