Auto populating email id using glide Ajax.

VinuvarshitSR
Giga Expert

I have 2fileds one is list for responsible group which is a list type and the other one is responsible dl i would like to auto populate dl if the user select the groups from the list using glide ajax i have written script but it's not working

Script include

Script include
var ResponsibleTeamHandler = Class.create();
ResponsibleTeamHandler.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    fetchGroupEmails: function() {
        var teamIds = this.getParameter('sysparm_teams'); 
        if (!teamIds) {
            return '';
        }

        var teamEmails = [];
        var teamIdsArray = teamIds.split(','); 
        var gr = new GlideRecord('sys_user_group'); 
        gr.addQuery('sys_id', 'IN', teamIdsArray);
        gr.query();

        while (gr.next()) {
            if (gr.email) {
                teamEmails.push(gr.email.toString());
            }
        }

        return teamEmails.join(', '); 
    },

    type: 'ResponsibleTeamHandler'
});

 

Client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga = new GlideAjax('ResponsibleTeamHandler');
    ga.addParam('sysparm_name', 'fetchGroupEmails');
    ga.addParam('sysparm_teams', newValue);
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_responsible_team_dls', answer);
    });

    // Add any additional code required here
}

Kindly help in resolving the issue

1 ACCEPTED SOLUTION

This worked and Thanks for the support I changed a few. @Viraj Hudlikar @NagaChandaE and @Ankur Bawiskar 
here is the code

Script include
var ResponsibleTeamHandler = Class.create();
ResponsibleTeamHandler.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    fetchGroupEmails: function() {
        var teamIds = this.getParameter('sysparm_teams');
        if (!teamIds) {
            return '';
        }

        var teamEmails = []; 
        var gr = new GlideRecord('sys_user_group');
        gr.addQuery('sys_id', 'IN', teamIds);
        gr.query();
        while (gr.next()) {
            if (gr.email) {
                teamEmails.push(gr.email.toString());
            }
        }
        return teamEmails.join(', ');
    },

    type: 'ResponsibleTeamHandler'
});

 

Client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

    var ga = new GlideAjax('ResponsibleTeamHandler');
    ga.addParam('sysparm_name', 'fetchGroupEmails');
    ga.addParam('sysparm_teams', g_form.getValue('u_responsible_teams')); 
    ga.getXMLAnswer(function(answer) {
        g_form.setValue('u_responsible_team_dls', answer); // Set the response in the desired field
    });


    // Add any additional code required here

}

View solution in original post

14 REPLIES 14

Are you want to get group emails or user email

Group email id @NagaChandaE 

 

Script include
var ResponsibleTeamHandler = Class.create();
ResponsibleTeamHandler.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    fetchGroupEmails: function() {
        var teamIds = this.getParameter('sysparm_teams'); 
        if (!teamIds) {
            return '';
        }

        var teamEmails = [];
        var teamIdsArray = teamIds.split(','); 
        var gr = new GlideRecord('sys_user_group'); 
        gr.addQuery('sys_id', 'IN', teamIdsArray);
        gr.query();

        while (gr.next()) {
            if (gr.email) {
                teamEmails.push(gr.email.toString());
            }
        }

        return teamEmails.join(', '); 
    },

    type: 'ResponsibleTeamHandler'
});

 

I have changed the querying with sys_id instead group try this

 

Viraj Hudlikar
Giga Sage

Hello @VinuvarshitSR 
In your script did you tried to add log in script include if correct data is passed or not, also in client script add alert statement when answer is return and see what value is returned?

Also, the responsible dl field is of string type I believe.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Yes i have tried to debug it. I was not able to come up with solution that's why reaching out. Yes it is a string field
Thank you - Viraj