Allows only excel files in attachment on Service Portal

piyushkumar
Kilo Contributor

Hi All,

I am trying to write a catalog client script, UI script to restrict the user to attach only excel files via the portal. My code is only working for checking the attachment. I want you to help me to complete the code to put a restriction for file extensions.

Don't want to use system property it will impact globally, also the BR will not help. ALSO GEL method will not support

Here my code below - 

Record Producer

Name: TestAttachment

Table: Change_Request

Variable: No Variable (No field is needed)

Only attachment icon and Submit button

1- User can only attach excel files

UI Script:

Name: sp_form (added in Themes under related list JS Includes)

var sp_form = {
getAttachments: function() {
var i, attachmentElement;
var catalogAttachments = [];
// var attachmentElements = angular.element("#sc_cat_item_producer").scope().attachments;
var attachmentElements = angular.element("#sc_cat_item").scope().attachments;
alert('ui script-'+attachmentElements);
for (i = 0; i < attachmentElements.length; i++) {
attachmentElement = attachmentElements[i];
catalogAttachments.push(

new this._CatalogAttachment(
attachmentElement.file_name,
attachmentElement.ext,
attachmentElement.sys_id,
attachmentElement.size
));
}
return catalogAttachments;
},
_CatalogAttachment: function(file_name, file_extension, sysID, file_size) {
this.file_name = file_name;
this.file_extension = file_extension;
this.sysID = sysID;
this.file_size = file_size;
}
};

 

Catalog Client Script

Name: Attachment Check

Applies to Catalog Item

function onSubmit() {
var hasCorrectAttachments = true;
var scAttachments=sp_form.getAttachments();
alert('SC'+scAttachments);
var REQUIREDATTACHMENTTYPE = 'xlsx'; // excel File type
var REQUIREDATTACHMENTTYPE_1 = 'xls';
for (var i = 0; i < scAttachments.length; i++) {

if ((scAttachments[i].file_extension !== REQUIREDATTACHMENTTYPE)&&(scAttachments[i].file_extension !== REQUIREDATTACHMENTTYPE_1)) {
hasCorrectAttachments = false;
break;
}
}
if (!hasCorrectAttachments) {
g_form.addErrorMessage('Please attach the Attachment in Excel Format only.');
return false;
}
}

 

 

 

 

Also Tried below UI Script- 

function getSCAttachmentCount() {
var length;
try {
length = angular.element("#sc_cat_item").scope().attachments.length;
} catch(e)
{
length = -1;
}
return length;
}

Catalog Client Script

function onSubmit() {
try { //Works in non-portal ui
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' )
{
alert('You must attach the completed form before submitting this request.');
return false;
}
} catch(e) { //For Service Portal
var count = getSCAttachmentCount();
if(count <= 0) {
alert('You must attach the completed form before submitting this request.');
return false;
} } }

 

Any suggestions please help here 🙂

 

Regards,

Piyush Kumar

5 REPLIES 5

Prasad Pagar
Mega Sage

Hi Piyush,

Are you getting any specific error?

Is your script getting trigger?

 

Thank you
Prasad