How can I call a Script Include from a BR, and return a value to use in the BR?

truzicki
Kilo Expert

Greetings, everyone.   I'm trying to call a script include from a business rule, and return a value to the BR to use in updating a record.   My goal is to search for the value of a field (in this case "u_ad_department" in the sys_user table) in a table that contains aliases and a "normalized" value.   Then, I want to update another field (in this case the department field in the sys_user table) with the "normalized" value.   Here's what I've got so far:

The BR is:

var gr =   new ABCNormalization();

gr.getNormalization(current.u_ad_department, 'cmn_department', 'DEPARTMENT');

current.department = answer;

And the script include is:

var ABCNormalization = Class.create();

ABCNormalization.prototype = {

        initialize: function() {

        },

        getNormalization: function(alias, table, label) {

            var tableID = '';

            var grTable = new GlideRecord('sys_db_object');

            grTable.addQuery('name', table);

            grTable.query();

            while (grTable.next()) {

                      tableID = grTable.sys_id;

            }

            var returnText = 'NOT NORMALIZED ' + label;

            var grAlias = new GlideRecord('u_abc_normalization_aliases');

            grAlias.addQuery('u_alias', alias);

            grAlias.addQuery('u_table', tableID);

            grAlias.query();

            while (grAlias.next()) {

                      returnText = grAlias.u_normalized_value.getDisplayValue();

            }

            return returnText;

        },

        type: 'ABCNormalization'

};

Calling the Script Include works, and the Script Include itself works (I've sprinkled a few gs.log statements throughout).   But, I can't get the value I want to come back to the Business Rule so that I can use it.  

Can anyone point out where I've wandered off the ranch?

Thank you!

-Tim.

1 ACCEPTED SOLUTION

Abinaya
Mega Expert

Hi Tim,



If your working fine, in that case you have to just initialize a variable and call the function within your script include:



Use the following code:


var gr =   new ABCNormalization();  


var returnVal = gr.getNormalization(current.u_ad_department, 'cmn_department', 'DEPARTMENT');  


current.department = returnVal ;


View solution in original post

6 REPLIES 6

Abinaya
Mega Expert

Hi Tim,



If your working fine, in that case you have to just initialize a variable and call the function within your script include:



Use the following code:


var gr =   new ABCNormalization();  


var returnVal = gr.getNormalization(current.u_ad_department, 'cmn_department', 'DEPARTMENT');  


current.department = returnVal ;


Thank you, Vibha!   That worked perfectly, although I now realize I need to fix my script include to return the sys_id of the department name, rather than the proper name.   Anyhow, I greatly appreciate the help!



Best Regards,


-Tim.


I can't resist making this a one liner...



current.department =   new ABCNormalization().getNormalization(current.u_ad_department, 'cmn_department', 'DEPARTMENT');



Thanks, Chuck!   You know, the funny thing is that I tried that at one point in my multiple iterations of trying to get the script right.   The problem (in hindsight) was that I was passing the display name of the department, not the sys_id, so I didn't think it was working and moved on.   Rookie move!  



Thanks for chiming in, though -- I appreciate the guidance on simplifying it to one line!



Best Regards,


-Tim.