How to hide/display field based on the user location in Record producer?

Deepika61
Tera Contributor

Hi All,

I have a requirement Actually in record producer,  i am using a Variable set , in that , three fields, " Selected user(Reference field),  type of leave and time of period ", So here the field 'leave type' was only visible , when the user(Selected user field )with location( particularly state  is one of WA or SA)?

 

Please help me to solve this

 

Thanks

Deepika

1 ACCEPTED SOLUTION

@Deepika61 

your script include is not client callable

Script include

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

	getLocation: function() {
		var userID = this.getParameter('sysparm_user'); 
		var returnID = '';
		var user = new GlideRecord('sys_user');
		if(user.get(userID)){
			return user.location.state; 
		}
	},

	type: 'Locationbased'
});

AnkurBawiskar_0-1696341278051.png

Client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	if(oldValue != newValue){
		//Type appropriate comment here, and begin script below
		var ga = new GlideAjax('Locationbased');
		ga.addParam('sysparm_name', 'getLocation'); 
		ga.addParam('sysparm_user', newValue); 
		ga.getXMLAnswer(function(answer){
			alert(answer);
			answer = answer.toString();
			if (answer == 'WA' || answer == 'SA')
				g_form.setDisplay('type_of _period', true);
			else {
				g_form.setDisplay('type_of _period', false);
			}
		});
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

Deepika61
Tera Contributor

@Ankur Bawiskar can you please help me to solve this.

 

Thanks

Deepika

@Deepika61 

so what did you start with and where are you stuck?

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

@Ankur Bawiskar i have written this code but its not working

Script include

 

var Locationbased = Class.create();
Locationbased.prototype = {
    //initialize: function() {},
    getLocation: function() {
        var userID = this.getParameter('sysparm_user'); 
        var returnID = '';
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', userID);
        user.query();
        if (user.next()) {
            return user.location.state; 
        }
    },

    type: 'Locationbased'
};
 
Client script;
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('Locationbased');
    ga.addParam('sysparm_name''getLocation'); 
    ga.addParam('sysparm_user', newValue); 
    ga.getXML(getInfo);

    function getInfo(response) {
        var info = response.responseXML.documentElement.getAttribute('answer');
        if (info == 'WA' || info == 'SA')
            g_form.setDisplay('type_of _period'true);

        else {
            g_form.setDisplay('type_of _period'false);
        }

    }
}

It looks like maybe your Script Include does not have the Client callable box checked as the second line should be:

Locationbased.prototype = Object.extendsObject(AbstractAjaxProcessor, {

You can add alerts to the Client Script and gs.info lines to the Script Include to confirm that it is running, the value passed in from the client, the result of the GlideRecord, and the value that will be returned to the client.

 

It's always best to return something to avoid errors, so consider changing the end of the SI to something more like this:

    if (user.next()) {
        returnID = user.location.state; 
    }
    return returnID;
    },
    type: 'Locationbased'
};