auto update field using reference qualifier but sys_idIN is causing issues

ServNowDev
Tera Guru

I have a SI that is getting the Service and if found it auto populates the Business Capability field , this is working fine using the below, 

 

 

 

populatecapabilitye: function(myfieldValue) {
        if (fieldValue != '') {
            var result= '';
            
            var x = new GlideRecord('custom table'); //Queries the Relationships
            x.addQuery('field', myfieldValue); //Gets the Sys ID of the Application Service
            x.addQuery('active', true);
            x.query();
            while (x.next()) {
                result = x.sys_id;

            }
           

            return 'sys_idIN' + myReturn;
        }
    },

 

 

 

their is a ajax and client script that is being used so this code works but my issue isnt the auto population , issue is if i select SAP Human resources the codes all work and populate it with Test 2 in the business capability field but if i alter that field it shows all business capabilities and not the one that matches SAP Human Resources. I have used alerts and it shows that in the client script its trying to set the Business Capability field to sys_inINf345980453454350 and that will not work but im using the sys_idIN cause it will filter it , if i take that portion out it will auto assign but will not filter , here are visuals

when using return result; 

Thomas42_0-1718213734457.pngit auto assigns but if i remove Test 2 and open the filter 

Thomas42_1-1718213812262.pngthis shows and i want it to only show Test 2 as an option

 

if i use return 'sys_idIN' + result 

will not work on auto assign part cause setvalue will not allow sys_idIN part 

Thomas42_2-1718213906809.png

but if you select the filter it will do that 

Thomas42_3-1718213973279.png

 

 

 

1 ACCEPTED SOLUTION

Slava Savitsky
Giga Sage

If my understanding of your situation is correct, you are calling your function from two places: from a Client Script in order to automatically populate the Business Capability field and from the Reference Qualifier for that field in order to limit the list of values available for selection. The problem is that those two use cases require a different syntax. To populate a reference field, you only need the sys_id, but for a reference qualifier, you need an encoded query string.

 

There are two ways you could address this:

 

Option 1: Have your function return just the sys_id and use it in your Client Script. For the Reference Qualifier, add the "sys_idIN" part directly in the code of the Reference Qualifier like this:

 

javascript: 'sys_idIN' + new yourScriptInclude().getCapabilityID();

 

Option 2: Define two separate functions in your Script Include, each with its own response format (with and without the "sys_idIN" part). For the sake of avoidance of duplicate code, your second function can call the first one and concatenate the returned sys_id with the "sys_idIN" prefix.

View solution in original post

4 REPLIES 4

Slava Savitsky
Giga Sage

If my understanding of your situation is correct, you are calling your function from two places: from a Client Script in order to automatically populate the Business Capability field and from the Reference Qualifier for that field in order to limit the list of values available for selection. The problem is that those two use cases require a different syntax. To populate a reference field, you only need the sys_id, but for a reference qualifier, you need an encoded query string.

 

There are two ways you could address this:

 

Option 1: Have your function return just the sys_id and use it in your Client Script. For the Reference Qualifier, add the "sys_idIN" part directly in the code of the Reference Qualifier like this:

 

javascript: 'sys_idIN' + new yourScriptInclude().getCapabilityID();

 

Option 2: Define two separate functions in your Script Include, each with its own response format (with and without the "sys_idIN" part). For the sake of avoidance of duplicate code, your second function can call the first one and concatenate the returned sys_id with the "sys_idIN" prefix.

Slava Savitsky
Giga Sage

On a side note, since you are obviously searching for a single sys_id, there are a few things you could optimize in your code:

 

...

var x = new GlideRecord('custom table');
x.addQuery('field', myfieldValue);
x.addQuery('active', true);
x.setLimit(1); // no need to query the whole table if you only need one sys_id
x.query();

if (x.next()) { // use if instead of while because you only need one value
    result = x.getValue('sys_id'); // always use getValue() to make sure you get a string and not a pointer
}

return 'sys_id=' + myReturn; // for the sake of clarity, use "=" instead of "IN" when querying for a single value (although "IN" is also going to work)

 

one issue is just using this code i wanst able to get any results on the filter , i also added the sys_idIN to the ref qual field, i would need to do two queries where if it empty return all capabilites and second would just return the sysid to set in my CS

 

Thomas42_0-1718219310777.png

i went the first option route with the code above

ServNowDev
Tera Guru

Thanks so much for this