need to make variable mandatory and visible for specific group

pritimayee
Tera Contributor

Can anyone please help me on the below requirement. Need to make one variable visible when the logged in user is member of specific group and need to make mandatory if the variable request type is assign or delete.

 

12 REPLIES 12

Pavankumar_1
Mega Patron

Hi,

why you deleted the comment @pritimayee1@gmail  that is not a good thing?

If you the post or comments for our efforts are useless.

 

Regards,
Pavankumar
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi,

I have not deleted any comments. Every comment is here.

Hi,

Okay. No issues. Please check my solution and let me know if you need anything.

If it resolve your issue close the thread by Mark Correct. 

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi,

create client and script include using GlideAjax you can get it.

Script Include: create script include and check client callable is true.

 

var Fetchusergroupinfo = Class.create();
Fetchusergroupinfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getData: function() {

        var grusr = new GlideRecord('sys_user_grmember');
        grusr.addQuery('group', '8a4dde73c6112278017a6a4baf547aa7'); //sysid of your group
        grusr.addQuery('user', gs.getUserID()); //it will check with logged in user
        grusr.query();
        if (grusr.hasNext()) { //if it is part of group it will return true and we can pass it to client script.
            return true;
        }

    },


    type: 'Fetchusergroupinfo'
});

find_real_file.png

Client Script: Create onload catalog client script.

 

function onLoad() {
    var req = g_form.getValue('reuqest_type');

    var gausr = new GlideAjax('Fetchusergroupinfo');
    gausr.addParam('sysparm_name', 'getData');
    gausr.getXMLAnswer(setDetails);

    function setDetails(response) {

        if (response == true) {
            if (req == 'delete' || 'assign') { // add backend values I am assuming those are delete and assign
                g_form.setMandatory('fieldname', true); //add your field that you need to make mandatory
            } else {
                g_form.setVisible('fieldname', true); //show field when user is part of group and request type is not delete and assign
            }
        }


    }

}

 

Hope it helps!

Please Mark Correct if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Mahendra RC
Mega Sage

Hello Pritimayee,

You can create a onLoad client script as shown below:

var requestType = g_form.getValue('request_type')
if (requestType == "assign" || requestType == "delete") {
	var ga = new GlideAjax("EscalatedScTask");
	ga.addParam("sysparm_name", "checkIfGroupMember");
	ga.addParam("sysparm_group_name", "YOUR GROUP NAME");
	ga.getXMLAnswer(parseReturnedResponse);
}

function parseReturnedResponse(response) {
	var isMember = response;
	if(isMember) {
		g_form.setDisplay("YOUR_FIELD_NAME", true);
		g_form.setMandatory("YOUR_FIELD_NAME", true);
}

Create a scritp include where client callable is set to true and use the below code:

checkIfGroupMember: function () {
var groupName = this.getParameter("sysparm_group_name");
return gs.getUser().isMemberOf(groupName);
}

Please mark my respsone as correct/helpful, if it answer your question.

Thanks