Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to show a downloadable xl file link conditionally when user selects one of choice in a dropdown

Nitin21
Tera Contributor

How to show a downloadable xl file link conditionally when user selects one of the choice in a dropdown field in service portal?

1 ACCEPTED SOLUTION

@Nitin21 

Steps

1) add file to your catalog item, get the sysId of sys_attachment record sysId

2) create variable of type URL and add the relative URL there in default value, use the correct sysId from the above step in this

AnkurBawiskar_1-1763132593714.png

 

3) then write onChange on your other variable to show/hide the URL and another UI policy to make URL variable read-only always so that users don't change it

Output:

AnkurBawiskar_2-1763132677209.png

 

I hope this answers your question and you can enhance it further from here based on your developer skills and requirements

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

11 REPLIES 11

@Nitin21 

Steps

1) add file to your catalog item, get the sysId of sys_attachment record sysId

2) create variable of type URL and add the relative URL there in default value, use the correct sysId from the above step in this

AnkurBawiskar_1-1763132593714.png

 

3) then write onChange on your other variable to show/hide the URL and another UI policy to make URL variable read-only always so that users don't change it

Output:

AnkurBawiskar_2-1763132677209.png

 

I hope this answers your question and you can enhance it further from here based on your developer skills and requirements

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

I've used Rich Text instead of URL type but it works through client script one for hiding it on load and one for hiding and displaying on choice selection. Thank you!image.png

Nice to help you 

Thanks for the help!

MaxMixali
Kilo Sage

Puoi provare ad usare la configurazione del Catalog Item

  1. Crea le variabili:
    • Variable 1: Select Box
      • Name: document_type
      • Question: Seleziona documento
      • Type: Single Line Text o Select Box
      • Choices:
 
 
       doc1=Documento 1       doc2=Documento 2       doc3=Documento 3
  • Variable 2: Macro (per il link)
    • Name: download_link
    • Type: Macro
    • Macro: <div id="download_link_container"></div>
  1. Catalog Client Script (onChange):
 
 
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    // Nascondi il container se esiste
    g_form.setDisplay('download_link', false);
    
    // Mappa dei link
    var linkMap = {
        'doc1': {
            url: '/sys_attachment.do?sys_id=xxxxx',
            name: 'Documento_1.pdf'
        },
        'doc2': {
            url: '/sys_attachment.do?sys_id=yyyyy',
            name: 'Documento_2.xlsx'
        },
        'doc3': {
            url: 'https://example.com/file.zip',
            name: 'Documento_3.zip'
        }
    };
    
    if (linkMap[newValue]) {
        // Mostra il link
        g_form.setDisplay('download_link', true);
        
        // Crea l'HTML del link
        var linkHTML =             '<div class="alert alert-info" style="margin-top: 15px;">' +
            '<i class="fa fa-download"></i> ' +
            '<a href="' + linkMap[newValue].url + '" target="_blank" class="btn btn-primary">' +
            'Scarica ' + linkMap[newValue].name +
            '</a>' +
            '</div>';
        
        // Inserisci il link nel container
        setTimeout(function() {
            var container = document.getElementById('download_link_container');
            if (container) {
                container.innerHTML = linkHTML;
            }
        }, 100);
    }
}