Service Portal auto populate fields after user name selected

Moedeb
Tera Guru

I have a requirement for a couple of fields to be populated with the appropriate information once a customer adds the name of the 'owner' of a dashboard - that persons email and department needs to then be added to the appropriate fields.

I have checked a number of forum posts similar, tried most of them and still can't get it to work, so here I am asking directly.

find_real_file.png

So based on the Owner name being added (which shows as the first name and surname - eg: Joe Bloggs) - note: it is almost certainly not going to be the name of the user entering the details.

owner_section would map to the 'Department' field

owner_email would map to the 'email' field

I believe all are available through sys_user

Also as a side question - is it possible to allow free text in a reference field if someone needs to enter something that is not available to select? 

If someone could let me know how to do this I'd certainly be grateful.

1 ACCEPTED SOLUTION

Can you make that variable as reference and refer to cmn_department table.

View solution in original post

23 REPLIES 23

p t1
Kilo Sage
Kilo Sage

Hi,

This code is for based on requestor(sys_ user) autopulate first name and last name.. You can try for for what fields you want to get..

Script include,

var test1 = Class.create();
test1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
demo:function()
{
var user_obj = {};
var req=this.getParameter('sysparm_requestor');
var gr=new GlideRecord('sys_user');
if(gr.get(this.getParameter('sysparm_requestor')))
{
user_obj.first_name=gr.getValue('first_name');
user_obj.last_name=gr.getValue('last_name');
}
var user_json = new JSON();
var user_oData = user_json.encode(user_obj);
return user_oData;
},

type: 'test1'
});

Catalog client script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var requestor=g_form.getValue('v_requestor');
var ga=new GlideAjax('global.test1');
ga.addParam('sysparm_name','demo');
ga.addParam('sysparm_requestor',requestor);
ga.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
var myObj = JSON.parse(answer);
g_form.setValue('v_firstname', myObj.first_name);
g_form.setValue('v_lastname', myObj.last_name);
}
}

 

Thanks

ok, happy to try but sorry don't understand where I put the first part - Script include,

 

Also presumably with the catalog client script part I would be doing:

Type: onChange

Catalog item: (the one I'm doing this for)

Variable name: owner_name

 

?

Hi,

1.Go to navigator>>system definition>>script include

2.Instead of requester use your owner_field

Onchange variable: owner_name

instead of first name and last name get email and department from sys_user table.

Thanks

Ok thanks, I can now find where the script include is, but honestly I don't know where to replace what in your script - sorry obviously I'm a bit simple with this stuff, incorrectly presumed this would be pretty simple to do.

 

script Incude:

Client callable - checked

Accessible from - All application scopes

var test1 = Class.create();
test1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 demo:function()
 {
 var user_obj = {};
 var req=this.getParameter('sysparm_owner_name');
 var gr=new GlideRecord('sys_user');
 if(gr.get(this.getParameter('sysparm_owner_name')))
 {
 user_obj.email=gr.getValue('owner_email');
 user_obj.department=gr.getValue('owner_section');
 }
 var user_json = new JSON();
 var user_oData = user_json.encode(user_obj);
 return user_oData; 
 },


type: 'test1'
});

 

Followed by the catalog client script:

and am completely lost where to reference my fields in the here!!!

Hi,

 

var test1 = Class.create();
test1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
demo:function()
{
var user_obj = {};
var req=this.getParameter('sysparm_owner_name');
var gr=new GlideRecord('sys_user');
if(gr.get(this.getParameter('sysparm_owner_name')))
{
user_obj.email=gr.getDisplayValue('email');//in sys_user table email field
user_obj.department=gr.getDisplayValue('department');//in sys_user table department field
}
var user_json = new JSON();
var user_oData = user_json.encode(user_obj);
return user_oData;
},

type: 'test1'
});

catalog client script

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var owner=g_form.getValue('owner_name');
var ga=new GlideAjax('global.test1');
ga.addParam('sysparm_name','demo');
ga.addParam('sysparm_owner_name',owner);
ga.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
var myObj = JSON.parse(answer);
g_form.setValue('owner_email', myObj.email);//enter correct field variables
g_form.setValue('owner_section', myObj.department);
}
}

 

Thanks