Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

ismemberOf in BR condition field

TM19
Tera Expert

Hi

I want to make a scoped BR run a conditon:

Suppose the BR is on incident table, then the BR should only run when the caller in the caller_id field is a member of a particular group.

I tried the below code in the condition field 

gs.getUser(current.caller_id).isMemberOf(<group name>)

But this gives false, because instead of taking the user in caller field it takes logged in user.

 

Any solution for this?

Note: If similar code is user in background script as below in a gliderecord:

gs.getUser(gr.caller_id).isMemberOf(<group name>)

It returns the expected output. 

 

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

HI @TM,

 

isMemberOf() will only work for loggedin users but as your requirement is to use the script in BR CONDITION. To achieve this you need to write a Script include and GlideRecord Group Member table and check if Caller is member of group then return true otherwise return false. Let me give you a sample.

 

BR CONDITION [if script include is in SAME scope]

 

javascript: new groupMemberUtility().member(current.caller_id);

 

BR CONDITION [if script include is in GLOBAL scope i.e call it by its API name]

 

javascript: new global.groupMemberUtility().member(current.caller_id);

 

SCRIPT INCLUDE

NAME - groupMemberUtility

var groupMemberUtility = Class.create(); //(auto generated)
groupMemberUtility.prototype = Object.extendsObject(AbstractAjaxProcessor, { //(auto generated)

    member: function(data) {

        gs.info("$$SH ---> Script include called!!! " + data);
        var gr = new GlideRecord("sys_user_grmember");
        gr.addQuery("user", data);
	    gr.setLimit(1);
	    gr.query();
        
	    if (gr.next())
           return true;

        return false;
    },

});

 

 

Please mark this CORRECT & HELPFUL, if it answered your question.

 

Thanks & Regards,
Sharjeel

Regards,
Muhammad

View solution in original post

24 REPLIES 24

MrMuhammad
Giga Sage

HI @TM,

 

isMemberOf() will only work for loggedin users but as your requirement is to use the script in BR CONDITION. To achieve this you need to write a Script include and GlideRecord Group Member table and check if Caller is member of group then return true otherwise return false. Let me give you a sample.

 

BR CONDITION [if script include is in SAME scope]

 

javascript: new groupMemberUtility().member(current.caller_id);

 

BR CONDITION [if script include is in GLOBAL scope i.e call it by its API name]

 

javascript: new global.groupMemberUtility().member(current.caller_id);

 

SCRIPT INCLUDE

NAME - groupMemberUtility

var groupMemberUtility = Class.create(); //(auto generated)
groupMemberUtility.prototype = Object.extendsObject(AbstractAjaxProcessor, { //(auto generated)

    member: function(data) {

        gs.info("$$SH ---> Script include called!!! " + data);
        var gr = new GlideRecord("sys_user_grmember");
        gr.addQuery("user", data);
	    gr.setLimit(1);
	    gr.query();
        
	    if (gr.next())
           return true;

        return false;
    },

});

 

 

Please mark this CORRECT & HELPFUL, if it answered your question.

 

Thanks & Regards,
Sharjeel

Regards,
Muhammad

Nice work. I was going to suggest creating your own script include and calling it from the condition field.

FWIW, you don't need 'javascript:' in a condition field. It already knows it's JavaScript (e.g. current.active, for example, not javascript:current.active)

Thanks @Chuck Tomasi.

Earlier I tested without " Javascript: " and it was not working so I went with "Javascript:". I Just tested again without "javascript:" after reading your feedback and its working fine šŸ™‚

Thanks again for the valuable information!

Regards,
Muhammad

Thanks Sharjeel

I was also going in the script include direction, was trying to find any OOB script include would help.

Thanks again

Hi Muhammad ,

Where are we giving the desired group's name?