Need to update the location based on the Short description

Vani14
Tera Contributor

we have short description like  [S1871R02] and we need to update the 1871 into the location field in incident.
we wrote script already but when we try to update the incident location is not matching with short description. 

Business Rule script:

(function executeRule(current, previous) {
     var shortDescription = current.short_description;
      current.location = null; 
 
    if (shortDescription) {
 
        var numericPartMatch = shortDescription.match(/S(\d{4})R/);
        if (numericPartMatch && numericPartMatch.length > 1) {
            var numericPart = numericPartMatch[1];
 
           var locRef = new GlideRecord("cmn_location");
 
locRef.initialize();
            locRef.addQuery("name", 'M' + numericPart); 
            locRef.query();
        
            if (locRef.next()) {
           
                current.location = locRef.getUniqueValue();
            } else {
                gs.info("Location not found for the provided numeric part: " + numericPart);
            }
        } else {
            gs.info("No numeric part found in the short description.");
        }
    } else {
        gs.info("Short description is empty.");
    }
})(current, previous);
 
Vani14_0-1695986534646.png

 

 
 

 

18 REPLIES 18

So using my version of the script you are seeing "Location found:" in the log, but that's all it says - no Location Name following, or are you not even seeing "Location found:" in the log?

 @Brad Bowman  ,
we are not seeing any logs


Vani14_0-1696337120391.png

 

That makes more sense.  So back it up a step - are you seeing the "numericPart:" log, and if so, so the value preceded by 'M' match the name of a Location record?  If you are not seeing this log, what is the value in the "numericPartMatch:" log? Since you are using the second member of this value in to assign to numeric part, change the numericPartMatch log to confirm the value of [1]

gs.info("numericPartMatch: " + numericPartMatch + " numericPartMatch[1]: " + numericPartMatch[1]);

That's progress.  Do you have a Location record with a Name = M0516?