Record producer - Populate field using reference qualifier

Bart15
Giga Contributor

Hi community,

 

I'm on a lost concerning how to populate a reference field on a record producer.
I've searched in the communities and found many posts concerning this and tried to follow some of them but nothing seems to work. 

 

Situation:

I've got a record producer that will create an incident.
On this record producer I have 2 variables, both are reference fields, that are 'map to field'.

The first variable:

Question: Requested by

Field: Requested by

Label on incident form: u_requested_by

Reference: sys_user

Default value: javascript:gs.getUserID();

This field is made invisible via an UI Policy since this will always be the current user.

 

Second variable:

Question: Registered for

Field: Caller

Label on incident form: caller_id

Reference: sys_user

 

What I'm trying to achieve is that 'requested for' is populated with users of the same company as the user in 'requested by'. 

 

I've learned that I need to use a script include for this and with some examples tried to create a script (I'm a complete scripting Newby) but cannot get it to work.

I've also learned that I should call on that script in the advanced reference qualifier of Registered for.

I've tried something like: javascript:new getCompanyCurrentRequestedBy() and also javascript:new getCompanyCurrentRequestedBy().getCompany() but both do not work.

 

The script include I've created:

 

var getCompanyCurrentRequestedBy = Class.create();
getCompanyCurrentRequestedBy.prototype = {
    initialize: function() {
    },
CompanyCurrentRequestedBy: function getCompanyCurrentRequestedBy(){ 
		var usrsList = ' ';
		var cmpny = current.variables.u_requested_by;
		var usr = new GlideRecord('sys_user');
		usr.addQuery('company',cmpny);
		usr.query();
		while(usr.next()) {
			if (usrsList.length > 0) {
			//build a comma separated string of groups if there is more than one
				usrsList += (',' + usr.sys_id);
			}
			else {
			usrsList = usr.sys_id;
			}
		}
		// return a list of sys_user id's for the selected company
		return 'sys_idIN' + usrsList;
	},
 type: 'getCompanyCurrentRequestedBy'
};

Can somebody please help me to achieve the goal of populating 'requested for' with only users of the same company as the user in 'requested by'?

1 ACCEPTED SOLUTION

Bart15
Giga Contributor

Thank you guys for the replies. Because of some system difficulties I could not reply or update yesterday but I did manage to solve the issue.

On the reference field I placed the advanced qualifier: javascript: new getCompanyCurrentRequestedBy().getUserList();

The script include I've used:

var getCompanyCurrentRequestedBy = Class.create();
getCompanyCurrentRequestedBy.prototype = {
    initialize: function() {
    },
    getUserList: function () {
        var grUser = new GlideRecord('sys_user');
        grUser.get(gs.getUserID());
        var company = grUser.getValue('company');
        var query = '';
        query += 'active=true';
        query += '^company=' + company;
        query += '^EQ';
        return query;
    },
    type: 'getCompanyCurrentRequestedBy'
};

View solution in original post

3 REPLIES 3

Kalaiarasan Pus
Giga Sage

Add this to reference qualifier and see if it works
javascript:'active=true^company='+current.variables.u_requested_by.company

If you want to also exclude the user who was selected in requested by
javascript:'active=true^sys_id!='+current.variables.u_requested_by+'^company='+current.variables.u_requested_by.company

Joel Millwood2
Kilo Guru

Hi Bart,

imkalai beat me to it, in this case you can do it without a script include.

If you do have cause to use a script include in the future as a reference qualifier you would have to change what you have to:

javascript: new getCompanyCurrentRequestedBy().CompanyCurrentRequestedBy(queryValue); and pass in the value you want to query on. (new ScriptIncludeName().functionName(value)).

For example:

Reference qualifier: 

javascript: new userUtils().getCompanyQuery(current.variables.caller_id);

Script Include:

var userUtils = Class.create();
userUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCompanyQuery: function(user) {
        var userRec = new GlideRecord('sys_user');
        userRec.get(user); // get record for sys_id of user passed in
        return 'company=' + userRec.getValue('company');
    },

    type: 'userUtils'
});

Hopefully the above example helps you out in future when you have a use case for using a script include as a reference qualifier. If you want me to explain any of the above, please don't hesitate to ask.

 

 

Bart15
Giga Contributor

Thank you guys for the replies. Because of some system difficulties I could not reply or update yesterday but I did manage to solve the issue.

On the reference field I placed the advanced qualifier: javascript: new getCompanyCurrentRequestedBy().getUserList();

The script include I've used:

var getCompanyCurrentRequestedBy = Class.create();
getCompanyCurrentRequestedBy.prototype = {
    initialize: function() {
    },
    getUserList: function () {
        var grUser = new GlideRecord('sys_user');
        grUser.get(gs.getUserID());
        var company = grUser.getValue('company');
        var query = '';
        query += 'active=true';
        query += '^company=' + company;
        query += '^EQ';
        return query;
    },
    type: 'getCompanyCurrentRequestedBy'
};