Calling a Script Include from a Business Rule (both in same scope)

CraigW1
Giga Contributor

I am trying to call a Script Include from a business rule on a Madrid instance. The problem is that when I try to initialize it, I get the following error: "The undefined value has no properties". To rule out any issue from my own Script Include, here's an example using a built in (to Security Incident Response app) Script Include that throws the same error:

Business Rule:

Table: sn_si_incident
Application: Security Incident Response
Active: True
Advanced:True:
When to run:
When: After
Order: 100
Insert: True
<No Filter Conditions/Role Conditions>

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


gs.warn("Calling incident utils");
var x = sn_si.SecurityIncidentUtils();
gs.warn("Successfully called incident utils");
})(current, previous);

Both the Script Include (sn_si.SecurityIncidentUtils) and my business Rule are in the Security Incident Response application. The first warn message prints,the second does not, and I get the following error in my log:

On my New York Personal Developer instance, I get similar behavior, but I get an additional error message (in addition to the one above):

org.mozilla.javascript.EcmaError: The undefined value has no properties.
Caused by error in sys_ui_action.42da42d00a0a0b340066377beb6dd099.script at line 1

==> 1: (function executeRule(current, previous /*null when async*/) {
2:
3:
4: gs.warn("Calling incident utils");

 

I also tried removing the `sn_si` Script Include namespace: var x = SecurityIncidentUtils()

That did not change the errors.

1 ACCEPTED SOLUTION

The issue you are facing is because you are calling the script include but not callingthe function of Script Include which you wants to get executed.

Please refer to one of the OOB Business Rules where the same script include and function is being called and executed without any issues. The same was intended in my very first comment.

OOB Business Rule: Close child security incidents

Script: 

// closes or cancels any active child SIs when the parent SI is closed or cancelled
function onAfter(current, previous) {
   new sn_si.SecurityIncidentUtils().closeActiveChildSecurityIncidents(current);
   new sn_si.SecurityIncidentUtils().closeAssessments(current.getUniqueValue());
}

So you can refer to the above script and see that the same script include "SecurityIncidentUtils" is called and the function "closeActiveChildSecurityIncidents" & "closeAssessments" is being called.

closeActiveChildSecurityIncidents: It closes all the active child records associated with the current record.

closeAssessments: Closes all the assessments of the child records.

 

Similarly you need to define the function which you want to be evaluated.

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok

View solution in original post

9 REPLIES 9

Could you please share latest code so that it will be easy to debug

CraigW1
Giga Contributor

Alok - thanks for your interest in helping me solve this issue!

The code above will throw the error if you debug it. Here's the steps to reproduce it.

0. If you don't have the Security Incident Response plugin installed in your developer instance, install it.

1. Create a business rule that targets the sn_si_incident table. Have it run this script in `onAfter` for Inserts.

2. Paste the code:

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


gs.warn("Calling incident utils");
var x = sn_si.SecurityIncidentUtils();
gs.warn("Successfully called incident utils");
})(current, previous);

3. Save the Business Rule

4. Insert a new security incident (Security Incident -> Show All Incidents. Then click the `New` button). It doesn't matter what's in the incident. I believe the only required field is the Short Description.

4. Inspect the instance log (System Logs -> All) and you will see the first message print, and then you will see the error messages. The second message doesn't print.

The issue you are facing is because you are calling the script include but not callingthe function of Script Include which you wants to get executed.

Please refer to one of the OOB Business Rules where the same script include and function is being called and executed without any issues. The same was intended in my very first comment.

OOB Business Rule: Close child security incidents

Script: 

// closes or cancels any active child SIs when the parent SI is closed or cancelled
function onAfter(current, previous) {
   new sn_si.SecurityIncidentUtils().closeActiveChildSecurityIncidents(current);
   new sn_si.SecurityIncidentUtils().closeAssessments(current.getUniqueValue());
}

So you can refer to the above script and see that the same script include "SecurityIncidentUtils" is called and the function "closeActiveChildSecurityIncidents" & "closeAssessments" is being called.

closeActiveChildSecurityIncidents: It closes all the active child records associated with the current record.

closeAssessments: Closes all the assessments of the child records.

 

Similarly you need to define the function which you want to be evaluated.

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok

CraigW1
Giga Contributor

Alok - thanks for the help! 

I was playing around with your solution and realized that I was missing the `new` keyword in front of my object creation!

I had:

 var x = sn_si.SecurityIncidentUtils();

and I had just forgotten and kept missing that I had forgotten the new keyword:

 var x = new sn_si.SecurityIncidentUtils();

If you want to update your answer to mention that, I'll then mark it as the correct answer.

Thanks!

Craig

Yes you were missing the 'new' and the function call. You can verify that in all of my solutions I had 'new' in my script as well as the initialization of function. And in the very beginning you confirmed that you were using new. So I concentrated on looking for some other issues. However I believe we are now on the same page and now the issue is resolved. Could you please mark my answer as Correct and Helpful based on the impact so that other users with similar questions may get benefit from this thread.