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

I think you have found out the reason why it is not finding the right record. the location name extracted from short decsription is M0111 but the complete name on location field is different.

The Name is not exactly "M0516" so the GlideRecord query is not finding a match.  If you're saying now that your Location naming convention will always START WITH M plus the numeric value, not EQUAL it, then the addQuery line to find this record would be

locRef.addQuery("name", "STARTSWITH", numericPart); 

 

Hi @Brad Bowman  ,
Thank you for  helping on  this
we have given the this script now its working
locRef.addQuery("name", 'CONTAINS', numericPart);


Hi,

 

var locRef = new GlideRecord("cmn_location");
locRef.addQuery("name", 'M' + numericPart); 

locRef.query();

Can you log getRowCount over here.

Then after your if (locRef.next()) {

Log the sys_id returned : locRef.sys_id 

Use this sys_id in your query filter on cmn_location to see if the right sys_id is being returned and then set it on location field on incident form .

Still if it is not updating in that case you need to check any BR's if updation of location field is being aborted.