Find assigned to is member of particular group

Dileep2
Tera Contributor

Hi All,

 

I have assigned to field referencing to user table. Now I want to check if the Assigned to user is part of  "BCKAM -200&210" group. If Yes I have to clear Assignment group field value.

 

Please suggest any solution.

8 REPLIES 8

Hi @Dileep2 

Follow below for your scripts

function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading || newValue == '') {
              return;
      }
var assignedTo = g_form.getValue("assigned_to");

var ga = new GlideAjax('scriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_assignedTo', assignedTo );
ga.getXML(callback);
}

function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}

in the script include

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

functionName: function () {
var assignedTo= this.getParameter('sysparm_assignedTo');
var groupSysId = "abcd";//this should be sys_id of BKACS group
var member = new GlideRecord("sys_gr_member");
member.addQuery("group",groupSysId);
member.addQuery("user",assignedTo);
member.query();
if(member.next()){
return true; //It means he is part of it
}
}
}
})
Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

What you have tried?

Shekhar Navhak1
Kilo Sage
Kilo Sage

Hi @Dileep2 ,

 

Please find below script to resolve your issue:

 

Onchange client script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	var usr=g_form.getValue("assigned_to");
    var ga = new GlideAjax('validateAssignee');
    ga.addParam('sysparm_name', 'getCampus');
    ga.addParam('sysparm_userid',usr);
    ga.getXML(updateCampus);


    function updateCampus(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        if (answer=='true') {
            g_form.setValue("assigned_to", '');
            g_form.addErrorMessage('Assignee is member of XYZ group');
        }
    }
}

 

ShekharNavhak1_0-1674638159235.png

Client Callable script include (Replace 'ITSM App-Dev' with your group name):

 

var validateAssignee = Class.create();
validateAssignee.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCampus: function() {
        var userid = this.getParameter('sysparm_userid');
        var usr = new GlideRecord('sys_user_grmember');
        usr.addEncodedQuery('group.name=ITSM App-Dev^user=' + userid); // Replace 'ITSM App-Dev' with your group name
        usr.query();
        if (usr.hasNext()) {
            return 'true';
        } else {
            return 'false';
        }

    },

    type: 'validateAssignee'
});

 

ShekharNavhak1_1-1674638248557.png

 

 

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar

Ankur Bawiskar
Tera Patron
Tera Patron

@Dileep2 

It should be pretty simple.

What script did you start with and where are you stuck?

If you try from your side it will be learning for you as well.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader