Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

NagaChandaE
Kilo Sage

Hi @VinuvarshitSR 

 

While using getXMLAnswer you not need to write response you directly use the answer attribute below example.

var ga = new GlideAjax('QuickQuery'); 
  ga.addParam('sysparm_name', 'getField'); 
  ga.addParam('sysparm_table', 'x_hotel_guest'); 
  ga.addParam('sysparm_sys_id', newValue); 
  ga.addParam('sysparm_field', 'vip'); 
  ga.getXMLAnswer(function(answer) { 
    if (answer == 'true') { 
      g_form.showFieldMsg('guest', 'Guest is a VIP!'); 
    } 
  });

below i corrected your code

 

 

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(answer) {
      
        g_form.setValue('u_responsible_team_dls', answer);
    });

    // Add any additional code required here
}

 

 

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

 

Thanks & Regards
@NagaChandaE 

Thanks a lot for your solution

Hi @VinuvarshitSR 

 

Update me whether it solved the issue or not .

 

I have tried it but not working. it's fetching the sys id but not fetching the mail ids.