How to write the script for attached the file in workspace in popup window in sow workspace

reenak446674965
Tera Expert

I want  to write the  workspace client script ( pop up window ) for upload new version behavior it is just like the attachment functionalities as shown in screen shot in sow workspace . Could you please help me on this.

reenak446674965_0-1768565266266.png

 

11 REPLIES 11

vaishali231
Tera Guru

hey @reenak446674965 

 

In Workspace, we cannot use classic client scripts or GlideDialogWindow. To achieve Upload New Version behavior similar to the attachment popup, we need to use a Workspace UI Action with a client script and open a Now Modal.

Steps

Create a Workspace UI Action on the required table and set it as Client = true.

UI Action Client Script

function onClick() {
    var modal = new nowModal({
        title: 'Import a Word File',
        size: 'md'
    });

    modal.render(`
        <div style="padding:16px">
            <input type="file" id="ws_file" accept=".doc,.docx" />
            <br><br>
            <button class="btn btn-primary" onclick="uploadNewVersion()">Import</button>
        </div>
    `);

    window.uploadNewVersion = function () {
        var fileInput = document.getElementById('ws_file');

        if (!fileInput || !fileInput.files.length) {
            alert('Please select a file');
            return;
        }

        var file = fileInput.files[0];

        var attachmentHandler = new nowAttachmentHandler({
            tableName: g_form.getTableName(),
            tableSysId: g_form.getUniqueValue()
        });

        attachmentHandler.uploadAttachment(file).then(function () {
            modal.close();
            g_form.refresh();
        });
    };
}

 

*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.

Regards
Vaishali Singh

reenak446674965
Tera Expert

Hi @vaishali231 

I tried in PDI , It is not working 

@reenak446674965 

please share screenshot , add log in code show me error 

@vaishali231 

it would be nice if you could share screenshots for your solution

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

reenak446674965
Tera Expert

 I added the alert  in the code , only  first alert is working .


function onClick() {
    alert("test1");
      var modal = new nowModal({
        title: 'Import a Word File',
        size: 'md'
    });
alert("test2" +modal);
     modal.render(`
         <div style="padding:16px">
             <input type="file" id="ws_file" accept=".doc,.docx" />
             <br><br>
             <button class="btn btn-primary" onclick="uploadNewVersion()">Import</button>
         </div>
    `);

     window.uploadNewVersion = function () {
         var fileInput = document.getElementById('ws_file');
alert("test3 window function" +modal);
     if (!fileInput || !fileInput.files.length) {
             alert('Please select a file');
             return;
         }

         var file = fileInput.files[0];
alert("test4 window function" +modal);
         var attachmentHandler = new nowAttachmentHandler({
             tableName: g_form.getTableName(),
             tableSysId: g_form.getUniqueValue()
        });

        attachmentHandler.uploadAttachment(file).then(function () {
            alert("test5 window function" +modal);
             modal.close();
             g_form.refresh();
         });
     };
}