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.

c.server.get not working in service portals

kamade rohini
Tera Contributor

I have a requirement where user should get informational message if he is trying to attach a file of more than 3MB on a portal.

 

I'm trying to get this functionality done over a widget itself.

below are my client controller and server script.

Client controller:

 function setAttachments(attachments, action) {
alert('action inside: '+action);
 
            c.data.action = 'getGUID';
            c.data.attID = $scope.data._attachmentGUID;
            var id = c.data.attID;
            
            alert("scoped: " + $scope.data._attachmentGUID);
            alert('c.data.attID: ' + c.data.attID);
           alert('size1: ' + $scope.data.size);
var inputs={
action: 'getGUID',
            attID: id
}
           c.server.get(inputs).then(function(response) {
                alert('size: ' + $scope.data.size);
  c.server.update();
            });
        }
        $scope.attachments = attachments;
 
    }



Server script:
data._attachmentGUID = gs.generateGUID();
if (input && input.action == 'getGUID') {
data.final_size=0;
  gs.log("input.attID: "+input.attID,'rohini');
var att = new GlideRecord('sys_attachment');
        att.orderBy('sys_created_on');
        att.addQuery('table_sys_id',input.attID);
        att.query();
        while(att.next()) {
           data.size = att.size_bytes;
gs.log("data.size " + data.size, "rohini");
gs.log("Count1: " + att.getRowCount(), "rohini");
        }
    }



Now, I'm able to get size of attached of file on a server side in logs. but at the same I'm unable to fetch it on client side.
1 REPLY 1

Sanjay191
Tera Sage
Tera Sage

Hi @kamade rohini 

 

can you please check with below line of code 

   c.server.get(inputs).then(function(response) {
                alert('size: ' + $scope.data.size);
  c.server.update();
            });
instead of this try to do this and check what you receive from the server side to client side in response and put some log there
please try to use this 

c.server.get(inputs).then(function(response) {

// Fetch the value returned by server
var fileSize = response.data.size;

if (fileSize > 3 * 1024 * 1024) { // >3MB
alert('File size exceeds 3MB. Please upload a smaller file.');
} else {
alert('File uploaded successfully. Size: ' + fileSize);
}
});


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You