show/hide section based on assignment group name

archie5
Tera Expert

I want to make a section visible on the form based on assgn group name. Let's say I have 2 groups A and B, and two sections with same name A and B
Section A should be visible only when group A is selected, B section only when Group B is selected and so on
Section name will be same as group name. How to not hard code this so that this works even when more groups and sections (of same names) are added. Thank you!

13 REPLIES 13

Kiran_45
Giga Guru

Hello @archie5 ,

 

If I'm not wrong you are trying to show the section based on the AG group name, so inorder to do that follow these steps.

1) Use on change client script call Glide Ajax, pass the assignment group value to script include.

2) In script include glide to group table with the passed sys_id, return the group name to client script.

3) if the group name matches with your section name you can use the returned value.

 

 

Hope it helps!!. Please hit thumbs up.

 

Regards,

Kiran

I wrote the scripts as you mentioned. Looks like there is some error as I'm getting null in answer and undefined in sections[i]. Can you help me please?

Here is the script include:

var GroupUtils = Class.create();
GroupUtils.prototype = {
  initialize: function() {},

  getGroupName: function(groupId) {
    var groupGR = new GlideRecord('sys_user_group');
    if (groupGR.get(groupId)) {
      return groupGR.getValue('name');
    }
    return '';
  },

  type: 'GroupUtils'
};

Also, here is the client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var sections = g_form.getSectionNames();
    g_form.addInfoMessage('in' + sections.toString());

    var assignmentGroupId = (g_form.getDisplayBox('assignment_group').value);

    g_form.addInfoMessage('first' + assignmentGroupId);

    var ga = new GlideAjax('GroupUtils');
    ga.addParam('sysparm_name', 'getGroupName');
    ga.addParam('sysparm_group_id', assignmentGroupId);
    ga.getXML(function(response) {
        var groupName = response.responseXML.documentElement.getAttribute('answer');
        if (groupName !== '') {
            var sectionNames = ['Tax', 'Biz Tools', 'Sales Ops Systems']; {

                if (sectionNames.includes(groupName)) {


                    // Show the sections
                    g_form.addInfoMessage('hello' + sectionNames[i]);
                    for (var i = 0; i < sectionNames.length; i++) {
                        g_form.setSectionDisplay(sectionNames[i], true);
                    }
                } else {
                    // Hide the sections
                    g_form.addInfoMessage('bye' + groupName);
					g_form.addInfoMessage('bye' + sectionNames[i]);
                    for (var j = 0; j < sectionNames.length; j++) {
                        g_form.setSectionDisplay(sectionNames[i], false);
                    }
                }
            }
        }
    });
}

Also, the section names will change with a '_' in b/w like biz_tools. So how will I check those?

@Ankur Bawiskar can you please help here?

From your code. 

var assignmentGroupId = (g_form.getDisplayBox('assignment_group').value);

Assignment group is a reference field. Why not do g_form.getValue(‘assignment_group’)  it will return a sys_id

 

Is your groupId client side getting back to you script include? What is your script include returning, is it returning anything. Add some gs.info() messages to your script include

 

and use getXMLAnswer as it it much easier to use and far more efficient. 

https://www.servicenow.com/community/developer-articles/getxmlanswer-vs-getxml/ta-p/2307589