Link that allows users to download the files directly instead of opening them in a new tab

saketh poola
Tera Contributor

We have a requirement in a Record Producer form that includes a Select Box field. Based on the user's selection, a Rich Text Label should dynamically display a corresponding template—one template for a specific option, and a different template for another. Additionally, when the user clicks on any links within the Rich Text Label, the associated file should download directly rather than opening in a new browser tab.

How we can achieve this? 
I have tried with widget however it is working for only one template, if the form having multiple templates it won't work. - I am not looking for Widget as a solution.
Please help me!!

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@saketh poola 

this is possible with variable of this approach

1) store those 2 templates on catalog item record, grab the sys_attachment sysIds for both the files

2) then use onChange catalog client script on that select box

a) use g_form.addInfoMessage() and show the link to download the file

Something like this

Give correct sysId from sys_attachment, correct choice values to compare

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

    var link1 = '<a href="/sys_attachment.do?sys_id=sysId1" target="_blank">Click here to download Template 1</a>';
    var link2 = '<a href="/sys_attachment.do?sys_id=sysId2" target="_blank">Click here to download Template 2</a>';

    if (newValue == 'choiceValue1') {
        // Add the info message with the link
        g_form.addInfoMessage(link1);
    } else if (newValue == 'choiceValue2') {
        // Add the info message with the link
        g_form.addInfoMessage(link2);
    }

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 
Thank you for your response.
I wanted to check if this can be achieved within a Rich Text Label. I’ve tried using the HTML snippet in the Rich Text Label source code. However, my main concern is that when we move the record producer from sub-prod to prod, the attached files do not get transferred automatically. This means we have to manually re-upload the files in prod, retrieve the new sys_ids, and then update the links in the label or script, which is not ideal or scalable.

@saketh poola 

then do this, anyways since you want to use attachment this is the way I believe.

1) create 2 system property which holds sysId of sys_attachment

2) use GlideAjax and get that property value and fetch sysId

3) after moving to next instance, re-upload and get the 2 sysIds of sys_attachment and update the property value

4) no script change required

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

I can implement it that way; however, is there a way to achieve this directly within a Rich Text Label? The file is stored in SharePoint, and due to domain restrictions, I believe we can't use the SharePoint link in ServiceNow. Is it possible to replicate the same functionality using widgets? I’m able to do this with widgets when the form uses a single template, but I’m facing challenges when the form includes multiple templates.