Pls advise how to get current record sys_id into scriptinclude

BanuMahalakshmi
Tera Contributor

Hi,

Please advise to get sysid of current location value into script include.

I am fetching location name from current record (UI action) and passing to script include. 

UI Action:

 var location = g_form.getvalue('name');
    var ga = new GlideAjax('ContactLocation');
    ga.addParam('sysparm_name', 'getlocation');
    ga.addParam("sysparm_value", location);
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('fieldname', answer);
        g_form.save();
    });

 

Script Include:

 
Query by sys_id of current location instead of name
    getlocation: function() {
        var locationsysid = this.getParameter('sysparm_value');
       // var locationsysid = locationname.sys_id;
            
        var locationGR = new GlideRecord('cmn_location');
        locationGR.addQuery('u_active', true);
        if (locationsysid != '') {

            locationGR.addQuery('sys_id', '=',locationsysid);
        }
 
Thanks.

 

 

 

 

 

 

 

 

8 REPLIES 8

GlideFather
Tera Patron

Hi @BanuMahalakshmi 

return locationGR;

And depending if you expect to have sys_id getUniqueValue(), toString(), or the Name .getDisplayValue() 

 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


GopikaP
Mega Sage

Hi @BanuMahalakshmi , if the field - 'name' is a reference field, then 'location' variable is already having the sys_id of the location. 

or else if you are trying to pass the sys_id of the current form - then try this- 

var location = g_form.getvalue('sys_id');

Please let me know if you are trying to pass the sys_id of the current form, or a reference field's sys_id or a string field's? If it a string field's , then we will have to query against 'name' field in the script include.

Aniket Chavan
Tera Sage
Tera Sage

Hello @BanuMahalakshmi ,

I went through the entire thread, and based on the conversation so far, I think the main confusion might be around the type of field you're using on the form — whether it's a reference field or just a string field. Depending on that, the approach to get the sys_id will be different.

 

Let me break it down for both cases so you can pick whichever fits your use case:

 

1. If you're using a reference field (like u_location or location):

In this case, you can directly get the sys_id using g_form.getValue() and pass that to your Script Include.

 

UI Action:

var location = g_form.getValue('name');  // replace with your actual reference field name
var ga = new GlideAjax('ContactLocation');
ga.addParam('sysparm_name', 'getlocation');
ga.addParam("sysparm_value", location);
ga.getXML(function(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    g_form.setValue('fieldname', answer);
    g_form.save();
});

 

Script Include:

getlocation: function() {
    var locationsysid = this.getParameter('sysparm_value');
    var locationGR = new GlideRecord('cmn_location');
    locationGR.addQuery('u_active', true);
    locationGR.addQuery('sys_id', locationsysid);
    locationGR.query();
    if (locationGR.next()) {
        return locationGR.getUniqueValue(); // or whatever value you want to return
    }
    return '';
}

 

2. If you're using a string field (like name field with plain text):

Here, the value from the form will just be a name, so you'll need to query the location table using the name field instead of sys_id.

 

UI Action:

var locationName = g_form.getValue('name');
var ga = new GlideAjax('ContactLocation');
ga.addParam('sysparm_name', 'getlocation');
ga.addParam("sysparm_value", locationName);
ga.getXML(function(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    g_form.setValue('fieldname', answer);
    g_form.save();
});

 

Script Include:

getlocation: function() {
    var locationName = this.getParameter('sysparm_value');
    var locationGR = new GlideRecord('cmn_location');
    locationGR.addQuery('u_active', true);
    locationGR.addQuery('name', locationName);
    locationGR.query();
    if (locationGR.next()) {
        return locationGR.getUniqueValue(); // this will return the sys_id
    }
    return '';
}

 

So just check if your field is a reference or a plain string, and pick the matching code.

 

🔹 If this solves your query, feel free to mark my response—or whichever one helped you the most—as the accepted solution. You can also mark it as helpful if you found it useful.

 

Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024

Hello @BanuMahalakshmi ,

 

Just wanted to check in to see if my earlier response helped you out or if you're still facing any issues. If your query is resolved, it would be great if you could mark my reply as helpful and accept it as the solution — that way it can also benefit others who come across the thread later.😊

 

Also, if you found any other responses helpful, feel free to mark those as helpful too. You can even accept multiple solutions if they contributed to resolving your issue.

 

Let me know if you need anything else!

 

Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024