Business rule is running twice ( with "async" and "update")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2017 09:14 PM
Hi, I have a business rule that executes twice.
The business rule executes whenever the change Request database changes. I make a "change" to the "change request form" and save it, then my business rule executes twice.
I've looked at
Business rule appears to be running twice
Query business rule is running twice ?
Business rules are running multiple times for one application module in service now.
- This seems like a common problem without a (clear) answer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2017 10:23 PM
Sounds like a good idea, but there's too many business rules. (2016 of them!)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2017 09:39 PM
What is your BR doing? Can you include the code or a screenshot of the Actions tab? Is there a particular reason the Order is set to 50,000?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2017 10:22 PM
I haven't changed the action tab
The code sends an "outbound" message to another server of the record that has been changed. I set the order from 100 to 50000 with the hope that it would fix the issue.
There's no conditions, one of the solution suggestion putting a "lock" in there, but I couldn't get it to work.
The code is a little complex, and the java script formatting doesn't seem to work, so here it is in plain text.
Code is a bit messy sorry. Hacked it a lot to get it to work. Not sure if the code is causing the double call.
function CreateJson(obj, jsonstring){
if (obj.length > 0)
{
// jsonstring += "{";
}
for (var x in obj)
{
if (typeof obj[x] != "function")
{
if (obj[x] != null)
{
//gs.log(sep + x + ":: " + obj[x]);
jsonstring += '"' + x + '":"' + obj[x] + '",';
}
}
//logObj(obj[x], sep + "*" );
jsonstring = CreateJson(obj[x], jsonstring);
}
if (obj.length > 0)
{
// jsonstring += "}";
}
return jsonstring;
}
/*
function getChangedFieldNames(gr) {
var result = [];
var elements = gr.getElements();
var size = elements.size();
for (var i = 0; i < size; i++) {
var ge = elements.get(i);
if (ge.changes()) {
result.push(ge.getName());
}
}
return result;
}
*/
function logObj(obj, sep){
for (var x in obj){
if (typeof obj[x] != "function"){
gs.log(sep + x + ":: " + obj[x]);
}
logObj(obj[x], sep + "*" );
}
}
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var json = new global.JSON();
//gs.log("Entered");
//logObj(current, '*');
//Display an information message for each change to the record
var gru = GlideScriptRecordUtil.get(current);
//gs.log("Entered: 1");
var changedFields = gru.getChangedFields(); //Get changed fields with friendly names
//gs.log('j : ' + changedFields.toString() + ' : ' + changedValues.toString() );
//gs.log("Entered 2");
var changedValues = gru.getChanges(); //Get changed field values
//gs.log('j2 : ' + changedValues.toString() + ' : ' + changedValues.toString() );
//gs.log("Entered: 3");
//Convert to JavaScript Arrays
gs.include('j2js');
//gs.log("Entered: 4");
changedFields = j2js(changedFields);
//gs.log("Entered: 5");
changedValues = j2js(changedValues);
//gs.log("Entered: 6");
var jsonData;
//Process the changed fields
for(var i = 0; i < changedValues.length; i++)
{
var val = changedValues[i];
jsonData[changedFields[i]] = val.getDisplayValue();
gs.log(val.getDisplayValue());
}
var jsonstring = "{";
if (changedValues.length == 0)
{
//jsonData = current;
//jsonData = getChangedFieldNames(current);
//gs.log("jscount" + jsonData.length);
//jsonData = json.encode(jsonData);
jsonstring = CreateJson(current, jsonstring);
// remove the trailing ","
jsonstring = jsonstring.substring(0, jsonstring.length - 1);
jsonstring += "}";
gs.log( JSON.stringify(jsonstring));
//gs.log('jsonData: ' + JSON.stringify(jsonData));
}
var text = jsonstring ; //json.encode(jsonstring);
//gs.log(text);
//gs.log("Entered: 7");
try {
var r = new sn_ws.RESTMessageV2('name', 'post');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthentication(authentication type, profile name);
gs.log("RequestBody=" + JSON.stringify(text));
r.setRequestBody(text);
gs.log("Headers=" + r.getRequestHeader("Content-Type"));
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log("httpStatus:" + httpStatus);
gs.log("responseBody:" + responseBody);
}
catch(ex) {
var message = ex.getMessage();
gs.log("ERROR!");
gs.log(message);
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2017 11:05 PM
Hi Michael,
I do not see any issue in the code. To make sure that above code is working fine and not creating any issue, comment all the code and put only 1 log statement and check how many times you see that log. If you still see log more that one time than issue is not with this BR and it is occurring because of some other BR. May be you are using current.update. But if after commenting all the code you see log only one time than we need to find the issue in async BR itself.
Hope this helps.
Regards
Ujjawal