Fetch records based on logged in user from a custom table

Rekha20
Tera Contributor

Hi All, 

I have created a custom a table sn_hr_core_request_type, having fields like below

Rekha20_0-1754676498769.png

 

I have created an HR Service which associated with an Record Producer, record producer is having variable:

 

Rekha20_1-1754676587964.png

 

I need to show Request type choices based on available for country field based on who is this request for variable changes.

 

I have craeted script include: 

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

    getRequestTypeQualifier: function() {
        var userID = this.getParameter('sysparm_user_id');
        var hrServiceID = this.getParameter('sysparm_hr_service_id');

        if (!userID || !hrServiceID)
            return "sys_idINnone";

        // Get user's country via HR Profile
        var hrProfileGR = new GlideRecord('sn_hr_core_profile');
        hrProfileGR.addQuery('user', userID);
        hrProfileGR.query();

        var userCountry = '';
        if (hrProfileGR.next() && hrProfileGR.user.u_country)
            userCountry = hrProfileGR.user.u_country.toString();

        var validRequestTypes = [];
        var gr = new GlideRecord('sn_hr_core_request_type');
        gr.addQuery('u_hr_service', hrServiceID);
        gr.addQuery('u_active', true);
        gr.query();

        while (gr.next()) {
            var available = gr.u_available_for_country.toString();
            var notAvailable = gr.u_not_available_for_country.toString();

            var include = true;

            if (available && available.indexOf(userCountry) === -1)
                include = false;

            if (notAvailable && notAvailable.indexOf(userCountry) !== -1)
                include = false;

            if (include)
                validRequestTypes.push(gr.getUniqueValue());
        }

        if (validRequestTypes.length === 0)
            return "sys_idINnone";

        return "sys_idIN" + validRequestTypes.join(',') + "^ORDERBYu_order";
    },

    type: 'HRRequestTypeQualifier'
});
 
Catalog client script: 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') return;

    var ga = new GlideAjax('HRRequestTypeQualifier');
    ga.addParam('sysparm_name', 'getRequestTypeQualifier');
    ga.addParam('sysparm_user_id', newValue);
    ga.addParam('sysparm_cat_item', g_form.getUniqueValue()); // record producer sys_id

    ga.getXMLAnswer(function(response) {
        if (response) {
            g_form.setValue('u_request_type', response);
        } else {
            alert("No valid request types returned.");
        }
    });
}
 
but this is not working. Please help with the code.
13 REPLIES 13

Hello @Rekha20 ,

 

Kindly try to user

ref_auto_completer=AJAXTableCompleter;ref_sequence=u_order

 

If not working,Can you please share screen print of your dropdown what it's showing, and order in your custom table.

 

 Mark this as helpful if it resolves your issue.

 

Thanks,

Sunny R

 

Hi @Sunny3008 

Table record:

 

Rekha20_0-1754903119739.png

 

Choices on variable:

Rekha20_1-1754903154281.png

 

Order is correct for logged in user but when change user in Who is this request for? i.e. subject_person then these choices order are not correct.

Hi Rekha,

 

Did you tried with variable attribute like following

ref_auto_completer=AJAXTableCompleter;ref_sequence=u_order,ref_qual_elements=subject_person

 

Thanks,

Sunny R

 

Yes @Sunny3008  I tried but still the same result.