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

It seems the values you get from the list are not SysIDs, but rather string fields of manually entered data perhaps?

A list can contain both SysIDs, and manually entered data (depending on how it's setup).

So I would advice to double check the data, and check that it's a valid record before updating the record with the new value.

Hi @OlaN 

seems to be that the list field in the "u_dl_tso_lookup" table is rightly setup.

JohnDF_0-1721198433321.png

JohnDF_1-1721198521703.png

 

Satishkumar B
Giga Sage
Giga Sage

Hi @JohnDF ,
refer this:

 

 

(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()) {
                // Retrieve the sys_id of the u_leistungsschein record
                var leistungsscheinGR = new GlideRecord('u_leistungsschein');
                if (leistungsscheinGR.get('name', leistungsscheinValue)) { // Assuming 'name' is the field that matches leistungsscheinValue
                    taskSlaGR.u_leistungsschein = leistungsscheinGR.sys_id; // Assign the sys_id to the reference field
                    taskSlaGR.update();
                } else {
                    gs.error('u_leistungsschein record not found for value: ' + leistungsscheinValue);
                }
            }
        }
    }
})();

 

 

 

……………………………………………………………………………………………………

Please Mark it helpful👍 and Accept Solution✔️!! If this helps you to understand. 

Hi @Satishkumar B thanks for reply. 

 new GlideRecord('u_leistungsschein');

I have no table name like this. "u_leistungsschein" is a field on the "task_sla" table and "u_dl_tso_lookup" table to do the reference on both tables. 

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();
            }
        }
    }
})();