Adding approvers to an open change request via REST API

rydevops
Kilo Contributor

Hi everyone, 

Does anyone know how to create one or more sysapproval_approver records and associate them to an existing change_request record? For some reason, when I submit a POST or PUT request to the sysapproval_approver table the record gets created however strips the approver and sysapproval references. If I refresh the change ticket there also doesn't appear to be any approvers added. 

If I go into the change request and go to approvers and click edit I am able to pick one or more users from the sys_user table and add them to the ticket. Looking at the URL during this edit operation it seems to be using an m2m reference however I can't find any reference to this in the lists either. If I add a user and search the sysapproval_approver table using the REST TABLE API I can also see the users that were manually added. 

I am not sure if I have to update another table that stores the many-to-many associations and if so what table or if it's even possible. Please note, I am not able to use server-side scripting to accomplish this action as that data is coming externally. I have also tried this using a personal development instance configured with stock settings and also had the same result. 

I can post REST code if desired. 


Any suggestions/thoughts would be greatly appreciated!


Note: I tried to post this question earlier however it gave me an error. if this becomes a duplicate question I apologize in advance. 

4 REPLIES 4

johnfeist
Mega Sage
Mega Sage

Hi Rydevops,

What values are you getting via REST and in what form?  Most of the necessary fields in sysapproval_approver are some form of reference.  These include:

  • approver -> sys_user
  • sysapproval (approval_for) -> task
  • source_table -> a table name e.g. incident
  • document_id (approving) -> a document_id, typically the object sys_id

It is very unlikely that you will get the correct internal values coming in from an external source.  That means you need to do the look ups either in a scripted REST api or via scripting in your transform map.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

rydevops
Kilo Contributor

Hi John

 

Agreed. The external system is actually creating the change request first which means I have access to the change_request data including the sys_id for it. I then perform a sys_user look to locate the user I want to add and required that data as well. I then try performing a POST or PUT request on the sysapproval_approver table with the fields you mentioned. Below I provide some more details. 

 

Before I tried submitting posts I created a change request manaully and then added a user to the change request. I then performed a GET request against the sysapproval_approvers table (Table API) and received the following results:

{
  "result": [
    {
      "approver": {
        "display_value": "Alissa Mountjoy",
        "link": "https://dev34669.service-now.com/api/now/table/sys_user/6a826bf03710200044e0bfc8bcbe5dec"
      },
      "comments": "",
      "sysapproval": {
        "display_value": "CHG0030002",
        "link": "https://dev34669.service-now.com/api/now/table/task/c5437cac2fac9010107db2e72799b650"
      },
      "due_date": "",
      "sys_mod_count": "0",
      "approval_journal_column": "",
      "approval_column": "",
      "sys_updated_on": "2020-05-01 07:02:44",
      "document_id": "",
      "process_step": "",
      "sys_tags": "",
      "expected_start": "",
      "sys_id": "741b29852fe01410107db2e72799b693",
      "state_binding": "",
      "sys_updated_by": "admin",
      "u_approveme": "false",
      "sys_created_on": "2020-05-01 07:02:44",
      "iteration": "1",
      "state": "Not Yet Requested",
      "approval_source": null,
      "source_table": "",
      "sys_created_by": "admin",
      "group": "",
      "order": ""
    }
  ]
}

Next, I removed the user from the approvers list and the tried to construct a similar request using both POST and PUT methods.

 
{
    "approver": "6a826bf03710200044e0bfc8bcbe5dec",
    "document_id": "c5437cac2fac9010107db2e72799b650",
    "source_table": "change_request",
    "state": "Not Yet Requested",
    "sysapproval": "c5437cac2fac9010107db2e72799b650"
}​
 
This returns back a 201 CREATED response however the data (e.g. approver, document_id, source_table and sysapproval) are all blank in the request. 
 
{
  "result": {
    "approver": "",
    "comments": "",
    "sysapproval": "",
    "due_date": "",
    "sys_mod_count": "0",
    "approval_journal_column": "",
    "approval_column": "",
    "sys_updated_on": "2020-05-01 07:13:22",
    "document_id": "",
    "process_step": "",
    "sys_tags": "",
    "expected_start": "",
    "sys_id": "148d69812f201410107db2e72799b668",
    "state_binding": "",
    "sys_updated_by": "admin",
    "u_approveme": "false",
    "sys_created_on": "2020-05-01 07:13:22",
    "iteration": "1",
    "state": "Not Yet Requested",
    "approval_source": null,
    "source_table": "change_request",
    "sys_created_by": "admin",
    "group": "",
    "order": ""
  }
}
If I try to do the PUT option against this to update it I get a 200 OK response and the same response as above. If I try doing this option with PATCH it returns 404 NOT FOUND. 
 
This is all from the REST API explorer but has the same results if I submit the data through code. 
 
I have also tried a few different payloads such as setting document_id to blank, finding the task sys_id for the change_request (which are different) and settings that for the document_id and settings the task sys_id for both options. No matter what I submit I cannot seem to create a new approver for the ticket via this table. 
 

Hi Rydevops,

To test a couple of theories, I created the following script on my PDI (Orlando):

addApprover : function() {
	var theApprovals = new GlideRecord("sysapproval_approver");
	theApprovals.initialize();
	theApprovals.setValue("approver","645e03b0eb32010045e1a5115206feb5");
	theApprovals.setValue("sysapproval", "1d9ead0f1bb3401038dcdc6cdc4bcb29");
	theApprovals.setValue("document_id", "1d9ead0f1bb3401038dcdc6cdc4bcb29");
	theApprovals.setValue("source_table", "incident");
	theApprovals.setValue("state", "requested");
	var newApproval = theApprovals.insert();
	var anotherApp = new GlideRecord("sysapproval_approver");
	anotherApp.get(newApproval);
	var zot = 1;
},

Nothing very fancy but it creates approvals as expected.  I first did it for myself and then put in Andrew Jackson's id.  The reason being that I have the approver roles and he doesn't.  We are now approvers for that incident (showing up in the table and related list).  BOth incident and change_request extend task so I took the easy route.

That says that we can add approvers directly as opposed to things like attachments.

I have three possible solutions you might try. 

Make sure that the user doing the POST/PUSH has the right roles to create an approval.

Try adding an approval to another table.  change_request and the associated objects have a lot of process and procedures around them.  Make sure that it isn't something specific to change_request.

If those don't do it you might try having the endpoint be a Scripted REST API where you can duplicate the script above as well as trap errors and create log entries to check what is happening.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hi John, 

 

I tried this via the scripted REST API as well and can confirm this works. What's odd is the exact same data payload is used in the script and it works yet in the REST API for the now namespace does not work. No errors either. Wonder if this is a bug?

Anyways thank you very much for helping me find a solution. I think I can work with this option!