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

@rishabh

Glad to know.

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

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

Done sir, thanks

Please mark my response as correct so that it helps other members.

Regards
Ankur

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

@rishabh

Please mark my response as correct so that it helps other members.

Regards
Ankur

 

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

I already did sir for your very first response, now I did the same for second response also thanks