rbw / aiosnow

Asynchronous ServiceNow Library
MIT License
74 stars 12 forks source link

Query on particular assignment group #48

Closed fartbagxp closed 4 years ago

fartbagxp commented 4 years ago

I can't get any results using a query against a particular assignment group. It returns nothing, and I'm wondering if I have to create an Assignment Group schema created.

I have a simple schema.

class Incident(Schema):
  __location__ = "/api/now/table/incident"

  sys_id = fields.Text(is_primary=True)
  sys_created_by = fields.Text()
  sys_created_on = fields.Datetime()
  number = fields.Text()
  incident_state = fields.Text()
  state = fields.Text()
  short_description = fields.Text()
  description = fields.Text()
  assignment_group = fields.Text(pluck=Joined.DISPLAY_VALUE)
  assigned_to = fields.Text()
  opened_at = fields.Datetime()
  closed_at = fields.Text()
  sys_updated_on = fields.Datetime()
  closed_by = fields.Text()
  active = fields.Text()

async with self.app.resource(Incident) as r:
  selection = select(
    Incident.assignment_group.equals("Service Desk")
  ).order_desc(Incident.number)

  async for item in r.stream(selection, limit=0, chunk_size=250):
    print(item)

I spun up a ServiceNow test instance and this assignment group seems like a common group. image

I tried with datetime query selection and those are working, just not the one for assignment group.

Any help is much appreciated.

rbw commented 4 years ago

It returns nothing, and I'm wondering if I have to create an Assignment Group schema created.

Yes, I'm aware of this. Relations are queried by sys_id if not using a PartialSchema. While this is not a bug, I do agree automatically querying by name in this case would be more natural.

Currently, if you want to query by name, you can use a PartialSchema:

class AssignmentGroup(PartialSchema):
    sys_id = fields.Text()
    name = fields.Text()

class Incident(Schema):
    __location__ = "/api/now/table/incident"

    number = fields.Text()
    assignment_group = AssignmentGroup

selection = select(
    Incident.assignment_group.name.equals("Service Desk")
).order_desc(Incident.number)

Building support for querying by a related table's field without using PartialSchema shouldn't be too hard.

What's your take on something like this?:

assignment_group = fields.Text(pluck=Joined.DISPLAY_VALUE, query_by="assignment_group.name")

...or perhaps always use related table's name when plucking display value. That'd be somewhat inflexible and implicit though. Hmm.. need to do some pondering on this.

fartbagxp commented 4 years ago

Ahh, I didn't know the existence of PartialSchema. The following is clear to me, provided that I use a PartialSchema.

selection = select(
    Incident.assignment_group.name.equals("Service Desk")
).order_desc(Incident.number)

I prefer not having something like query_by - it feels like a bit of a hack to put that in, but may be useful in some other cases. This might be more of an example / nudge of how the users is intended to use the library. I much prefer the Incident.assignment_group.name.equals("Service Desk").

Thank you!

rbw commented 4 years ago

No problem.

It's covered in the documentation here: https://python-snow.readthedocs.io/en/latest/reference/resource/schema.html#nesting