Auto populate group description along with name of the group

Balaram7
Kilo Sage

Hi @everyone,

 

We have a requirement to auto populate group name along with the group description.

In service catalog there are two variables named Group Name (List Collector) and Group info (Multiline text).

I tried to populate using Onchange client script and Script includes, But unable to populate the data can anyone please help me on how to populate the data in the group description variable. 

Below is the script includes 

var PopulateGroupDescription = Class.create();
PopulateGroupDescription.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getGroups: function() {

        var results = [];
        var grp = this.getParameter('sysparm_grp');
        var gr = new GlideRecord('sys_user_group');
        gr.addQuery('sys_id', grp);
        gr.query();
        while (gr.next()) {
            results.push(gr.getValue('sys_id'));

        }
        return JSON.stringify(results);
    },

    type: 'PopulateGroupDescription'
});
 
 
Onchange client script:
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('PopulateGroupDescription');
    ga.addParam('sysparm_name', 'getGroups');
    ga.addParam('sysparm_grp', newValue);
    ga.getXML(parseData);

    function parseData(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        var responseData = JSON.parse(answer);
        var seperate = responseData.split(','); // "answer" is a comma seperated string
        for (var i = 0; i < seperate.length; i++) {
            g_form.setValue("group_info", seperate[i]);

        }
    }
}
1 ACCEPTED SOLUTION

Balaram7
Kilo Sage

I have updated the script this way to populate the data.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
  if (newValue!=''){
    // Get the selected values from the list collector
    var selectedGroups = g_form.getValue('group_name');

    if (selectedGroups) {
        var ga = new GlideAjax('PopulateGroupDescription');
        ga.addParam('sysparm_name', 'getGroups');
        ga.addParam('sysparm_grp_ids', selectedGroups);
        //ga.getXML(parseData);
    }

   
        ga.getXMLAnswer(function(response) {
            var data = JSON.parse(response);
 
function formatJsonArray(jsonArray) {
    return jsonArray
        .map(obj => `Group name: ${obj.name}\nGroup Description: ${obj.description_1}`)
        .join('\n\n');
}
 
var formattedData = formatJsonArray(data);
   
        g_form.setValue("group_description",formattedData);
        });
  }
    }

View solution in original post

2 REPLIES 2

Abhay Kumar1
Giga Sage

@Balaram7 Modify the Script Include to return the group name and description instead of just the sys_id.

And update the Client Script to parse the description and display it in the "Group info" variable.

Hope this will help you.

Balaram7
Kilo Sage

I have updated the script this way to populate the data.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
  if (newValue!=''){
    // Get the selected values from the list collector
    var selectedGroups = g_form.getValue('group_name');

    if (selectedGroups) {
        var ga = new GlideAjax('PopulateGroupDescription');
        ga.addParam('sysparm_name', 'getGroups');
        ga.addParam('sysparm_grp_ids', selectedGroups);
        //ga.getXML(parseData);
    }

   
        ga.getXMLAnswer(function(response) {
            var data = JSON.parse(response);
 
function formatJsonArray(jsonArray) {
    return jsonArray
        .map(obj => `Group name: ${obj.name}\nGroup Description: ${obj.description_1}`)
        .join('\n\n');
}
 
var formattedData = formatJsonArray(data);
   
        g_form.setValue("group_description",formattedData);
        });
  }
    }