data validation issues

ujjwala_678
Tera Contributor

Hello Experts,

I am facing one issue, I have a catalog item which has a variable fetching attachment by end users, that needs to be validated using onsubmit client script and script include, the problem is even if i am attaching file with wrong headers, transform map is running successfully, i can't use current.setAbortaction(true) as i'm working in scoped application,

I am using below scripts, could anyone please help me out here?

Thank you in advance!

attch_file-----variable taking attachment in catalog item

On submit CS:

function onSubmit() {

 
var cat_id = g_form.getValue('attch_file');
alert(cat_id);
var ga = new GlideAjax('validateheaders');
ga.addParam('sysparm_name', 'validate');
ga.addParam('attch_file', cat_id);
ga.getXML(ajaxResponse);

function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (answer == 'false') {
g_form.addErrorMessage('not valid headers');
return false;

}
}
Script Include:
var validateheaders = Class.create();
validateheaders.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
validate: function() {
var attachmentSysID = this.getParameter('attch_file');
var attachment = new GlideSysAttachment();
var attachmentStream = attachment.getContentStream(attachmentSysID);
var parser = new sn_impex.GlideExcelParser();
parser.parse(attachmentStream);
var headers = parser.getColumnHeaders();

var mandatoryHeaders = ["NAME", "NUMBER", "DATE", "CREATION_DATE", "PACKAGE"];

var isValid = true;

while (parser.next()) {
var row = parser.getRow();

for (var header in mandatoryHeaders) {
var currentHeader = mandatoryHeaders[header];

// Check if the header is present in the column headers
if (headers.indexOf(currentHeader) === -1) {
isValid = false;
 
}

// Check if the value in the mandatory column is empty
if (global.JSUtil.nil(row[currentHeader])) {
isValid = false;
 

}
}
}

return isValid;

 
},



type: 'validateheaders'
});
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@ujjwala_678 

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer this link

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

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

@ujjwala_678 

Thank you for marking my response as helpful.

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

Hello Sir,

Thank you so much for replying,

I will use the approach shared and update you with the results.

Thank you