ServiceNow / PySNC

Python API for ServiceNow
MIT License
91 stars 28 forks source link

Fields not populated on insert to sysapproval_approver #108

Closed ashleyghooper closed 4 months ago

ashleyghooper commented 5 months ago

Hello, thanks for producing this helpful piece of software. I'm successfully creating change requests and populating most of the fields I need to. However, I also want to add approvals, and when I insert to sysapproval_approver, the values I provide for the fields sysapproval, document_id, and approver are lost, even though the record is created successfully.

I am trying to insert as below (adapted to remove irrelevant details of classes, etc):


from pysnc import ServiceNowClient
client = ServiceNowClient(
    config["url"],
    (config["user"], password),
)

table = "sysapproval_approver"

values_map = {
    "source_table": "change_request",
    "u_chg_manual_approval": "Peer Review",
    "sysapproval": f"{gr_cr.sys_id}",
    "document_id": f"{gr_cr.sys_id}",
    "approver": f"{schedule_approver_id}",
    "state": "not requested",
},

gr = client.GlideRecord(table)
gr.initialize()
for key, value in values_map.items():
    setattr(gr, key, value)

If I print() the glide record after setting attributes as above, it looks like this:

sysapproval_approver(
    {
        'source_table': 'change_request',
        'u_chg_manual_approval': 'Peer Review',
        'sysapproval': '3b7b457287ae0a103c644009dabb35c3',
        'document_id': '3b7b457287ae0a103c644009dabb35c3',
        'approver': '7ffa419f1bbc3410778221fc274bcb49',
        'state': 'not requested'
    }
)

However, the resulting record in the sysapproval_approver table looks like this, and no approvals show against the change request record in the web UI.

        {
            "approver": "",
            "comments": "",
            "sysapproval": "",
            "due_date": "",
            "sys_mod_count": "0",
            "approval_journal_column": "",
            "approval_column": "",
            "sys_updated_on": "2024-06-10 22:05:58",
            "document_id": "",
            "sys_tags": "",
            "u_chg_manual_approval": "Peer Review",
            "expected_start": "",
            "sys_id": "06041d98877a4610ad4d8515dabb359b",
            "sys_updated_by": "API-User",
            "sys_created_on": "2024-06-10 22:05:58",
            "iteration": "1",
            "state": "not requested",
            "approval_source": "",
            "source_table": "change_request",
            "sys_created_by": "API-User",
            "group": "",
            "order": ""
        },

I have confirmed the sys_id that is used in the sysapproval and document_id fields is valid, and refers to the change request in question. I can query it via the REST API like so:

https://myinstance.service-now.com/api/sn_chg_rest/change/3b7b457287ae0a103c644009dabb35c3

Likewise, I can look up the value in the approver field against the sys_user table, and the correct user is returned.

https://myinstance.service-now.com/api/now/table/sys_user/7ffa419f1bbc3410778221fc274bcb49
vetsin commented 5 months ago

If you can validate that it works via REST api and not with pysnc, I will investigate more. Otherwise i suggest the following:

The sysapprover dictionary entry (/sys_dictionary.do?sys_id=aff1bfc0db3322005db1f9361d9619f7) is a Reference to the task table out of box:

  1. ensure your sys_id is a valid record for a task or table which extends task
  2. ensure your automation user has the sysapproval_approver.sysapproval role, as the above dictionary entry states it requires
  3. validate there are no before insert business rules modifying the value
  4. check your history on the record, to validate it's not being changed
  5. check any workflow contexts or flowrunner contexts
  6. double check the sys_log table for any hints

If this leads nowhere, I suggest trying NOW support. That being said if it's an issue with this library somehow i'll look into it more

ashleyghooper commented 4 months ago

For the record, the issue seems to have been due to requiring additional permissions to update the sysapproval, document_id, and approver columns in the table, which are understandably fairly tightly controlled, probably to avoid potential loopholes in the approval workflow.