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

@VinuvarshitSR  - So what are you receiving in your log or alert statements?

I am receiving the sys ids of the record but my script include is failing somewhere.

@VinuvarshitSR - You receiving sys_id of what and where you added logs? Don't just get panic or worried think what are you doing what is coming in what is going out. have some small break if you are stuck in between, it helps sometime.

 

Below is sample code I just ran on background script and see its output.

var listvalue = '019ad92ec7230010393d265c95c260dd,477a05d153013010b846ddeeff7b1225,d625dccec0a8016700a222a0f7900d06'; //this is sys_id of groups which will be coming from your client script

var teamEmails = [];
var grUser = GlideRecord('sys_user_group');
grUser.addQuery('sys_id','IN', listvalue);
grUser.query();
while(grUser.next()){
	if(grUser.email!=''){
		teamEmails.push(grUser.email.toString());
	}
}
gs.print(teamEmails.toString()); 
/*Output:
Test@example.com,Test2@example.com,service.desk@yourcompany.com
*/

 

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.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@VinuvarshitSR 

Are you sure your script include is client callable and getting called successfully from client script?

Is that list field referring to sys_user_group table?

Assuming answer to above questions are yes then try this once

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

    fetchGroupEmails: function() {
        var teamIds = this.getParameter('sysparm_teams');
        if (!teamIds) {
            return '';
        }

        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: Assuming it's triggering on change of list field

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('listField')); // give here the field name
    ga.getXMLAnswer(function(answer) {
        g_form.setValue('u_responsible_team_dls', answer);
    });

    // Add any additional code required here
}

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

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

}