Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Query with the incident number in the sys email table

SK41
Giga Guru

Hi, 

 

I have a requirement to check if I have received an email for the current incident. For this I am using glide record to query the sys_email table with current incident number to check if the email is there or not. I am searching the incident number in the target field of sys email but it seems query is not working. Could someone please help in resolving this?

Below is the code:

 

var id = fd_data.trigger.current.case;
gs.info("id is" + id);
if (id) {
var emailCheck = new GlideRecord('sys_email');
emailCheck.addQuery('instance', id);
emailCheck.query();
if (emailCheck.next()) {
return true;
} else {
return false;
}

 

Note: This script is used in flow to set the flow variable value.

Please suggest, how to fix it?

1 ACCEPTED SOLUTION

SK41
Giga Guru

Hi,

 

I was able to fix the issue by using sys id of the record as the query. Below code worked for me:

var id = fd_data.trigger.current.sys_id;
gs.info("id is" + id);
if (id) {
var emailCheck = new GlideRecord('sys_email');
emailCheck.addQuery('instance', id);
emailCheck.query();
if (emailCheck.next()) {
return true;
} else {
return false;
}

 

View solution in original post

6 REPLIES 6

Amit Pandey
Kilo Sage

Hi @SK41 

 

You can use filters to do that easily. BTW, for script, can you check this-

 

 

 

var id = fd_data.trigger.current.case; //You need to get the sys_id first.
gs.info("id is" + id);
if (id) {
    var emailCheck = new GlideRecord('sys_email');
    emailCheck.addQuery('instance', 'CONTAINS', id);  //This stores the sys_id. So id should contain sys_id only.
    emailCheck.query();
    if (emailCheck.hasNext()) { 
        gs.info("Email found for incident: " + id);
        return true;
    } else {
        gs.info("No email found for incident: " + id);
        return false;
    }
} else {
    gs.info("Incident number is empty.");
    return false;
}

 

 

 

Please share the logs to debug if this approach fails.

 

Regards,

Amit

This is not working. I think we cannot query the simple way as the target field in sys_email table is of document_id type. That is why the query is not working. Is there a way to query the document_id type field

awda.png

Hi @SK41 

 

The query is fine. It's just that you need to use the sys_id as it is reference field which I have already mentioned in the comments. I had tried above with sys_id in background script and that worked. 

 

Please mark my answer helpful and correct.

 

Regards,

Amit