Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

fixscript give me the string and not the sys_id of the record

JohnDF
Mega Sage

Hi everyone,

I have a fix script. But the variable "serviceKomponente" give me a string back. And I need the sys-id becasue the field "u_leistungschein" is a reference field.

 

(function() {
    // Initialize GlideRecord for the u_dl_tso_lookup table
    var tsoLookupGR = new GlideRecord('u_dl_tso_lookup');
    tsoLookupGR.query();
 
    // Iterate over each record in the u_dl_tso_lookup table
    while (tsoLookupGR.next()) {
        // Get the current value of u_leistungsschein from the u_dl_tso_lookup record
        var leistungsscheinValue = tsoLookupGR.u_leistungsschein;
        var primaereServicekomponenteValue = tsoLookupGR.u_primaere_servicekomponente.toString(); // Convert list field to string
 
        // Split the list field into individual values
        var serviceKomponenten = primaereServicekomponenteValue.split(',');
 
        // Iterate over each service component
        for (var i = 0; i < serviceKomponenten.length; i++) {
            var serviceKomponente = serviceKomponenten[i].trim();
 
            // Find the corresponding task_sla records by matching service_offering of the related task
            var taskSlaGR = new GlideRecord('task_sla');
            taskSlaGR.addQuery('task.service_offering', serviceKomponente);
            taskSlaGR.query();
 
            // Update the u_leistungsschein field in the task_sla table
            while (taskSlaGR.next()) {
                taskSlaGR.u_leistungsschein = leistungsscheinValue;
                taskSlaGR.update();
            }
        }
    }
})();


WHat I need to adapt here?

Thanks for your help.

1 ACCEPTED SOLUTION

JohnDF
Mega Sage
(function() {
    // Initialize GlideRecord for the u_dl_tso_lookup table
    var tsoLookupGR = new GlideRecord('u_dl_tso_lookup');
    tsoLookupGR.query();
 
    // Iterate over each record in the u_dl_tso_lookup table
    while (tsoLookupGR.next()) {
        // Get the current value of u_leistungsschein from the u_dl_tso_lookup record
        var leistungsscheinValue = tsoLookupGR.getValue('sys_id');
        //gs.addInfoMessage(leistungsscheinValue);
        var primaereServicekomponenteValue = tsoLookupGR.u_primaere_servicekomponente.toString(); // Convert list field to string
 //gs.addInfoMessage(primaereServicekomponenteValue);
        // Split the list field into individual values
        var serviceKomponenten = primaereServicekomponenteValue.split(',');

        // Iterate over each service component
        for (var i = 0; i < serviceKomponenten.length; i++) {
            var serviceKomponente = serviceKomponenten[i].trim();
  //gs.addInfoMessage(serviceKomponente);
            // Find the corresponding task_sla records by matching service_offering of the related task
            var taskSlaGR = new GlideRecord('task_sla');
            taskSlaGR.addQuery('task.service_offering.sys_id', serviceKomponente);
            taskSlaGR.query();
 
            // Update the u_leistungsschein field in the task_sla table
            while (taskSlaGR.next()) {
                taskSlaGR.setValue('u_leistungsschein', leistungsscheinValue);
                taskSlaGR.update();
            }
        }
    }
})();

View solution in original post

9 REPLIES 9

OlaN
Tera Sage
Tera Sage

Hi,

Could you please clarify your question.

A sysID is in fact a string, it's a 32bit unique identifier, for more info, you can read on the Docs.

What did you expect to get ?

@OlaN thanks for reply.

I checked one example records after I run the script and its put the display name into the reference field and not the sys_id.
The field is empty but the xml show me that something is in the field.

JohnDF_1-1721113467739.png

 

JohnDF_0-1721113404395.png

 

I expect to see the sys_id in the u_leistunggschein field and not the display name?


Thanks for your help.

Sorry, still hard to understand your issue.

But you can start off by avoiding the classic pass-by-reference issue.

Change your code to use getters and setters. Like below.

// change this type of code
var leistungsscheinValue = tsoLookupGR.u_leistungsschein;

// into this
var leistungsscheinValue = tsoLookupGR.getValue('u_leistungsschein');


// and change this type of code
taskSlaGR.u_leistungsschein = leistungsscheinValue;

// into this
taskSlaGR.setValue('u_leistungsschein', leistungsscheinValue);

 

@OlaN 

 

I changed the script. Still same behavior.

(function() {
    // Initialize GlideRecord for the u_dl_tso_lookup table
    var tsoLookupGR = new GlideRecord('u_dl_tso_lookup');
    tsoLookupGR.query();
 
    // Iterate over each record in the u_dl_tso_lookup table
    while (tsoLookupGR.next()) {
        // Get the current value of u_leistungsschein from the u_dl_tso_lookup record
        var leistungsscheinValue = tsoLookupGR.getValue('u_leistungsschein');
        var primaereServicekomponenteValue = tsoLookupGR.u_primaere_servicekomponente.toString(); // Convert list field to string
 
        // Split the list field into individual values
        var serviceKomponenten = primaereServicekomponenteValue.split(',');
 
        // Iterate over each service component
        for (var i = 0; i < serviceKomponenten.length; i++) {
            var serviceKomponente = serviceKomponenten[i].trim();
 
            // Find the corresponding task_sla records by matching service_offering of the related task
            var taskSlaGR = new GlideRecord('task_sla');
            taskSlaGR.addQuery('task.service_offering', serviceKomponente);
            taskSlaGR.query();
 
            // Update the u_leistungsschein field in the task_sla table
            while (taskSlaGR.next()) {
                taskSlaGR.setValue('u_leistungsschein', leistungsscheinValue);
                taskSlaGR.update();
            }
        }
    }
})();

 

The first screenshot shows that the "u_leistungsschein" reference field contains a value but lacks a corresponding sys_id. After running the script, the field should display both the value and the sys_id, similar to how it was manually done by me. see screenshot -> 

JohnDF_1-1721123164879.png


and here After I run the script I can group by "u_leistungsschein" I find the records bot there is no Reference in the field. Becasue I think of missing sys_id.

JohnDF_0-1721123099505.png