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.

Cancel Change Record Without Completing Mandatory Fields for certain group

atul_05
Tera Contributor

Hello Team,

 

I have a requirement to Cancel Change Record Without Completing Mandatory Fields for a xyz group. Can you please help me on how we can achieve this? 
There is a UI action already but I am not sure how to proceed with this requirement and how to configure.

UI action - Cancel Change.png

7 REPLIES 7

Najmuddin Mohd
Mega Sage

Hi @atul_05 ,

One way is to add a client function which checks whether the user is part of a group from a Script include which returns true or False, using gs.getUser().isMemberOf('group_sysID')

If it's true, then make g_form.setMandatory('field_name', false);

And, then execute the server side function.

Regards,
Najmuddin.

atul_05
Tera Contributor

Hi Najmuddin,
thanks for your response.

Can you please provide the code for this. Also there can be multiple fields that are mandatory so do I need to set that for all the fields? g_form.setMandatory('field_name', false);

HI @atul_05 ,

UI Action code:

 

function Cancel() {
	g_form.addInfoMessage('In client script');
    var ga = new GlideAjax('Community');
    ga.addParam('sysparm_name', 'userGroup');
    ga.getXML(answer);
}

function answer(response) {
	
    var answer = response.responseXML.documentElement.getAttribute("answer");
	
    if (answer) {
        g_form.setMandatory('caller_id', false);
		g_form.setMandatory('short_description',false); // write all the fields which needs to be mandatory false.
        gsftSubmit(null, g_form.getFormElement(), 'cancel'); // Replace 'cancel' with the name of Action Name of UI Action.
    }

}

if (typeof window == 'undefined')
    moveToCancel();
	
function moveToCancel() {
	// write the lines of code in your screenshot 
        // Line 4 to 9.
    
}

 


Script Include.
Name: Community
Client callable : true or GlideAjax Enabled: true

 

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

	userGroup : function(){
		if( gs.getUser().isMemberOf('c6c828c453751300ba3dddeeff7b1219')){ // sys_id of the group.
			gs.log('Part of group');
			return 'true';
		}else{
			gs.log('Not part of group');
			return 'false';
		}
	},

    type: 'Community'
});

 

 
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

Hi Najmuddin,

 

Thanks for the code. Can I do it using a client script and script include and let the UI action as it is?
I do not want to touch the UI action and instead create a script include and a client script

script include :

 

var xetChange = Class.create();
xetChange.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUser : function(){
    return gs.getUser().isMemberOf('c6c828c453751300ba3dddeeff7b1219');
},
    type: 'xetChange'
});


Client Script : Can you please help me with the client script? Should it be OnChange client script?

function onLoad() {
    var ajax = new GlideAjax('global.xetChange');
    ajax.addParam('sysparm_name', 'getUser');
    ajax.getXML(parseanswer);

    function parseanswer(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == 'true') {
            //do nothing
        }
        if ((g_form.getValue('state') == '-2' || g_form.getValue('state') == '-1' || g_form.getValue('state') == '0') && answer == 'false') {
            g_form.setReadOnly('work_start', true);
			g_form.setMandatory('start_date', false);



Thanks