- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2022 10:10 PM
Hello All,
I want to populate value based on field .
for ex: if I select employee all information related to employee will auto populate .
i wrote one code its working fine but one for single field .please check my once if I did any mistake while writing script .so please let me know.
SCRIPT INCLUDE :
var populatedata = Class.create();
populatedata.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData:function()
{
var para1=this.getParameter('sysparm_data');
var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id',para1);
gr.addNotNullQuery('name');
gr.query();
if(gr.next())
{
return gr.first_name;
}
if(gr.next())
{
return gr.last_name;
}
},
type: 'populatedata'
});
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga=new GlideAjax('populatedata');
ga.addParam('sysparm_name','getData');
ga.addParam('sysparm_data',newValue);
ga.getXML(populateGroup);
function populateGroup(response)
{
var answer=response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('firstname',answer);
}
}
Thank You.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2022 11:12 PM - edited ‎12-21-2022 11:13 PM
Hi Abhi,
Please check the below script to know about how to return multiple values from Script include to Client script using JSON.
OnChange Client Script on user field :
var abc = new GlideAjax("Script Include Name");
abc.addParam("sysparm_name", 'FunctionName');
abc.addParam("sysparm_userID", newValue);
abc.getXML(Hello);
function Hello(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
g_form.setValue("email", answer.firstName); //firstName is the obj name stored in Script include
g_form.setValue("department", answer.lastName);
g_form.setValue("manager", answer.Manager);
}
}
Client - Callable Script include:
var Autopopulate = Class.create();
Autopopulate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
myfunction: function(){
var name = {}; // to store multiple values
var usr = this.getParameter('sysparm_userID');
var tab = new GlideRecord("sys_user");
tab.addQuery("sys_id",usr);
tab.query();
if(tab.next()){
name.firstName = tab.getValue("first_name");
name.lastName = tab.getValue("last_name");
name.Manager = tab.getDisplayValue("manager");
}
return JSON.stringify(name);
},
type: 'Autopopulate'
});
In this way you can return multiple values using JSON.
Jus add data to the object. in above case it is "name". And call it in client script.
Please feel free to ask incase of any queries from the above script.
Please mark it as correct if it resolved your query.
Regards,
Deepika
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2022 06:43 AM
Hi @Sagar_pawar ,
If you are trying on catalog you don't required any script to populate first name and last name.
You can use Catalog Data Lookup Definitions.
Or Try below scripts it will work.
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('populatedata'); //scipt include name
ga.addParam('sysparm_name', 'getData'); //function name
ga.addParam('sysparm_data', newValue); //parameter pass to server
ga.getXMLAnswer(populateGroup);
function populateGroup(response) {
var parseval = JSON.parse(response);
g_form.setValue('firstname', parseval.userfirstname);
g_form.setValue('lastname', parseval.userlastname);
}
}
Script Include: Need to check the client callable true.
var populatedata = Class.create();
populatedata.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData: function() {
var sysid = this.getParameter('sysparm_data');
var obj = {};
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id', sysid);
usr.query();
if (usr.next()) {
obj.userfirstname = usr.first_name.getDisplayValue();
obj.userlastname = usr.last_name.getDisplayValue();
}
return JSON.stringify(obj);
},
type: 'populatedata'
});
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2022 08:15 AM
Yes, its working @Deepika18 Thanks a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2022 10:43 PM
Hi ,
Instead of returning one value why don't you return the whole info of the user then manipulate it in the client script .
Regards,
Edgar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2022 06:43 AM
Hi @Sagar_pawar ,
If you are trying on catalog you don't required any script to populate first name and last name.
You can use Catalog Data Lookup Definitions.
Or Try below scripts it will work.
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('populatedata'); //scipt include name
ga.addParam('sysparm_name', 'getData'); //function name
ga.addParam('sysparm_data', newValue); //parameter pass to server
ga.getXMLAnswer(populateGroup);
function populateGroup(response) {
var parseval = JSON.parse(response);
g_form.setValue('firstname', parseval.userfirstname);
g_form.setValue('lastname', parseval.userlastname);
}
}
Script Include: Need to check the client callable true.
var populatedata = Class.create();
populatedata.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData: function() {
var sysid = this.getParameter('sysparm_data');
var obj = {};
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id', sysid);
usr.query();
if (usr.next()) {
obj.userfirstname = usr.first_name.getDisplayValue();
obj.userlastname = usr.last_name.getDisplayValue();
}
return JSON.stringify(obj);
},
type: 'populatedata'
});
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2022 08:26 AM
Hi @Sagar_pawar ,
have you tried my response?
ServiceNow Community MVP 2024.
Thanks,
Pavankumar