Using JSUtil.nil in business rule

sigmachiuta
Kilo Guru

I am trying to use JSUtil.nil in a business rule but it does not assign to the group.   If i directly set the assignment_group to group 1 it will insert the record correctly.   Is there a reason JSUtil.nil does not work in a BR?

grTest.priority = 2;
grTest.approval = "approved";
if(!JSUtil.nil(Group1)){
grTest.assignment_group = Group1;
}
else if (!JSUtil.nil(Group2)){
grTest.assignment_group = Group2;
}
grTest.insert();
1 ACCEPTED SOLUTION

poyntzj
Kilo Sage

why not use JSUtil.notNil() instead of if(!JSutil.nil())


View solution in original post

8 REPLIES 8

not sure why if(!JSutil.nil()) doesnt work but thanks Julian JSUtil.notNil() works


I have stopped using JSUtil since it is problematic and there are better ways using native JavaScript to do the same thing. I suggest using the following code:



var Group1 = 'test', Group2 = 'c29ea314c0a80065699cd4bff4f6969c', grTest = {};//NOTE: these are only test values to prove the code and not intended for production



if(isSysId(Group1) ) grTest.assignment_group = Group1;


else if (isSysId(Group2) ) grTest.assignment_group = Group2;


function isSysId(itemToTest) {


  return !Boolean(String(itemToTest).search( '^([0-9a-fA-F]{32})$' ) );


}



gs.print('Group1 is ' + Group1 + ' Group2 is ' + Group2 + ' grTest.assignment_group is ' + grTest.assignment_group);//NOTE: this is only for testing to prove the code and not intended for production



Please see my presentation at Crafting Robust JavaScript Presentation for Knowledge16


Of course if you are setting a reference field such as assignment_group using the name then you should set it with the GlideRecord setDisplayValue function:


Here's an example:


var Group1 = 'LDAP Admins', Group2 = 'c29ea314c0a80065699cd4bff4f6969c', grTest = new GlideRecord('cmdb');//NOTE: this is only for testing to prove the code and not intended for production


grTest.addNullQuery('assignment_group');


grTest.setLimit(1);


grTest.query();


if(grTest.isValid() && grTest.next() ) {


  gs.print('grTest is ' + grTest.name + ' id is ' + grTest.sys_id + ' grTest.assignment_group.sys_id is ' + grTest.assignment_group.sys_id + ' grTest.assignment_group.name is ' + grTest.assignment_group.name);//NOTE: this is only for testing to prove the code and not intended for production


  if(isSysId(Group1) ) grTest.assignment_group = Group1;


  else if(Group1) grTest.assignment_group.setDisplayValue(Group1);


  else if (isSysId(Group2) ) grTest.assignment_group = Group2;


  else if(Group2) grTest.assignment_group.setDisplayValue(Group2);


  grTest.update();


  gs.print('grTest.assignment_group.sys_id is ' + grTest.assignment_group.sys_id + ' grTest.assignment_group.name is ' + grTest.assignment_group.name);//NOTE: this is only for testing to prove the code and not intended for production


  //if the assignment_group was not set properly, clear it out


  if(grTest.assignment_group && !isSysId(grTest.assignment_group) ) {


  grTest.assignment_group = "NULL";


  grTest.update();


  gs.print('grTest.assignment_group.sys_id is ' + grTest.assignment_group.sys_id + ' grTest.assignment_group.name is ' + grTest.assignment_group.name);//NOTE: this is only for testing to prove the code and not intended for production


  }


}


function isSysId(itemToTest) {


  return !Boolean(String(itemToTest).search( '^([0-9a-fA-F]{32})$' ) );//using Boolean to convert the output type of search (number) to Boolean. Using String since it always works no matter what itemToTest is


}


lcbell
Tera Expert

The JSUtil.nil function has the following issues:


1. First it calls JSUtil.isJavaObject in an if statement so if you are only sending in JavaScript items this isn't necessary or helpful. The if Statement has six execution steps. JSUtil.isJavaObject checks if the item is null in an if statement using the Equals Operator ( == ) (see the explanation in 2 below). If the item is not null then it calls value instanceof Packages.java.lang.Object in an if statement. The instanceof operator has 8 steps. If the item is not an instance of Packages.java.lang.Object then it calls GlideJSUtil.isJavaObject. So if the item is the empty string or undefined, there are a lot of execution steps that are wasted.


2. Second it checks the item if it is null (which isn't necessary since it already tested it in 1 above), typeof item is 'undefined', or the empty string (after doing a String concatenation with the the empty string which calls toString function of the item) using the Equals Operator ( == ) which has 6 steps including The Abstract Equality Comparison Algorithm in the fifth step. This could be up to 3 executions of the Abstract Equality Comparison Algorithm, which has 22 steps. It would be much better if these were implemented with the Strict Equals Operator ( === ) which has 6 steps including The Strict Equality Comparison Algorithm, which has 13 steps.



For JavaScript items, all of these checks can be handled by using the if statement and a logical NOT operator such as if(!Group1). The logical NOT operator only has five steps. The if Statement only has six steps and one of them calls the ToBoolean operator (see http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20Decem... ) so if the value being tested will never be zero or false then this is a simple way to handle it with far less execution steps.