0

Table Name: Task

Record Producer Script:

current.setAbortAction(true); //Prevent normal function of this record producer.
 //Initialize "global constants"
var serviceNowPlatformTeam = "fd6ba5e81bd555"; //You can provide your grp sys_id
 var defect = "1";
var incident = "3";
var enhancement = "4";
var defect_table = "rm_defect";
var enhancement_table = "rm_enhancement";
var incident_table = "incident";
var option = producer.how_can_we_help;
 (function() {
     var portal_redirect = "sp?id=ticket&table=";
    var record_sys_id = "";
     switch (true) {
        case option == defect:
            record_sys_id = createRecord(defect_table);
            portal_redirect += defect_table;
            break;
        case option == incident:
            record_sys_id = createRecord(incident_table);
            portal_redirect += incident_table;
            break;
        case option == enhancement:
            record_sys_id = createRecord(enhancement_table);
            portal_redirect += enhancement_table;
            break;
        default:
            break;
    }
     if (record_sys_id) {
        portal_redirect += "&sys_id=" + record_sys_id;
        producer.portal_redirect = portal_redirect;
    }
})();
 /**
 * createRecord - creates a record on the supplied table.
 * @param tableName
 */
function createRecord(tableName) {
    var new_record = new GlideRecord(tableName);
    new_record.initialize();
    new_record.urgency = producer.what_issue;
    new_record.impact = producer.impact_of_this_issue;
    new_record.short_description = producer.short_description;
    if (option == defect) {
        new_record.description = "Details: " + producer.please_provide_details + "\n";
        new_record.description += "When: " + producer.issue_happening;
    } else {
        new_record.description = producer.description;
    }
     if (option != incident) { //Incidents do not have a "product" field.
        new_record.product = serviceNowProduct;
    } else { //They do, however, take other parameters
        new_record.caller_id = gs.getUserID(); // caller_id (Contact)
        new_record.contact_type = "Self-Service"; //contact_type (Channel)
        new_record.category = "inquiry"; // category
    }
    new_record.assignment_group = serviceNowPlatformTeam;
    var new_record_sys_id = new_record.insert();
 var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_sys_id", current.sys_id);
attachment.addQuery('table_name', 'task');
attachment.query();
while (attachment.next()){
attachment.table_sys_id = new_record_sys_id;
attachment.table_name = tableName;
attachment.update();
}
//OR //GlideSysAttachment.copy('task', current.sys_id, 'tableName',new_record_sys_id);
     if (!new_record_sys_id) {
        gs.error("sc_cat_item_producer - ServiceNow Platform Team Contact Form: Error Creating Record - Provided Values: " + JSON.stringify(producer));
    } else {
        return new_record_sys_id;
    }
 }
Create target record in Multiple table via single Record producer
Working Code Edited question August 22, 2023