Error showing on service portal while Validating attachment dependent on variable

rishabh31
Mega Sage

An error (image named 'error on sp' attached) is displaying on the service portal while submitting the request for validating attachment dependent on variable value (if the variable value is yes, then pop up a message - 'attach a doc.', & if no, then successfully request submitted with req. no generate).

This successfully works on trying it of that catalog item but this is not working on service portal it shows error 'There is a JavaScript error in your browser console' (image attached), after checking in console (image attached) regarding error its written 'while running Client Script "setattachment": ReferenceError: getSCAttachmentCount is not defined'

I wrote the below script as a catalog client script with UI type- 'All', Isolate Script= 'false' (when I make isolate script=true, then it shows an error during submitting for both in catalog item as well as an in-service portal, but when it sets false then it only works on catalog item but not in sp)

function onSubmit() {
//Type appropriate comment here, and begin script below
if (g_form.getValue('is_attachment_reqd') == "Yes") { //update field values
try { //Works in non-portal ui
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
g_form.addErrorMessage('plz attach doc.');
return false;
}
} catch (e) { //For Service Portal
var count = getSCAttachmentCount();
if (count <= 0) {
g_form.addErrorMessage('plz attach doc.');
return false;
}
}
}
}

My need is when someone selects 'Yes' in catalog item variable 'is_attachment_reqd' & without attachment, if we try to submit the request then it shows error message 'plz attach doc.' & attachment becomes mandatory & when selects 'No' then it easily take request submission.

Kindly help me to fix the issue so that it can run on both UI types successfully. Thanks in advance

1 ACCEPTED SOLUTION

then check my blog and use that script.

it works in both native + portal

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

did you follow all the steps for that UI script?

check below link for the steps

Require attachment for catalog item in Platform and Service Portal

also check my blog which would work in both Native + portal; enhance for your requirement

If it helped please mark blog as helpful and also bookmark

Verify Mandatory Attachments Count on Catalog Item

Regards
Ankur

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

Thanks for your response sir, please be informed that at first, I tried 'Require attachment for catalog item in Platform and Service Portal' but the same error occurred then only I switch to below single catalog client script with ui type= all & isolate script="false"

function onSubmit() {
//Type appropriate comment here, and begin script below
if (g_form.getValue('is_attachment_reqd') == "Yes") { //update field values
try { //Works in non-portal ui
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
g_form.addErrorMessage('plz attach doc.');
return false;
}
} catch (e) { //For Service Portal
var count = getSCAttachmentCount();
if (count <= 0) {
g_form.addErrorMessage('plz attach doc.');
return false;
}
}
}
}

please help me to run the same script on the service portal as well, current below error is displaying, & after checking the console below screenshot error is showing

find_real_file.png

find_real_file.png

then check my blog and use that script.

it works in both native + portal

Regards
Ankur

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

Thanks its working now