Catalog Item - Referenced variable - Advanced reference qualifier based on user's company

AEterni
Mega Guru

Hello All,

I read many posts in this community related to advanced/dynamic reference qualifiers, but I need help understanding what I should do due to my lack of scripting skills.

 

Scenario

I have a catalogue item called "Update distribution list". The item has a variable called "User". The variable type is "Reference", and it references the sys_user table. The "Use reference qualifier" is Simple and the parameters are:

  • Active = True
  • Name = Is Not Empty

I need to change the reference qualifier and make it dynamic so that:

  • Users with company = XYZ should be able to see all users from the drop-down menu of the "User" referenced variable
  • Users with a company different from XYA should be able to see only their company.

For example:

  • John Doe - Company XYS - he should be able to see all users from the drop-down menu
  • Jane Doe - Company NOT-XYS - he should be able to see ONLY users with company NOT-XYS

I tried different script suggested by ChatGPT, but I can't get the working.

 

Thank you.

 

3 REPLIES 3

Ehab Pilloor
Mega Sage

Hi @AEterni

 

You need to use onChange Catalog client script and use Script Include for the reference qualifiers. Script Include should take in User as parameter and use if condition for your reference qualifiers. 

 

If you found my response helpful, please mark it as Solution and Helpful.

 

Thanks and Regards,

Ehab

AEterni
Mega Guru
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // Call the Script Include to get the dynamic reference qualifier
    var ga = new GlideAjax('UserFilterHelper');  // GlideAjax object for Script Include
    ga.addParam('sysparm_name', 'getUserReferenceQualifier');  // Call the method from Script Include
    ga.getXMLAnswer(function(response) {
        var qualifier = response;  // Get the response from the Script Include

        // Update the reference qualifier for the 'User' variable dynamically
        g_form.getControl('user_variable_name').setAttribute('data-ref-qual', qualifier);
        g_form.clearOptions('user_variable_name');  // Clear existing options
        g_form.refreshOptions('user_variable_name');  // Refresh the reference field
    });
}

Thank you.

 

I created a client callable script include.

 

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

    // Function to get the reference qualifier based on the current user's company
    getUserReferenceQualifier: function() {
        var currentUserCompany = gs.getUser().getCompanyID(); // Get current user's company sys_id
        var xyzCompanySysID = 'sys_id_company'; // sys_id of xyz

        // Define the qualifier conditionally based on the user's company
        if (currentUserCompany == xyzCompanySysID) {
            return "active=true^nameISNOTEMPTY";  // Show all users for xyz company
        } else {
            return "active=true^nameISNOTEMPTY^company=" + currentUserCompany;  // Show only users from the same company
        }
    },

    type: 'UserFilterHelper'  // Name of the Script Include
};

 

I created an onChange Client Script in the cat item.

 

Again...I leveraged ChatGPT to draft the script.

 

It doesn't work and I don't know how to troubleshoot it.

 

Thank you.

 

 

 

 

You need to store user records in an array in Script Include and pass it to onChange Client Script.

Use no filter query for user whose company is XYZ and use query with user company record.

You need to finetune your prompt for Script Include. Your Script Include should return array of records instead of Encoded Query.