Please help review script condition not working.

Jessica28
Tera Guru

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:

Jessica28_0-1713798280387.png

 

 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'
});      
3 ACCEPTED SOLUTIONS

Dom Gattuso
Mega Sage

@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"'){

 

View solution in original post

Amit Pandey
Kilo Sage

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

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@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;
    },

View solution in original post

7 REPLIES 7

Amit Pandey
Kilo Sage

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

@Amit Pandey 

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

 

Sandeep Rajput
Tera Patron
Tera Patron

@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;
    },