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

Siva Jyothi M
Mega Sage

Hi @Priyanka1997,

 

You will need to write a onchange client script on 'level3_organization' field and a script include to get the department data. Based on the logic, it would be better if  the level 1 and level2 fields are read-only.

Script Include:

var populateaddress = Class.create();
populateaddress.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    demo: function() {
        var level = this.getParameter('sysparm_company');
        var gr = new GlideRecord('organization_table');//replace with the correct table
        gr.addQuery('name', level);
        gr.query();
        if (gr.next()) {
 var gbsobj = {};
             gbsobj.dept2= gr.department_name_level2; //replace with the correct backend names
			gbsobj.dept1= gr.department_name_level1; //replace with the correct backend names
           return (JSON.stringify(gbsobj));
        }
    },
type: 'populateaddress'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('populateaddress');
    ga.addParam('sysparm_name', 'demo');
    var com = g_form.getValue('level3_organization');//replace with correct variable name
    ga.addParam('sysparm_company', com);
    ga.getXML(callBackFunction);
    function callBackFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		if (answer) {
            answer = JSON.parse(answer);
    g_form.setValue('leve2_variable', answer.dept2);
            g_form.setValue('leve1_variable', answer.dept1);
    }
}
}

Please mark this answer as correct and helpful if it solves your issue.

Regards,

Siva Jyothi M.

 

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Priyanka1997 ,

                                           Try to use below scipt.

OnChange Client Script : (triggers when level3_organization changes)

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   // Get the selected level3_organization value
   var level3Org = g_form.getValue('level3_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('level2_organization', answerObj.level2Org);
            g_form.setValue('level1_organization', answerObj.level1Org);
         }
      }
   }
}

Script Include : (Client Callable)

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

Kindly mark correct and helpful if applicable

Hi- @Chetan Mahajan ,

 

Thanks for your reply! 

 

 

I tried above script and It is showing null alert "Answer from Servernull"

Hello @Priyanka1997,

Please follow these steps to address the issue:

1. In your client script, modify the following line:
Change ga.addParam('sysparm_level3_org', level3Org);
to ga.addParam('sysparm_level3org', level3Org);

In your script include, modify this line:
Change var level3Org = this.getParameter('sysparm_level3_org');
to var level3Org = this.getParameter('sysparm_level3org');

2. Ensure that you have set the "Client Callable" checkbox to true on your Script Include.

3. Add the following line to your script include:
gs.info("Value from client script: " + level3Org);
This will help you verify whether the function is being triggered or not. You can check the `gs.info` output in the system logs under System Policy.

4. In the script include where you have the line "level2Org = level3OrgGR.parent; & level1Org = level2OrgGR.parent; " make sure that the "parent" make sure parent is field that referring to other department correct value based on your field. Make any necessary adjustments to ensure the correct reference.

Please make these changes and verify if the issue is resolved.

Kindly mark correct and helpful if applicable