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.

Custom button to show pop up to upload attachments in portal

Chenab Khanna
Tera Expert

Hi,

I have a requirement to create a button on portal on click of which 2 things should happen - 

1. A new record should be created in Document (dms_document) table, and current record should be updated with inserted record. (This is happening)

2. On click of the button, a pop up window should be displayed to upload attachment and the attachment should get attached to current record. (Need help with this)

Script - 

HTML Template - 

<div style="float:right; margin-bottom:10px;">  
  <button type="button" ng-click="clickUpload()" ng-if="data.showBtnDoc=='yes'">Upload Document</button>
  
</div>

Server script -

(function() {
    /* populate the 'data' object */
    /* e.g., data.table = $sp.getValue('table'); */


    var id = $sp.getParameter('sys_id');

    var getVal = new GlideRecord('x_eyit2_lcm_tool_fog_fof');
    getVal.get(id);

    var docVal = getVal.document;

    if (docVal == '') {
        data.showBtnDoc = 'yes';
    } else {
        data.showBtnDoc = 'no';
    }

    if (input && input.checkRec == 'yes') {
        var name = getVal.name;
        var num = getVal.number;
        var doc = new GlideRecord('dms_document');
        doc.initialize();
        doc.name = num + '-' + name;
        var sysID = doc.insert();
        data.new_id = sysID;


        getVal.document = sysID;
        getVal.update();
    }
})();

Client Controller - 

api.controller=function($scope) {
  /* widget controller */
  var c = this;
    $scope.clickUpload = function() {
        
        c.data.checkRec = 'yes';
        c.server.update().then(function(response) {
            //spUtil.update($scope);    
           c.data.checkRec = '';
            
        });
        location.reload(true);
    };

    
};

1 REPLY 1

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

there is an add attachment widget created by @HV here: github-link. It originated from this community post.

Generally speaking uploading an attachment with a custom action is not as easy as it may seem. However, there is an API (which is also used in the widget i linked) that allows you to upload an attachment to any table/record you may need.

Regards

Fabian