return users from the same company for ref var

Ellie5
Tera Contributor

Hi All,

I need help with an ajax call parsing values.

 

I have a script include that works correctly and returns the list of sys ids from the same company as the requester but the CCS I am not able to parse the results. I need to limit the list of available users to be in the same company as the contact person.

 

Any help greatly appreciated.

 

CCS

function onLoad() {
// Get the sys_id of the contact person
var contactPersonSysId = g_form.getValue('ref_contact_person');
console.log('Contact Person sys_id: ' + contactPersonSysId);
if (contactPersonSysId) {
// GlideAjax object to call the Script Include for getting users from the same company
var ga = new GlideAjax('GetUsersFromCompany'); // Script Include name
ga.addParam('sysparm_name', 'getList'); // Method name
ga.addParam('sysparm_contactPersonSysId', contactPersonSysId); // Pass the contact person's sys_id

// Make the AJAX call to the Script Include
ga.getXMLAnswer(function(response) {
// Check if response is valid before parsing
if (response) {
try {
// The response should be a JSON string, so parse it
var answer = response;
var userSysIds = JSON.parse(answer);
console.log('Parsed User sys_ids: ', userSysIds);

// Set the list of user sys_ids into the field (e.g., 'hidden_req_for')
g_form.setValue('hidden_req_for', userSysIds.join(',')); // Convert array to comma-separated string
} catch (e) {
console.error('Error parsing JSON: ', e);
}
} else {
console.error('Error: The response is empty or undefined');
}
});
}
}

 

 

 

SI

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

// Define the getList function to get the list of user sys_ids
getList: function() {
var contactPersonSysId = this.getParameter('sysparm_contactPersonSysId'); // Get the contact person's sys_id
gs.info('Script Include - Contact Person sys_id: ' + contactPersonSysId); // Log the contact person sys_id

var userList = [];

// Get the company of the contact person
var userGR = new GlideRecord('sys_user');
if (userGR.get(contactPersonSysId)) {
var contactPersonCompany = userGR.company; // Get the company of the contact person

// Query users in the same company
var gr = new GlideRecord('sys_user');
gr.addQuery('company', contactPersonCompany);
gr.addQuery('active', true); // Filter only active users
gr.query();

// Collect sys_ids of users in the same company
while (gr.next()) {
var sysId = gr.sys_id + ''; // Ensure sys_id is treated as a string for consistency
if (userList.indexOf(sysId) === -1) { // Only add unique sys_ids
userList.push(sysId); // Add the sys_id of the user to the list if it's not already added
}
}
}

// Log the response before returning for debugging
gs.info('Returning sys_ids: ' + JSON.stringify(userList)); // Logs the JSON string of sys_ids

// Return the list as a JSON string
return JSON.stringify(userList); // Convert to JSON string and return it
},
type: 'GetUsersFromCompany'
});
1 ACCEPTED SOLUTION

You cannot set a reference qualifier with a Catalog Client Script.  Well, there is a way using a Script Include to putClientData in the session, but then the reference qualifier needs changed to get the session data, so as long as you're changing the qualifier...

 

Assuming the two qualifiers are entirely distinct, since u_collective_identifier wasn't mentioned in your new script, the cleanest way to do this is to change the qualifier in the variable set to:

javascript:new global.getManagerUsers().getUsers(current.variables.cat_item_id, current.variables.ref_contact_person);

So we're passing in another variable value, and removing the criteria for active and collective_identifier which we will incorporate into the Script Include like you did for active on the new script.  The idea for the Script Include is that the same function now includes both GlideRecords, but only runs the new one (first) if the ref_contact_person was passed in.  If this variable name is not and cannot be made unique to this Catalog Item to distinguish it from the ~87 others then you can also pass in the Catalog Item sys_id using g_form.getUniqueValue().  The Script Include would now look more like this, or whatever makes sense to you knowing what each script was doing:

