create business rule with rest api

ericharrington
Mega Contributor

Hello,

I've been trying to find out the answer to this in this community and around the web without success so far.

This is what I'm trying to accomplish:

I want to be able to create/get a business rule (the when, what, and advaced script) via the REST api, is this possible?

10 REPLIES 10

Community Alums
Not applicable

OK, that makes more sense, thanks.



If that is the case, and as you stated in another comment, the 'sys_script' table is not available in the table API, so in order to create the BR, you would need to use a scripted rest API. This would allow you to do an API call and insert a business rule into the 'sys_script' table.



However, if the concern is not wanting to manually create some artifact on the remote end (servicenow), having to create the scripted rest api on the instance would basically be the same as having to create the business rule in the first place.



Cheers,



Tim


ark6
Mega Guru

Hi Eric,



Try using the direct web services for OOB tables



https://<instance>.service-now.com/sys_script.do?wsdl for SOAP



and



https://<instance>.service-now.com/api/now/table/<table_name with query> for REST.



You can find references here


http://wiki.servicenow.com/index.php?title=Direct_Web_Services#gsc.tab=0



Also, you can create scripted APIs based on your requirement


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hello Eric,


Well REST get on a BR is straightforward - a post might need more attention!



curl "https://xxxxx.service-now.com/api/now/table/sys_script/2bc2f9b1c0a801640199f9eb0067326e?sysparm_disp..."   --request GET   --header "Accept:application/json"   --user 'admin':'admin'



Yep it is the incident query BR...


{


    "result":{


        "client_callable":"false",


        "template":"",


        "sys_replace_on_upgrade":"false",


        "access":"This application scope only",


        "action_insert":"false",


        "action_update":"false",


        "advanced":"true",


        "action_delete":"false",


        "change_fields":"false",


        "description":"",


        "action_query":"true",


        "sys_updated_on":"2011-07-14 13:50:43",


        "when":"before",


        "sys_class_name":"Business Rule",


        "sys_id":"2bc2f9b1c0a801640199f9eb0067326e",


        "is_rest":"false",


        "rest_method_text":"",


        "sys_updated_by":"admin",


        "rest_service_text":"",


        "sys_created_on":"2005-02-19 09:56:00",


        "sys_domain":"global",


        "sys_name":"incident query",


        "sys_scope":"Global",


        "sys_created_by":"fred.luddy",


        "order":"100",


        "rest_method":"",


        "rest_service":"",


        "add_message":"false",


        "sys_mod_count":"12",


        "active":"true",


        "sys_overrides":"",


        "collection":"incident",


        "message":null,


        "priority":"",


        "sys_domain_path":"/",


        "sys_tags":"",


        "script":"if (!gs.hasRole(\"itil\") && gs.isInteractive()) {\r\n   var u = gs.getUserID();\r\n   var qc = current.addQuery(\"caller_id\", u).addOrCondition(\"opened_by\", u).addOrCondition(\"watch_list\", \"CONTAINS\", u);\r\n   gs.print(\"query restricted to user: \" + u);\r\n}",


        "abort_action":"false",


        "execute_function":"false",


        "filter_condition":"",


        "sys_package":"System (apps/system1)",


        "condition":"",


        "sys_update_name":"sys_script_2bc2f9b1c0a801640199f9eb0067326e",


        "rest_variables":"",


        "name":"incident query",


        "sys_customer_update":"false",


        "role_conditions":"",


        "sys_policy":null


    }


}



If the reply was informational, please like, mark as helpful or mark as correct!


Anthony,



This looks promising however there are a couple of things I'm still not sure:


1) where did you get the BR id from? (in my case I'll need to do a GET to verify if a the BR exists first in order to create it or not), so I'll need to do some sort of search/filter instead of getting the specific br by id


2) I wanted to test this in the REST API explorer but the sys_script table is not available, what am I missing?



Sorry for asking this much, I've never used or worked with ServiceNow and I'm having trouble finding all this.


Pallavi14
Kilo Contributor

We tried to follow above suggestion to create business rule using Rest Api call with Postman. 

URL: https://{service-now-instance}/api/now/table/sys_script

Body :

{
   
        "client_callable": "false",
        "template": "",
        "access": "package_private",
        "action_insert": "true",
        "action_update": "true",
        "advanced": "true",
        "action_delete": "false",
        "change_fields": "false",
        "description": "",
        "action_query": "false",
        "sys_updated_on": "2020-07-20 11:39:58",
        "when": "after",
        "sys_class_name": "sys_script",
       
        "is_rest": "false",
        "rest_method_text": "",
        "sys_updated_by": "admin",
        "rest_service_text": "",
        "sys_created_on": "2020-07-20 10:16:28",
        "sys_domain": {
            "link": "https://dev71276.service-now.com/api/now/table/sys_user_group/{link=https://dev71276.service-n",
            "value": "{link=https://dev71276.service-n"
        },
        "sys_name": "postman_rule",
        "sys_scope": {
            "link": "https://dev71276.service-now.com/api/now/table/sys_scope/global",
            "value": "global"
        },
        "sys_created_by": "admin",
        "order": "100",
        "rest_method": "",
        "rest_service": "",
        "add_message": "false",
        "sys_mod_count": "7",
        "active": "true",
        "sys_overrides": "",
        "collection": "incident",
        "message": "",
        "priority": "100",
        "sys_domain_path": "/",
        "sys_tags": "",
        "script": "(function executeRule(current, previous /*null when async*/) {\r\n\tgs.info(\"webhook calling\");\r\n\t// Add your code here\r\n\r\n})(current, previous);",
        "abort_action": "false",
        "execute_function": "false",
        "filter_condition": "",
        "sys_package": {
            "link": "https://dev71276.service-now.com/api/now/table/sys_package/global",
            "value": "global"
        },
        "condition": "",
       
        "rest_variables": "",
        "name": "postman_rule",
        "role_conditions": "",
        "sys_policy": ""
    }

 

Rest call was successful and entry is listed in UI. But business rule is not getting triggered for incident Insert/Create event.

Can you please help us for:

 

1. Is there any way to see sys_script table API in Rest API explorer? Currently it is not visible on Explorer.

2. What can we do to solve this issue? is there any prior api call/ body parameters missing in this POST call? 

3. Is there any other way to achieve it other than using UI as we need to do this using script.