Need assistance with Auto-Populating Parent Fields in service portal Catalog Item

Priyanka1997
Tera Contributor

Hi All,

I have a service portal Catalog Item with  following three variables, all of which are reference fields to the 'cmn_department' table: 'level1_organization', 'level2_organization', and 'level3_organization'. 
Priyanka1997_0-1694896355538.png
What I'm trying to achieve is that when an end user selects a value in the 'level3_organization' field (the most specific organization), I want the 'level2_organization' and 'level1_organization' fields to automatically populate with the parent departments of the selected 'level3_department'.

 

 
Here's the desired behavior:
- User selects a department in 'level3_organization'.
- The 'level2_organization' field should automatically populate with the parent department of the selected 'level3_department'.
- Similarly, the 'level1_organization' field should populate with the parent of the selected 'level2_department'.
 
I've attempted to implement this using GlideAjax and scripting, but I'm facing challenges in getting the auto-population to work as intended.
 
Can anyone provide guidance or share a script example to achieve this functionality in my catalog form? Your help would be greatly appreciated!
 
Thank you!
8 REPLIES 8

Anurag Tripathi
Mega Patron
Mega Patron

What have you tried so far?

 

best would be for you to show what you tried and we can help you fix it.

-Anurag

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'
};

Alex Tod1
Kilo Sage

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?

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.