Add groups to variable field through automation.

Atchutaram
Tera Contributor

Hi Everyone.

I have a requirement where i need to add groups of one variable to other variable in a catalog item.

 

We have one variable called AD group, and other variable Company groups. This company groups is a list collector variable it has reference to a company group table. In every record company group table there is one variable called Org groups. When we select any company group record , the data in that org groups field of that particular company group  should be appended to AD group variable. How can i achieve this?

 

Thank you in advance.

8 REPLIES 8

Hi

 

Function name has to be what you provide in sysparm_name, in your case function name is different in script include GetAADValues.

Also try to add logs to check where your code is creating issues 

 

Thanks!

Ankur Bawiskar
Tera Patron
Tera Patron

@Atchutaram 

try this

var GetOrgValues = Class.create();
GetOrgValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getOrgValues: function() {
        var ids = this.getParameter('sysparm_ids');
        if (!ids) return '[]';
        var idArray = ids.split(',');
        var result = [];
        var gr = new GlideRecord('u_company_group');
        gr.addQuery('sys_id', 'IN', idArray);
        gr.query();
        while (gr.next()) {
            var aad = gr.getValue('u_org_group');
            if (aad) {
                result.push(aad);
            }
        }
        return JSON.stringify(result);
    }
});

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var selectedIDs = g_form.getValue('u_company_groups');
    if (!selectedIDs) return;

    var currentADGroup = g_form.getValue('vo_ad_groups');

    var ga = new GlideAjax('GetOrgValues');
    ga.addParam('sysparm_name', 'getOrgValues');
    ga.addParam('sysparm_ids', selectedIDs);
    ga.getXMLAnswer(function(response) {
        if (!response) return;
        var aadvalues = JSON.parse(response);
        var newADGroupValue = currentADGroup;
        if (aadvalues.length > 0) {
            // Remove duplicates if needed
            var existing = currentADGroup ? currentADGroup.split(',') : [];
            var combined = existing.concat(aadvalues);
            // Remove empty strings and duplicates
            var unique = Array.from(new Set(combined.filter(Boolean)));
            newADGroupValue = unique.join(',');
        }
        g_form.setValue('vo_ad_groups', newADGroupValue);
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , i have tried this and i am getting error like "there is java script error in your browser".

I am getting an error when i try to fill company group variable.

 

BR

@Atchutaram 

sorry but you didn't share what debugging did you perform?

share screenshots of your form, variable configuration etc

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader