- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2024 02:03 AM
Hi All,
I have a requirement like the record producer fields should be auto filled based on the first two field selection.
E.g. I have to two field business function and business process so once if user select those fields and if already there is a record existed with same business function and business process in the table then all the remaining fields should be auto populated same as already existing record how to do this if anyone has pointers and scripts for this, please do share.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2024 11:54 PM
Hi @Kruthik M Shiva,
Hope you are doing well and Glad to see that you are following my answers and solutions and happy to assist you.
Proposed Solution
As a solution, you need to modify above script as same as mentioned below and I tried it on my Personal Developer Instance to get the desired outputs. Hope this will work for you now and let me know if any other help needed. Attaching screenshots for the reference.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_form.getValue('u_category') != '') {
if (newValue != '') {
var val = confirm('If you want to copy same fields?');
if (val) {
var ga = new GlideAjax('global.AutoPopulateIncidentDetails');
ga.addParam('sysparm_name', 'incidentDetails');
ga.addParam('sysparm_category', g_form.getValue('u_category'));
ga.addParam('sysparm_sub_category', newValue);
ga.getXML(populateDetails);
function populateDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var ans = JSON.parse(answer);
g_form.setValue('short_description', ans[0]);
g_form.setValue('state', ans[1]);
}
}
else{
alert('You cancel the Auto Population Fields Request.');
}
}
} else {
alert('Kindly fill category first.');
g_form.setValue('u_sub_category', '');
}
}
Sample Outputs: -
After Pressing Ok: - Fields will be auto populated.
After Pressing Cancel: - Field will not be auto populate and 1 alert message will be displayed.
If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.
Thanks ‌‌:)
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2024 04:47 AM
Hello @Kruthik M Shiva ,
write onchange client on both fields.
please refer below script for your reference.
Thanks.
Script include:
var AutoFillFieldsScriptInclude = Class.create();
AutoFillFieldsScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getExistingRecordData: function(businessFunction, businessProcess) {
var existingRecordData = {};
var existingRecord = new GlideRecord('your_table_name');
existingRecord.addQuery('business_function', businessFunction);
existingRecord.addQuery('business_process', businessProcess);
existingRecord.query();
if (existingRecord.next()) {
existingRecordData.field1 = existingRecord.getValue('field1');
existingRecordData.field2 = existingRecord.getValue('field2');
}
return existingRecordData;
}
});
Onchange client for both fields.
var businessFunction = g_form.getValue('business_function');
var businessProcess = g_form.getValue('business_process');
if (businessFunction && businessProcess) {
var ga = new GlideAjax('AutoFillFieldsScriptInclude');
ga.addParam('sysparm_name', 'getExistingRecordData');
ga.addParam('business_function', businessFunction);
ga.addParam('business_process', businessProcess);
ga.getXMLAnswer(function(response) {
if (response) {
var existingRecordData = JSON.parse(response);
g_form.setValue('field_to_auto_populate1', existingRecordData.field1);
g_form.setValue('field_to_auto_populate2', existingRecordData.field2);
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2024 05:39 AM
Hi @Kruthik M Shiva,
Hope you are doing well.
Proposed Solution
As a solution, I tried to implement your query or ask on my Personal Developer Instance to get the solution as soon as possible. For this, I refer to the "Incident" as a Table and "Category as Business Function" and SubCategory as Business Process" and tried to auto populate the value of "State and Short Description" Field on the "Record Producer" if any existing record found with the help of an "On-Change Catalog Client Script and Script Include". Kindly follow below scripts : -
Client Script: -
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_form.getValue('u_category') != '') {
var ga = new GlideAjax('global.AutoPopulateIncidentDetails');
ga.addParam('sysparm_name', 'incidentDetails');
ga.addParam('sysparm_category', g_form.getValue('u_category'));
ga.addParam('sysparm_sub_category', newValue);
ga.getXML(populateDetails);
function populateDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var ans = JSON.parse(answer);
g_form.setValue('short_description', ans[0]);
g_form.setValue('state', ans[1]);
}
} else {
alert('Kindly fill category first.');
g_form.setValue('u_sub_category', '');
}
}
Script Include: -
var AutoPopulateIncidentDetails = Class.create();
AutoPopulateIncidentDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
incidentDetails: function() {
var arr = [];
var category = this.getParameter('sysparm_category');
var subCategory = this.getParameter('sysparm_sub_category');
var gr = new GlideRecord("incident");
gr.addQuery("category", category);
gr.addQuery("subcategory", subCategory);
gr.query();
if (gr.next()) {
arr.push(gr.short_description.toString());
arr.push(gr.state.toString());
}
return JSON.stringify(arr);
},
type: 'AutoPopulateIncidentDetails'
});
For your reference, also attaching screenshots of the outputs that will give you better insights of how the scripts are working or the best thing will be to follow the solution and execute the scripts on your instance.
Client Script
Script Include
Record Producer
If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.
Thanks ‌‌:)
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2024 06:09 AM
Hi @AakashG2703 and @Abhijeet_Pawar Thanks for giving your time and quick response I will implement the same and accept solution once it is done, I will accept your solution.
Thanks&Regards,
Kruthik Shivaprasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2024 08:12 AM
Glad to hear that @Kruthik M Shiva. Hope our solutions will resolve your ask or query and let me know if any help needed.
If you find any information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.
Thanks ‌‌:)
Aakash Garg
ServiceNow Developer