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

@Brad Bowman  yes we have M0516 location record but in incident its updating as M1203

So then back to the script logs.  Are you seeing "numericPart: 0516"?  And you're still not seeing "Location found: ...."?  Are you seeing "Location not found..."

 

It wouldn't hurt to force numericPartMatch to a string and append the 'M' there so that you know for certain what the GR query is searching for:

var numericPart = "M" + numericPartMatch[1].toString();
...

locRef.addQuery("name", numericPart); 

confirm the log for numericPart is M0516, then see if the GlideRecord query finds this record.  If it still doesn't, try a different location record.

 

we are able to get logs for numeric Part as M0516 after adding 


Var numeric Part = "M" + numericPartMatch[1].toSting();

 

but we didn't get logs on Locationfound:

 

 var locRef = new GlideRecord("cmn_location");
locRef.addQuery("name", numericPart);
locRef.query();
if (locRef.next()) {
gs.info("Location found: " + locRef.name);
this is not working we didn't get any logs for Location found

Vani14_0-1696355831928.png

 

 

 

Now were back/down to there must not be a record on the Location table with the Name exactly = "M0516".  Maybe there's an initial or trailing space in the Name or the zero is a letter O?

@Brad Bowman  

i have checked  the location name 

Vani14_0-1696416045272.png