How to pass parameters to a script include through a business rule

neil_b
Tera Guru

Hi,

 

I apologize if this has been asked before, but I couldn't find anything that was very helpful.

 

I have a business rule that runs on a custom user table (which is used to identify approval routings) where if a user is replaced, it updates any open approvals with the new user. 

 

My additional requirement is to update the watchlist with the new user as well and I need to do this through a script include so that it can be re-used for other scenarios where we update the watchlist.

 

Here is my after business rule

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

    var previousApprover = '';
    var currentApprover = '';

 if (current.u_approver != previous.u_approver) {
        previousApprover = previous.u_approver;
        currentApprover = current.u_approver;

var gr = new GlideRecord("sysapproval_approver");
    gr.addEncodedQuery('u_approval_stateINnot requested,requested,paused^approver='previousApprover)
    gr.query
    while (gr.next()) {
			gr.approver = currentApprover;
			gr.update();
// this is where I also want to call a script include and pass the previousApprover and currentApprover as variables
// I know that for a client script, i can use this.getParameter and glideajax.addParam but since this is a business rule I wasn't sure how to accomplish this so this is my attempt
			var watchList = new global.updateWatchList(); //this is my script include name
			watchList.getCurrentWatchList();
// I'm not sure what to do next to pass the two approvers as parameters to the script include

This is my script include

var updateWatchList = Class.create();
updateWatchList.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCurrentWatchList: function() {
// I'm not sure what to do at this point since I don't know how to pass the approvers as parameters to my script include. 
    },

    type: 'updateWatchList'
});

 

1 ACCEPTED SOLUTION

Hi @neil_b 

answer is used when calling script include using glideAjax that is client side.

Since you are calling the Script Include from a Business Rule (server-side), you can simply return the value from the Script Include function and store it in a variable

 

var watchList = new global.updateWatchList();
var newWatchList = watchList.getCurrentWatchList(previousApprover, currentApprover, lrNumber);

if (newWatchList) {
current.watch_list = newWatchList;
}

 

Also maybe you need to re-look into script include part. you are using replace() for array, here is the version you can use:

 

var grID = new GlideRecord("sc_req_item");
grID.addQuery("number", lrNumber);
grID.query();

if (grID.next()) {
var watchList = grID.watch_list.toString();

if (watchList.includes(previousApprover)) {
watchList = watchList.replace(previousApprover, currentApprover);
}

return watchList; // return string to Business Rule
}

return "";

 

Regards,

Mohammed Zakir

View solution in original post

4 REPLIES 4

lauri457
Tera Sage

You just define the parameters in your method definition and then you pass the data as arguments when you call that method. 

 

var watchList = new global.updateWatchList(); //this is my script include name
watchList.getCurrentWatchList(previousApprover , currentApprover); //names don't need to match
...
getCurrentWatchList: function(previousApprover , currentApprover) {
// I'm not sure what to do at this point since I don't know how to pass the approvers as parameters to my script include. 
},
...

 

Glideajax works differently as it uses the xmlhttpprocessor (xmlhttp.do). The client side data is sent to the server with a http post to this endpoint and the script include object is constructed with the request as a property which is then accessed with getparameter.

 

The script include methods work just like named functions in this context

 

Thank you @lauri457 that worked! That was incredibly simple! 😁

 

Do you know how I can pass the response from the script include back to the business rule?

 

For instance, once I do obtain the Watchlist, I want to pass the string back to the Business Rule. Would I do something like this? 

 

		var grID = new GlideRecord("sc_req_item");
		grID.addEncodedQuery('number=' + lrNumber);
		grID.query();
		if (grID.next()) {
			var watchList = [];
			watchList.push(grID.watch_list);			
			if (watch_list.includes(previousApprover))	{
			var newWatchList = watchList.replace(previousApprover,currentApprover);
			}
		} else {
		}
	return newWatchList; // Is this sufficient enough to pass back to the BR?

and then would the BR look like this?

			var watchList = new global.updateWatchList();
			watchList.getCurrentWatchList(previousApprover, currentApprover, lrNumber);
			return(answer); // Is adding this third line for "answer" enough to get the watchlist string back from the script include?

 

Hi @neil_b 

answer is used when calling script include using glideAjax that is client side.

Since you are calling the Script Include from a Business Rule (server-side), you can simply return the value from the Script Include function and store it in a variable

 

var watchList = new global.updateWatchList();
var newWatchList = watchList.getCurrentWatchList(previousApprover, currentApprover, lrNumber);

if (newWatchList) {
current.watch_list = newWatchList;
}

 

Also maybe you need to re-look into script include part. you are using replace() for array, here is the version you can use:

 

var grID = new GlideRecord("sc_req_item");
grID.addQuery("number", lrNumber);
grID.query();

if (grID.next()) {
var watchList = grID.watch_list.toString();

if (watchList.includes(previousApprover)) {
watchList = watchList.replace(previousApprover, currentApprover);
}

return watchList; // return string to Business Rule
}

return "";

 

Regards,

Mohammed Zakir

Perfect! That's it @Mohammed8 I appreciate your assistance!