- 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: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:27 AM - edited 04-22-2024 08:28 AM
Hi @Jessica28,
Within your Script Include, you don't need to 'JSON.stringfy()' the result.
Remove the line: return JSON.stringify(result);
Replace with: return result;
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:42 AM
Hi @Robbie
I followed your suggestion to replace return JSON.stringify(result) with return result, but still no return value.
I might have placed the return value in the wrong area.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 09:06 AM - edited 04-22-2024 09:06 AM
Hi @Jessica28,
The adjusted 'return' statement is in the write area.
I have however just spotted the 'result' variable is not declared.
Easily fixed... a line above where you have defined the 'var sysIdNumber', also define the result variable as follows:
var result = '';
This should fix it.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie