Alternative for DOM Manipulation in client script

kavitha_cr
Mega Guru

Hi All,

I am using the below code in a Onsubmit catalog client script to show an alert for attaching a file before submitting the ticket in the portal.

Script:

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

According to Best Practice, we will not be using DOM manipulation , so I need to replace the code. Can I get the alternative solution for the line 3 in the above script by not breaking the existing functionality.

Thanks in Advance,

Shilpa.

1 ACCEPTED SOLUTION

Hi,

I haven't tried with this

let us know if that works well

var attachments = g_form.getFormElement('header_attachment_list_label');

Regards
Ankur

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

View solution in original post

20 REPLIES 20

Hi @Ankur Bawiskar ,

Ya, I have already tried and it is working with this line Ankur.

I thought of checking with you if u have tried earlier.

Thanks,

Shilpa

Hi,

then you should be good here.

Please mark my response as correct and close the question

Regards
Ankur

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

Hi @Ankur Bawiskar ,

yes Ankur, I have marked it.

I have one more question if u can answer..

which code can I replace the below bold code.(we should not use DOM)

function getParmVal(name){
var url = document.URL.parseQuery();
if(url[name]){
return decodeURI(url[name]);
}
else{
return;

Thanks,

Shilpa

 

Hi,

use top.location.href to get the URL

to get parameter value

var glideURL = new GlideURL();
glideURL.setFromCurrent();
var comp = glideURL.getParam("sysparm_Comp");

Regards
Ankur

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

Hi @Ankur Bawiskar ,

Below is the complete code, where can I modify exactly

 

function onLoad() {
//Populate the variables with the parameters passed in the URL
//Use the 'getParmVal' function below to get the parameter values from the URL
var sd = getParmVal('sysparm_short_descr');
var ds = getParmVal('sysparm_full_descr');


g_form.setValue('short_descr',sd);
g_form.setValue('full_descr',ds);


}

function getParmVal(name){
var url = document.URL.parseQuery();
if(url[name]){
return decodeURI(url[name]);
}
else{
return;
}
}

Thanks,

Shilpa.