Need to populate custom table field with RITM number

Sharath807
Tera Contributor

Hi friends, iam working on a task in which i attach an excel sheet containing records in a catalog item , when i sumbit that catalog item ,records from excel gets populated in custom table. This is done by copying attachement from sc_req_item to datasource followed by transform map  its working fine. Now the issue is the custom table contains one field called Ritm number, the field should be populated with the ritm number of reqseted item when ever records is created using the catalog item. How can i achieve this. help me

17 REPLIES 17

@Slava Savitsky  any update on this task.

Sharath807
Tera Contributor

@Slava Savitsky Hi am using business rule script:

(function executeRule(current, previous /*null when async*/) {
//     var attachment = new GlideSysAttachment();
//  var attached = attachment.copy('sc_req_item', current.sys_id, 'sys_data_source', '0dbd1dcd97f6ce10c37bba86f053af9e');
//  if (attached) {
//  var schImp_GR = new GlideRecord("scheduled_import_set");
//  if(schImp_GR.get( 'c9772901977ace10c37bba86f053af5a')){
   
//      gs.executeNow(schImp_GR);

//  }
//  }
// })(current, previous);

The copy() method returns sys_ids of the original attachments and their copies. If you track this somewhere (e.g. in a custom table with three columns: ritm_id, attachment_original_id, attachment_copy_id), you would be able to query that table later from a transform map script and find the original RITM by the id of the attachment, attached to the Data Source record.

 

However, there are some general problems with your solution design that you need to be aware of:

 

1. With every new RITM, the number of attachments in your Data Source record will grow. You need a mechanism to remove old attachments. This can be done with an onComplete transform map script.

 

2. If another RITM of this kind is submitted before the previous import has completed, it won't work because the new attachment will be deleted together with the previous one when the import is over. So you probably need to disallow submission of new RITMs of this type until processing of the previous one is completed. This can be achieved via a GlideAjax call from an onSubmit client script in your catalog item.

@Slava Savitsky Hi can you provide sample script regarding fetching those details to custom table.
To delete the attachments i have post import script in scheduled import.

var attachmentRec = new GlideRecord('sys_attachment');
attachmentRec.addQuery('table_sys_id', '0dbd1dcd97f6ce10c37bba86f053af9e');
attachmentRec.query();
if (attachmentRec.next()) {
    attachmentRec.deleteRecord();
}

@Slava Savitsky  what about this script:

(function executeRule(current, previous /*null when async*/) {
    // Initialize attachment utility
    var attachment = new GlideSysAttachment();
    // Get the list of attachments for the current record
    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_sys_id', current.sys_id);
    attachmentGR.addQuery('table_name', 'sc_req_item');
    attachmentGR.query();

    while (attachmentGR.next()) {
        // Rename the attachment to include the RITM sys_id
        var newFileName = current.sys_id + '_' + attachmentGR.file_name;
        attachment.rename(current.sys_id, attachmentGR.file_name, newFileName);

        // Copy the renamed attachment to the data source
        var attached = attachment.copy('sc_req_item', current.sys_id, 'sys_data_source', '0dbd1dcd97f6ce10c37bba86f053af9e');
       
        if (attached) {
            var schImp_GR = new GlideRecord("scheduled_import_set");
            if (schImp_GR.get('c9772901977ace10c37bba86f053af5a')) {
                gs.executeNow(schImp_GR);

                // Adding a delay to ensure the import process is completed
               // gs.sleep(5000); // Wait for 5 seconds
            }
        }
    }
})(current, previous);