Need assistance with Auto-Populating Parent Fields in service portal Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2023 01:31 PM - edited 09-16-2023 01:47 PM
Hi All,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2023 02:44 PM
What have you tried so far?
best would be for you to show what you tried and we can help you fix it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2023 10:23 AM
Hi @Anurag Tripathi ,
I used below client script and script include, it showing null alert.
//Catalog Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get the selected level3_organization value
var level3Org = g_form.getValue('level_3_organization');
if (level3Org) {
// Create a GlideAjax instance to call the server-side script
var ga = new GlideAjax('AutoPopulateDepartments'); // Script incldue name
ga.addParam('sysparm_name', 'getLevel2AndLevel1Orgs'); // Function name
ga.addParam('sysparm_level3_org', level3Org);
ga.getXML(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
alert("Answer from Server" + answer); // check if you are getting response from server
if (answer) {
var answerObj = JSON.parse(answer);
// Set the values for level2_organization and level1_organization
g_form.setValue('level_2_organization', answerObj.level2Org);
g_form.setValue('level_1_organization', answerObj.level1Org);
}
}
}
}
//Script Include
var AutoPopulateDepartments = Class.create();
AutoPopulateDepartments.prototype = {
initialize: function() {},
getLevel2AndLevel1Orgs: function() {
var level3Org = this.getParameter('sysparm_level3_org');
var level2Org = '';
var level1Org = '';
if (level3Org) {
var level3OrgGR = new GlideRecord('cmn_department');
if (level3OrgGR.get(level3Org)) {
// Get the parent organization (level2)
level2Org = level3OrgGR.parent;
// Check if there is a parent for level2 organization (level1)
if (level2Org) {
var level2OrgGR = new GlideRecord('cmn_department');
if (level2OrgGR.get(level2Org)) {
level1Org = level2OrgGR.parent;
}
}
}
}
// Return the parent organizations as JSON
var answerObj = {
level2Org: level2Org,
level1Org: level1Org
};
return JSON.stringify(answerObj);
},
type: 'AutoPopulateDepartments'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2023 02:58 PM
Hi @Priyanka1997,
According to the logic you described, you need a client script (on change) on the "level3_organization" field and a script include to get the parents of departament, that would be an option, shouldn't the first 2 fields be read only? Do I understand that the logic applies only if these 2 fields have not been completed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2023 10:15 AM
Hi @Alex Tod1 ,
Thanks for your reply!
The first two fields are not read only, If the end user knows only the 'level3_organization,' then i want to set up a script to automatically populate its parent in 'Level2' and 'Level2's' parent in 'Level1.' This way, if the user selects a value for 'level3_organization,' the script can fetch the corresponding parent organizations and fill in 'level2_organization' and 'level1_organization' fields accordingly.