How to create (function executeRule(current, previous /*null when async*/) using String operations programmatically to do a successful POST to BusinessRule?

Siddharth8
Kilo Contributor

I have been able to create Business Rule programmatically but need some help there: This is the script i want to create in Advanced tab:

(function executeRule(current, previous /*null when async*/) {

var strBody = "{";
strBody += "'Id': '" + current.number.toString() + "', ";
strBody += "'Title': '" + current.short_description.toString() + "', ";
strBody += "'Description': '" + current.description.toString() + "', ";
strBody += "'Category': '" + current.category.toString() + "', ";
strBody += "'Impact': '" + current.impact.toString() + "',";
strBody += "'Urgency': '" + current.urgency.toString() + "'";
strBody += "}";

var request = new sn_ws.RESTMessageV2('YourNameSpace', 'APIName');
request.setRequestBody(JSON.stringify(strBody));
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
})(current, previous);

 

The example was given to me above has a simple script but will appreciate if you guys can help me how to add it to below postData that i m using to create my business rule:

postBusinessRule = "{\r\n \"abort_action\": \"false\",\r\n \"access\": \"package_private\",\r\n \"action_delete\": \"true\",\r\n \"action_insert\": \"true\",\r\n \"action_query\": \"true\",\r\n \"action_update\": \"true\",\r\n \"active\": \"true\",\r\n \"add_message\": \"false\",\r\n \"advanced\": \"true\",\r\n \"change_fields\": \"true\",\r\n \"client_callable\": \"false\",\r\n \"collection\": \"incident\",\r\n \"condition\": [],\r\n \"description\": [],\r\n \"execute_function\": \"false\",\r\n \"filter_condition\": {\r\n \"@table\": \"incident\"\r\n },\r\n \"is_rest\": \"false\",\r\n \"message\": [],\r\n \"filter_condition\":\"stateVALCHANGES%5EORurgencyVALCHANGES%5EORdescriptionVALCHANGES%5EEQ\",\r\n \"name\": \"yourBusinessRuleName2\",\r\n \"order\": \"1\",\r\n \"priority\": \"1\",\r\n \"rest_method\": [],\r\n \"rest_method_text\": [],\r\n \"rest_service\": [],\r\n \"rest_service_text\": [],\r\n \"rest_variables\": [],\r\n \"role_conditions\": [],\r\n \"script\": \"(function executeRule(current, previous \/*null when async*\/) {var request = new sn_ws.RESTMessageV2('yourRestAPINameSpace', 'PostApiName'); request.setRequestBody(JSON.stringify()); var response = request.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode();})(current, previous);\",\r\n \"sys_class_name\": \"sys_script\",\r\n \"sys_created_by\": \"admin\",\r\n \"sys_created_on\": \"2020-02-24 17:56:33\",\r\n \"sys_customer_update\": \"false\",\r\n \"sys_domain\": \"global\",\r\n \"sys_domain_path\": \"\/\",\r\n \"sys_id\": \"\",\r\n \"sys_mod_count\": \"1\",\r\n \"sys_name\": \"create test BR\",\r\n \"sys_overrides\": [],\r\n \"sys_package\": {\r\n \"@display_value\": \"Global\",\r\n \"@source\": \"global\",\r\n \"#text\": \"global\"\r\n },\r\n \"sys_policy\": [],\r\n \"sys_replace_on_upgrade\": \"false\",\r\n \"sys_scope\": {\r\n \"@display_value\": \"Global\",\r\n \"#text\": \"global\"\r\n },\r\n \"sys_update_name\": \"sys_script_03edd56b2fc38814e8f955f62799b6e8\",\r\n \"sys_updated_by\": \"admin\",\r\n \"sys_updated_on\": \"2020-02-24 17:58:22\",\r\n \"template\": [],\r\n \"when\": \"after\"\r\n \r\n}"
This has been blocking me as i have everything else working. Your help is much appreciated.
 
Regards
Siddharth
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Siddharth,

can you explain what you are trying to achieve?

are you saying you want to set the request body?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Gowrisankar Sat
Tera Guru

You mean to INSERT a business rule record using POST operation?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Siddharth,

Any update on this?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader