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
Kilo Patron
Kilo Patron

l would remove these lines as they are not needed and may be having an adverse effect:

current.location = null; 
locRef.initialize();

and change the assignment to:

current.location = locRef.sys_id;

Beyond that, you'll need to add some logs to the script to confirm the values of your script variables are what you are expecting, then see if the GlideRecord is returning a record.  You'll also want to be sure this Business Rule is running before Insert and/or Update.

 

@Brad Bowman  

i have tried using locRef but ncident location is not matching with short description.

 

 

(function executeRule(current, previous) {
// Get the short description from the incident
var shortDescription = current.short_description;

// Log the short description for debugging
gs.info("Short Description: " + shortDescription);

// Check if the short description contains a location number
if (shortDescription) {
// Use a regular expression to extract the numeric part  from the short description
var numericPartMatch = shortDescription.match(/S(\d{4})R/);

// Check if a numeric part was found
if (numericPartMatch && numericPartMatch.length > 1) {
var numericPart = numericPartMatch[1];


var locRef = new GlideRecord("cmn_location");
locRef.query();
while (locRef.next()) {
gs.info("Location Name: " + locRef.name); // Log all location names
}


// Check if a location record was found
if (locRef.next()) {
// Update the incident's location field with the location reference
current.location = locRef.getUniqueValue();
gs.info("Location found: " + current.location);
} 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);

You need to log the results/value of numericPartMatch to confirm that is doing what you expect, since you're basing the rest of the script on the value.  Then go back to querying for just that record:

(function executeRule(current, previous) {
    // Get the short description from the incident
    var shortDescription = current.short_description;

    // Log the short description for debugging
    gs.info("Short Description: " + shortDescription);

    // Check if the short description contains a location number
    if (shortDescription) {
        // Use a regular expression to extract the numeric part from the short description
        var numericPartMatch = shortDescription.match(/S(\d{4})R/);
    
        // Log the numeric part match for debugging
        gs.info("numericPartMatch: " + numericPartMatch);

        // Check if a numeric part was found
        if (numericPartMatch && numericPartMatch.length > 1) {
            var numericPart = numericPartMatch[1];

            // Log the numeric part for debugging
            gs.info("numericPart: " + numericPart);

            var locRef = new GlideRecord("cmn_location");
            locRef.addQuery("name", 'M' + numericPart); 
            locRef.query();
            if (locRef.next()) {
                gs.info("Location found: " + locRef.name); // Log location name
                // Update the incident's location field with the location reference
                current.location = locRef.sys_id;
            } 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);

@Brad Bowman  when i try to check the  location found: log its showing empty.