Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

GlideAjax script not working

brogaming70
Mega Contributor

I have a record producer, which is having some variables and variable set. There is a variable legal_entity (reference field) that populates the logged in user company. I want to use this company value in my script include function to fetch some data. As I am using script include function in the reference qualifier of one of my variable (budget) and this variable is in the variable set. 
I have used glideAjax client script to send the legal_entity value to the script include but it is not working.

I have already tried getBudget(current.variables.legal_entity); in the reference qualifier.

Any help on this is very much appreciated. Attaching some screenshots for reference.

 

Thank you

4 REPLIES 4

Sneha KH
Giga Guru

Hello @brogaming70 ,
1. Remove GlideAjax
2. Modify Script Include:

var ReferenceQualifierNonCatalogItems = Class.create();
ReferenceQualifierNonCatalogItems.prototype = {

getBudget: function(companySysId) {

if (!companySysId)
return 'sys_idINnone';

var idSet = [];

var map = new GlideRecord('x_sfrna_source_t_0_accounting_combination');
map.addQuery('company', companySysId);
map.addQuery('cost_center.name', 'STARTSWITH', 'jc');
map.query();

while (map.next()) {
if (map.cost_center)
idSet.push(map.cost_center.toString());
}

if (idSet.length == 0)
return 'sys_idINnone';

return 'sys_idIN' + idSet.join(',');
},

type: 'ReferenceQualifierNonCatalogItems'
};

3. Go to Budget variable:Set Use reference qualifier --> Advanced

Use this:
javascript: new sn_shop.ReferenceQualifierNonCatalogItems()
.getBudget(current.variables.legal_entity);
Try if this works!

Tanushree Maiti
Giga Sage

Hi @brogaming70 ,

 

Remove client script and try the code shared by  @Sneha KH . If its not working , put gs.log() and debug where it is breaking.

It will work for you.

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Hi,
I have removed the client script and updated the script and the variable as well. It is still not working. I put the gs.log() as well to print the companySysId but it is printing undefined. 

DiveshTyagi
Mega Guru

Hi @brogaming70 ,

 

You do NOT need GlideAjax for a reference qualifier on a catalog variable.
That is why it’s not working.

In Catalog Reference Qualifier (Advanced), you can directly access other variable values using:

current.variables.variable_name

 

You can Use Script Include using try and catch error handling or logs which will give proper error logging.

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

    getBudget: function(companySysId) {

        try {
            gs.info('RQ DEBUG: Company Sys ID = ' + companySysId);
            // Safety check
            if (!companySysId) {
                gs.info('RQ DEBUG: No company received.');
                return 'sys_idINnone';
            }

            var ids = [];
            var gr = new GlideRecord('x_sfma_source_t0_accounting_combination'); // change if needed
            gr.addQuery('company', companySysId);
            gr.query();
            while (gr.next()) {

                if (gr.cost_center) {
                    ids.push(gr.cost_center.toString());
                }
            }
            gs.info('RQ DEBUG: Total records found = ' + ids.length);

            if (ids.length == 0) {
                gs.info('RQ DEBUG: No matching budget found');
                return 'sys_idINnone';
            }
            var qualifier = 'sys_idIN' + ids.join(',');
            gs.info('RQ DEBUG: Final Qualifier = ' + qualifier);

            return qualifier;
        } catch (ex) {

            gs.error('RQ ERROR: getBudget failed');
            gs.error('RQ ERROR Message: ' + ex.message);

            return 'sys_idINnone'; // fail-safe
        }
    },

    type: 'ReferenceQualifierNonCatalogItems'
};

 

Go to Budget variable → Type Specifications → Reference Qualifier

Set:

  • Use reference qualifier → Advanced

  • Script:

javascript:new sn_shop.ReferenceQualifierNonCatalogItems().getBudget(current.variables.legal_entity);

 

Why GlideAjax Was Failing

Because:

  • Reference qualifier already runs server-side.

  • GlideAjax is client → server async.

  • Reference qualifier expects synchronous return string.

  • You cannot use getXMLAnswer inside qualifier.

So it was never returning proper filter.

 

-------------------------------------------------------------------------------------------------------------------------------------------------

If my response helped, please mark it as the accepted solution so others can benefit as well.

Regards,

Divesh Tyagi