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

e__rajesh_badam
Mega Guru

@Ankur Bawiskar I saw one of your post

https://www.servicenow.com/community/itsm-forum/alternative-for-dom-manipulation-in-client-script/m-... and i used same method but it is allowing attachments without warning. 

 

Like //var value = (this.g_form.getFormElementByClassName('get-attachment')[0].innerHTML).toLowerCase();

 

Is my way of declaration is correct, please suggest.

@e__rajesh_badam 

You can use that script if required.

DOM is not recommended but we could find lot of OOB scripts using that.

Instead of that why not use attachment variable type and make it mandatory?

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

Agreed with you @Ankur Bawiskar  but the script was designed long back. Client wana go with script.

So, looking for alternative for it.

@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