var getManagerUsers = Class.create();
getManagerUsers.prototype = {
    initialize: function() {},

    getUsers: function(cat_id, contactPersonSysId, catItemId) {
        if (contactPersonSysId) { //or catItemId=..., or may re-use !isValid?...
            var userList = [];

            // Get the company of the contact person
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(contactPersonSysId)) {
                var contactPersonCompany = userGR.company; // Get the company of the contact person

                // Query users in the same company
                var gr = new GlideRecord('sys_user');
                gr.addQuery('company', contactPersonCompany);
                gr.addQuery('active', true); // Filter only active users
                gr.query();

                // Collect sys_ids of users in the same company
                while (gr.next()) {
                    userList.push(gr.sys_id.toString()); // Add the sys_id of the user to the list
                }
            }
        
            // Log the response before returning for debugging
            gs.info('Returning sys_ids: ' + userList.join(',')); // Logs the array string of sys_ids

            // Return the list as a JSON string
            return 'sys_idIN' + userList.join(','); // Convert to array string and return it
		}
    
        var items = gs.getProperty('hidden_req_for_ref_qual_item_ids').split(','); //Store Item ids to apply ref qual
        var isValid = items.indexOf(cat_id.toString()) != -1;

        if (isValid) {
            var tab = [];
            var grUser = new GlideRecord('sys_user');
            grUser.addQuery('manager', gs.getUserID());
            grUser.addQuery('active', true);
            grUser.addQuery('u_collective_identifier', false);
            grUser.query();
            while (grUser.next()) {
                tab.push(grUser.getUniqueValue());
            }
            return "sys_idIN" + tab;
        } else {
            return "";
        }
    },

    type: 'getManagerUsers'
};

View solution in original post

6 REPLIES 6

You cannot set a reference qualifier with a Catalog Client Script.  Well, there is a way using a Script Include to putClientData in the session, but then the reference qualifier needs changed to get the session data, so as long as you're changing the qualifier...

 

Assuming the two qualifiers are entirely distinct, since u_collective_identifier wasn't mentioned in your new script, the cleanest way to do this is to change the qualifier in the variable set to:

javascript:new global.getManagerUsers().getUsers(current.variables.cat_item_id, current.variables.ref_contact_person);

So we're passing in another variable value, and removing the criteria for active and collective_identifier which we will incorporate into the Script Include like you did for active on the new script.  The idea for the Script Include is that the same function now includes both GlideRecords, but only runs the new one (first) if the ref_contact_person was passed in.  If this variable name is not and cannot be made unique to this Catalog Item to distinguish it from the ~87 others then you can also pass in the Catalog Item sys_id using g_form.getUniqueValue().  The Script Include would now look more like this, or whatever makes sense to you knowing what each script was doing:

var getManagerUsers = Class.create();
getManagerUsers.prototype = {
    initialize: function() {},

    getUsers: function(cat_id, contactPersonSysId, catItemId) {
        if (contactPersonSysId) { //or catItemId=..., or may re-use !isValid?...
            var userList = [];

            // Get the company of the contact person
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(contactPersonSysId)) {
                var contactPersonCompany = userGR.company; // Get the company of the contact person

                // Query users in the same company
                var gr = new GlideRecord('sys_user');
                gr.addQuery('company', contactPersonCompany);
                gr.addQuery('active', true); // Filter only active users
                gr.query();

                // Collect sys_ids of users in the same company
                while (gr.next()) {
                    userList.push(gr.sys_id.toString()); // Add the sys_id of the user to the list
                }
            }
        
            // Log the response before returning for debugging
            gs.info('Returning sys_ids: ' + userList.join(',')); // Logs the array string of sys_ids

            // Return the list as a JSON string
            return 'sys_idIN' + userList.join(','); // Convert to array string and return it
		}
    
        var items = gs.getProperty('hidden_req_for_ref_qual_item_ids').split(','); //Store Item ids to apply ref qual
        var isValid = items.indexOf(cat_id.toString()) != -1;

        if (isValid) {
            var tab = [];
            var grUser = new GlideRecord('sys_user');
            grUser.addQuery('manager', gs.getUserID());
            grUser.addQuery('active', true);
            grUser.addQuery('u_collective_identifier', false);
            grUser.query();
            while (grUser.next()) {
                tab.push(grUser.getUniqueValue());
            }
            return "sys_idIN" + tab;
        } else {
            return "";
        }
    },

    type: 'getManagerUsers'
};

I will try this and revert