- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:07 AM
Hello,
I'm learning scripting but encountered a problem at the "If" statement condition.
The callBackFunction returned a value. However, the if statement failed to trigger.
Here is the returned value:
Could someone please provide suggestion on how to fix it? Thank you
Client Script:
var gaAttFile = new GlideAjax('GetAttachmentFile');
gaAttFile.addParam('sysparm_name', 'getFile');
gaAttFile.addParam('sysparm_request_sysID', originalSysId);
gaAttFile.getXMLAnswer(callBackFunction);
function callBackFunction(result) {
alert(result);
if (result == 'Yes') {
alert('Has Attachment')
} else if (result == 'No'){
alert('No Attachment');
}
}
Script Include:
var GetAttachmentFile = Class.create();
GetAttachmentFile.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getFile: function() {
var sysIdNumber = this.getParameter("sysparm_request_sysID");
var grAtt = new GlideRecord('sys_attachment');
grAtt.addQuery('table_name', 'sc_req_item');
grAtt.addQuery('table_sys_id', sysIdNumber);
grAtt.addQuery('file_name', 'LIKE', '%.pdf');
grAtt.query();
if (grAtt.next()) {
result = 'Yes';
} else {
result = 'No';
}
return JSON.stringify(result);
},
type: 'GetAttachmentFile'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:13 AM - edited 04-22-2024 08:13 AM
@Jessica28 - I believe the issue is that you are using stringify on your return so the value in result contains the quotes. So if you were to change you if statement to the following, it should work:
if(result == '"Yes"'){
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:30 AM
Hi @Jessica28
Can you check this-
function callBackFunction(result) {
result = result.trim(); // Trim white spaces
if (result === 'Yes') {
alert('Has Attachment');
} else if (result === 'No') {
alert('No Attachment');
}
}
Regards,
Amit

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:49 AM
@Jessica28 Script in your script include is crashing as the result variable is not defined in getFile method please update the method getFile as follows and let me know if it works for you.
getFile: function() {
var sysIdNumber = this.getParameter("sysparm_request_sysID");
var grAtt = new GlideRecord('sys_attachment');
grAtt.addQuery('table_name', 'sc_req_item');
grAtt.addQuery('table_sys_id', sysIdNumber);
grAtt.addQuery('file_name', 'LIKE', '%.pdf');
grAtt.query();
var result = '';
if (grAtt.next()) {
result = 'Yes';
} else {
result = 'No';
}
return result;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:30 AM
Hi @Jessica28
Can you check this-
function callBackFunction(result) {
result = result.trim(); // Trim white spaces
if (result === 'Yes') {
alert('Has Attachment');
} else if (result === 'No') {
alert('No Attachment');
}
}
Regards,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:58 AM
I would like to know when I use the if statement, I must user the below line of code in order to get it to works is that right?
result = result.trim(); // Trim white spaces

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:49 AM
@Jessica28 Script in your script include is crashing as the result variable is not defined in getFile method please update the method getFile as follows and let me know if it works for you.
getFile: function() {
var sysIdNumber = this.getParameter("sysparm_request_sysID");
var grAtt = new GlideRecord('sys_attachment');
grAtt.addQuery('table_name', 'sc_req_item');
grAtt.addQuery('table_sys_id', sysIdNumber);
grAtt.addQuery('file_name', 'LIKE', '%.pdf');
grAtt.query();
var result = '';
if (grAtt.next()) {
result = 'Yes';
} else {
result = 'No';
}
return result;
},