Alternate Method to DOM for this.document.getElementsByClassName

e__rajesh_badam
Mega Guru

 

Hi,

 

Am using below code to restrict file attachment type. The Portal and Non-Portal Code was working.

 

But as per ServiceNow suggestion it's not good to use DOM method. I did replacement with this line of code (this.g_form.getFormElementByClassName) for DOM in Portal script but it is not working.

 

So, please suggest alternatives to achieve my requirement.

 

Here is my code:-

function onSubmit() {
try
{
//for non-portal
if (window == null && g_form.getValue('request_type') == "new font request")
{
var sysid =g_form.getElement('sysparm_item_guid').value;

alert('sysid is  ' + sysid); // is this coming properly

var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
var rowcount = attachment.rows.length;
if(rowcount != 1)
{
if(rowcount==0)
alert("Attachment Required!!");
return false;
}
else if(rowcount == 1 && (attachment.content_type.indexOf(".woff") <= -1)|| (attachment.content_type.indexOf(".woff2") <= -1))
{
//alert(attachment.content_type);
alert("Attachment should be in only woff/woff2 format!");
return false;
}
else return true;
}
}
catch(e)

{
//for-portal
if (window == null && g_form.getValue('request_type') == "new font request")
{
    //if(this.g_form.getFormElementByClassName('get-attachment').length == 1)
if(this.document.getElementsByClassName('get-attachment').length ==1)
 {
    //var value = (this.g_form.getFormElementByClassName('get-attachment')[0].innerHTML).toLowerCase();
var value = (this.document.getElementsByClassName('get-attachment')[0].innerHTML).toLowerCase();
//alert(value.indexOf('.woff'));
if ((value.indexOf('.woff')>-1)||(value.indexOf('.woff2')>-1) )
{
return true;
}
else
{
alert ("Attachment should be in only woff/woff2 format!");
return false;
}
}
else //if(this.g_form.getFormElementByClassName('get-attachment').length <1)
if(this.document.getElementsByClassName('get-attachment').length <1)
{
alert( "Attachment Required!!");
return false;
}
}
}
}

 

1 ACCEPTED SOLUTION

@e__rajesh_badam 

if you want to restrict file type then better go with attachment variable type and use allowed_extension attributes to only allow particular file type

something like this

AnkurBawiskar_0-1743422160801.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

9 REPLIES 9

Yes @Ankur Bawiskar Did it this before and it was working. Anyway thanks for your quick response.

Nilesh Pol
Tera Guru

@e__rajesh_badam 

will suggest to try with following code which uses Attachment API.

if (g_form.getValue('request_type') === "new font request") {
var attachments = g_form.getAttachments();

if (!attachments || attachments.length === 0) {
alert("Attachment Required!!");
return false;
}


var validExtensions = ['woff', 'woff2'];
for (var i = 0; i < attachments.length; i++) {
var fileName = attachments[i].file_name.toLowerCase();
var fileExtension = fileName.split('.').pop();

if (!validExtensions.includes(fileExtension)) {
alert("Attachment should be in only woff/woff2 format!");
return false;
}
}

return true;
}

Thank you for suggestion @Nilesh Pol used suggested script but it is allowing attachment instead of showing alert message attachment required.

 

My Requirement is need to restrict the attachment (if other then woff/woff2) in End user Portal.

Nilesh Pol
Tera Guru

@e__rajesh_badam Since g_form.getAttachments() does not work in the Service Portal, you need to use the Attachment Validation Script in the onSubmit client script to enforce restrictions.

use the updated script:

function onSubmit() {
if (g_form.getValue('request_type') === "new font request") {
// Get attachments using spAttachment API
var attachments = window.top.CustomEvent ? window.top.CustomEvent.attachments : [];

if (!attachments || attachments.length === 0) {
alert("Attachment Required!!");
return false;
}var validExtensions = ['woff', 'woff2'];

for (var i = 0; i < attachments.length; i++) {
var fileName = attachments[i].name.toLowerCase();
var fileExtension = fileName.split('.').pop();

if (!validExtensions.includes(fileExtension)) {
alert("Attachment should be in only .woff/.woff2 format!");
return false;
}
}
}

return true;
}

Sorry @Nilesh Pol Still script is not restricting, request is getting submit even after if i wont attach files while submitting request.