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.

Populating dl using glide Ajax with another table

VinuvarshitSR
Giga Expert

I am trying to do it with Glide ajax

1. case Table Fields:

  • Responsible Team (u_responsible_teams): This field is a reference to the sys_user_group table. It stores the team name (as referenced from the sys_user_group table).
  • Priority (priority): This field stores the priority for the selected responsible team (integer type).
  • Responsible Team DL (u_responsible_team_dls): This field will store the email addresses (comma-separated) retrieved based on the selected responsible team and priority.

2. u_dl_u_responsible_dl_lookup Table Fields:

  • Responsible Team (u_responsible_team): This is a reference field, where the team name is stored. It corresponds to the u_responsible_teams field in the u_case table. both refer to group table
  • Priority (u_priority): This is a string field that stores the priority value. This field must match the priority in the u_case table.
  • Responsible DL (u_responsible_dl): This is a string field that stores the email address associated with the team for the given priority. This email needs to be fetched and populated in the u_responsible_team_dls field in the u_case table.

3. Objective:

  • When a user selects a responsible team in the u_case table, the system should check for the priority and match it with records in the u_dl_u_responsible_dl_lookup table.
  • The system should then retrieve the email addresses (u_responsible_dl) from the u_dl_u_responsible_dl_lookup table for that team and priority.
  • The emails should be returned as a comma-separated string and populated in the u_responsible_team_dls field in the u_case table.

 

 

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

    // Function to fetch email addresses based on team names and priority
    fetchGroupEmails: function() {
        // Get the comma-separated team names and the priority from the client script
        var teamNames = this.getParameter('sysparm_teams'); // The responsible team names passed from client
        var priority = this.getParameter('sysparm_priority'); // The priority passed from client

        // If either team names or priority is not provided, return an empty string
        if (!teamNames || !priority) {
            return ''; // Return empty if no team names or priority are provided
        }

        // Convert the comma-separated team names string into an array
        var teamNamesArray = teamNames.split(','); // Convert the team names into an array

        var teamEmails = []; // Array to store email addresses

        // Create a GlideRecord query on the u_dl_u_responsible_dl_lookup table
        var gr = new GlideRecord('u_dl_u_responsible_dl_lookup');
        gr.addQuery('u_priority', priority); // Match the priority field
        gr.addQuery('u_responsible_team', 'IN', teamNamesArray); // Match the team names field
        gr.query(); // Execute the query

        // Loop through the records and collect the email addresses
        while (gr.next()) {
            if (gr.u_responsible_dl) {
                teamEmails.push(gr.u_responsible_dl.toString()); // Add the email to the array
            }
        }

        // Return the email addresses as a comma-separated string
        return teamEmails.join(', '); // Join the email addresses with commas and return
    },

    // Define the Script Include name
    type: 'ResponsibleTeamHandler'
});
​

 

 


 

 

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

    // Exit early if the form is loading or if there is no change in the value
    if (isLoading || newValue === '') {
        return;
    }

    // Get the value of the selected Responsible Teams (team name) and Priority
    var responsibleTeams = g_form.getValue('u_responsible_teams'); // The Responsible Team field value
    var priority = g_form.getValue('priority'); // The Priority field value

    // Use GlideAjax to call the Script Include
    var ga = new GlideAjax('ResponsibleTeamHandler');
    ga.addParam('sysparm_name', 'fetchGroupEmails'); // The function name to call in the Script Include
    ga.addParam('sysparm_teams', responsibleTeams); // Send the responsible team names to the Script Include
    ga.addParam('sysparm_priority', priority); // Send the priority value to the Script Include

    // Execute the GlideAjax request and handle the response
    ga.getXMLAnswer(function(answer) {
        // Populate the Responsible Team DL field with the comma-separated email addresses returned by the Script Include
        g_form.setValue('u_responsible_team_dls', answer); // The emails returned by the Script Include will be populated here
    });

}

 

 

 

The dls are not populating.

17 REPLIES 17

@VinuvarshitSR 

I informed to make it reference type and not list type

Also clear the value in onChange

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
   g_form.clearValue('u_responsible_team_dls');
    var responsibleTeams = g_form.getValue('u_responsible_teams');
    var priority = g_form.getValue('priority');

    alert('Responsible Teams: ' + responsibleTeams);
    alert('Priority: ' + priority);

    var ga = new GlideAjax('ResponsibleTeamHandler');
    ga.addParam('sysparm_name', 'fetchGroupEmails');
    ga.addParam('sysparm_teams', responsibleTeams);
    ga.addParam('sysparm_priority', priority);

    ga.getXMLAnswer(function(answer) {
        alert('Emails returned: ' + answer);
        g_form.setValue('u_responsible_team_dls', answer);
    });
}

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  changed and tried but still not fetching the dls form the lookup table

@VinuvarshitSR 

What came in logs?

Did you debug?

please share the latest scripts and screenshots.

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

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

    fetchGroupEmails: function() {
        var teamNames = this.getParameter('sysparm_team');
        var priority = this.getParameter('sysparm_priority');

        if (!teamNames || !priority) {
            gs.info('No team names or priority provided v');
            return '';
        }

        var teamNamesArray = teamNames.split(',');
        var teamEmails = [];

        var gr = new GlideRecord('u_dl_u_responsible_dl_lookup');
        gr.addQuery('u_priority', priority);
        gr.addQuery('u_responsible_team', 'IN', teamNamesArray);
        gr.query();
        while (gr.next()) {
            if (gr.u_responsible_dl) {
                teamEmails.push(gr.u_responsible_dl.toString());
            }
        }

        gs.info('Emails found: ' + teamEmails.join(', '));
        return teamEmails.join(', ');
    },

    type: 'FetchResponsibleTeamDL'
});
Client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.clearValue('u_responsible_team_dls');
    var responsibleTeams = g_form.getValue('u_responsible_teams');
    var priority = g_form.getValue('priority');

    alert('Responsible Teams: ' + responsibleTeams);
    alert('Priority: ' + priority);

    var ga = new GlideAjax('FetchResponsibleTeamDL');
    ga.addParam('sysparm_name', 'fetchGroupEmails');
    ga.addParam('sysparm_teams', responsibleTeams);
    ga.addParam('sysparm_priority', priority);

    ga.getXMLAnswer(function(answer) {

        if (answer === null || answer === '') {
            alert('Error: No response received from FetchResponsibleTeamDL');
            return;
        }
        alert('Emails returned: ' + answer);
        g_form.setValue('u_responsible_team_dls', answer);
    });
    //Type appropriate comment here, and begin script below

}

1.PNG2.PNGNO DL from look upNO DL from look up

@VinuvarshitSR 

seems your query is not working and hence you are getting empty array

Did you try that?

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