How to get unique values and set them into multiline text field

Sai18
Tera Expert

Requirement : I have a Lookup select box field called "Division" and based on the selection of the Division Value We have to autopopulate the "Secondary LDAP groups" into the "AD Group" field for their related records

 

But as per the screenshot provided, if you select ACQI as division in the corresponding Secondary LDAP Groups there were some duplicates also, So we need to remove those duplicate values and set the unique values into the "AD Group" field.

Sai18_0-1720175622879.png

Please help me out on this

2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

Hi @Sai18 ,

 

Basically you need to create a client script on change for division field

Call a script include where you need to Use the ArrayUtil class to remove duplicates from the "Secondary LDAP groups" and set the unique values in the "AD Group" field.

The script should look like below-

 Client-

 var ga = new GlideAjax('LDAPGroupProcessor');
    ga.addParam('sysparm_name', 'getUniqueLDAPGroups');
    ga.addParam('sysparm_division', newValue);
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            // Set the unique LDAP groups into the "AD Group" field
            g_form.setValue('ad_group', answer);
        }
    });

 

Script include-

var LDAPGroupProcessor = Class.create();
LDAPGroupProcessor.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUniqueLDAPGroups: function() {
        var division = this.getParameter('sysparm_division');
        var ldapGroups = [];
        var gr = new GlideRecord('your_table_name'); // Replace with your table name
        gr.addQuery('division', division);
        gr.query();
        while (gr.next()) {
            var groups = gr.getValue('secondary_ldap_groups').split(',');
            ldapGroups = ldapGroups.concat(groups);
        }

        // Remove duplicates
        var uniqueGroups = ArrayUtil.unique(ldapGroups);
        return uniqueGroups.join(',');
    }
});

 

Note- Please adjust the field names as per your instance.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

 

 

View solution in original post

Thanks for your help out on this

 

initially it wont work but I have updated script slightly and working fine

Client Script : 

var ga = new GlideAjax('AnthemCatalogAjaxGetReferenceUtils');
    ga.addParam('sysparm_name', 'getUniqueLDAPGroups');
    ga.addParam('sysparm_division', newValue);
    ga.getXML(callBackFun);
    function callBackFun(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('ad_groups', answer);
    }
 
Script include : (function)
 
getUniqueLDAPGroups: function() {
        var division = this.getParameter('sysparm_division');
        var ldapGroups = [];
        var gr = new GlideRecord('u_sas_project_sub_division_name');
        gr.addQuery('u_division', division);
        gr.query();
        while (gr.next()) {
            var groups = gr.getValue('u_secondary_ldap_groups').split(',');
            ldapGroups = ldapGroups.concat(groups);
           
        }

        // Remove duplicates
        var arrayUtil = new ArrayUtil();
        var resUniq = arrayUtil.unique(ldapGroups);
        gs.log('check ldap groups' +resUniq);
        return resUniq.join(', ');
       
    },

View solution in original post

3 REPLIES 3

Kristen Ankeny
Kilo Sage

I would leverage ArrayUtil and the unique function as a way to handle gathering data that might have duplicates and de-duplicating it. 

Community Alums
Not applicable

Hi @Sai18 ,

 

Basically you need to create a client script on change for division field

Call a script include where you need to Use the ArrayUtil class to remove duplicates from the "Secondary LDAP groups" and set the unique values in the "AD Group" field.

The script should look like below-

 Client-

 var ga = new GlideAjax('LDAPGroupProcessor');
    ga.addParam('sysparm_name', 'getUniqueLDAPGroups');
    ga.addParam('sysparm_division', newValue);
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            // Set the unique LDAP groups into the "AD Group" field
            g_form.setValue('ad_group', answer);
        }
    });

 

Script include-

var LDAPGroupProcessor = Class.create();
LDAPGroupProcessor.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUniqueLDAPGroups: function() {
        var division = this.getParameter('sysparm_division');
        var ldapGroups = [];
        var gr = new GlideRecord('your_table_name'); // Replace with your table name
        gr.addQuery('division', division);
        gr.query();
        while (gr.next()) {
            var groups = gr.getValue('secondary_ldap_groups').split(',');
            ldapGroups = ldapGroups.concat(groups);
        }

        // Remove duplicates
        var uniqueGroups = ArrayUtil.unique(ldapGroups);
        return uniqueGroups.join(',');
    }
});

 

Note- Please adjust the field names as per your instance.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

 

 

Thanks for your help out on this

 

initially it wont work but I have updated script slightly and working fine

Client Script : 

var ga = new GlideAjax('AnthemCatalogAjaxGetReferenceUtils');
    ga.addParam('sysparm_name', 'getUniqueLDAPGroups');
    ga.addParam('sysparm_division', newValue);
    ga.getXML(callBackFun);
    function callBackFun(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('ad_groups', answer);
    }
 
Script include : (function)
 
getUniqueLDAPGroups: function() {
        var division = this.getParameter('sysparm_division');
        var ldapGroups = [];
        var gr = new GlideRecord('u_sas_project_sub_division_name');
        gr.addQuery('u_division', division);
        gr.query();
        while (gr.next()) {
            var groups = gr.getValue('u_secondary_ldap_groups').split(',');
            ldapGroups = ldapGroups.concat(groups);
           
        }

        // Remove duplicates
        var arrayUtil = new ArrayUtil();
        var resUniq = arrayUtil.unique(ldapGroups);
        gs.log('check ldap groups' +resUniq);
        return resUniq.join(', ');
       
    },