How to download different files on seletion of each choice value in record producer variable

kalp
Tera Contributor

Hi, 

I have requirement where in a record producer, a choice field needs to be created with around 7 dropdown values. So on selection of the choice value on the record producer form, each file associated with the choice value should get downloaded. How can I achieve this. Please provide solution or any suggestion. It would be really helpful. 

 

Thanks in advance, 

Kalyani

1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

Hello @kalp 
It's pretty straight forward. 

1) Add the attachments to your catalog item.

2) Store the sys_id's and use them in below code:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == 'a') {
        top.window.open('https://dev258416.service-now.com/sys_attachment.do?sys_id=222d9a35837c1210b31f9cb6feaad3fb'); //replace with your actual sys_id's
    } else if (newValue == 'b') {
        top.window.open('https://dev258416.service-now.com/sys_attachment.do?sys_id=70acd2f1837c1210b31f9cb6feaad31e'); //replace with your actual sys_id's
    }
    //continue the remaining code
}

Attach the documents like below:

MurthyCh_0-1727073926350.png

DownloadAttachment.gif

Hope it helps:)

View solution in original post

3 REPLIES 3

Akash4
Kilo Sage

Hi Kalyani, 

This is indeed a heavy customization, although using Client Script and Script Include you can achieve.

1. Considering your choice variable as "file_download" with choices "file1"...

2. Choose the right location for file storage, you may attach on choices themselves or using a new custom table - in both cases they will be saved in sys_attachment table

3. Create Client Script in 3 steps- getValue(file_download) with Script Include name in GlideAjax, get Reponse to download

function onSubmit() {
   var selectedFile = g_form.getValue('file_download');
   var ga = new GlideAjax('FileDownloadScript');
   ga.addParam('sysparm_name', 'getFileURL');
   ga.addParam('sysparm_choice', selectedFile);  // client to server data
   ga.getXMLAnswer(function(response) {
       var fileURL = response;
       if (fileURL) {
           window.location.href = fileURL;
       } 
   });
}

4. Create Script Include say FileDownloadScript, 

var FileDownloadScript = Class.create();
FileDownloadScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   getFileURL: function() {
      var selectedChoice = this.getParameter('sysparm_choice');
      var fileRecord = new GlideRecord('u_file_download_table'); // if using custom table as storage of files
      fileRecord.addQuery('u_file_name', selectedChoice);  // Match the file by choice value **important
      fileRecord.query();
      if (fileRecord.next()) {
         var attachmentGR = new GlideRecord('sys_attachment');
         attachmentGR.addQuery('table_sys_id', fileRecord.sys_id);
         attachmentGR.query();
         if (attachmentGR.next()) {
            var attachmentSysID = attachmentGR.sys_id;
            var fileURL = gs.getProperty('glide.servlet.uri') + 'sys_attachment.do?sys_id=' + attachmentSysID;
            return fileURL;
         }
      }
      return ''; //else case
   }
});

Try these out and adjust as required. You can message for any errors. Happy learning!

kalp
Tera Contributor

Hi Akash, 

Thank you for providing the above code .

I need your suggestion on below changes to proceed, could you please tell

1. How to write code , to fetch attachment sys_id when custom table is not used.//script include

2.In place of onsubmit can we use onchange script , to download files of selecting choice value.

 

Thanks & Regards,

Kalyani

 

Murthy Ch
Giga Sage

Hello @kalp 
It's pretty straight forward. 

1) Add the attachments to your catalog item.

2) Store the sys_id's and use them in below code:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == 'a') {
        top.window.open('https://dev258416.service-now.com/sys_attachment.do?sys_id=222d9a35837c1210b31f9cb6feaad3fb'); //replace with your actual sys_id's
    } else if (newValue == 'b') {
        top.window.open('https://dev258416.service-now.com/sys_attachment.do?sys_id=70acd2f1837c1210b31f9cb6feaad31e'); //replace with your actual sys_id's
    }
    //continue the remaining code
}

Attach the documents like below:

MurthyCh_0-1727073926350.png

DownloadAttachment.gif

Hope it helps:)