Python query not picking up correct records in ServiceNow

bpolo
Tera Guru

We have a developer writing code for a Python/Flask web application. This web app talks to the ServiceNow API to query records from the security incident SNOW table. One of the fields in the query is that the parent_security_incident field should be blank, but the issue is that the query returns records regardless if the parent_security_incident field is blank or not.
This is his query:
sys_id = self.get_user_sys_id(user)
parent = ''
query =

f"affected_user={sys_id}&u_academic_year={academic_year}&assignment_group={DMCA_MANAGED_SYS_ID}&parent_security_incident={parent}"

 

Any thoughts on how he should code the query so as to only retrieve records that have a blank parent_security_incident.

Thanks in advance!

 
2 REPLIES 2

k_lutz
Tera Expert

Hi,

 

I think the issue is more in the SN script they are trying to send in python. I would remove the part where they say "parent = ''" and adjust the query at the end to be like "&parent_security_incidentISEMPTY" which is more of what you would do for checking if that field is empty.

Rafael Batistot
Kilo Patron

Hi @bpolo 

 

In ServiceNow, querying for blank/empty fields isn’t done by passing an empty string. Instead, you need to use the query operator ISEMPTY (or ISNOTEMPTY if you’re looking for the opposite).

 

query = (
f"affected_user={sys_id}"
f"&u_academic_year={academic_year}"
f"&assignment_group={DMCA_MANAGED_SYS_ID}"
f"&parent_security_incidentISEMPTY"
)

 

ISEMPTY goes directly after the field name, without an = or value.

 

This ensures the API only returns records where parent_security_incident is truly empty.