Santhana Rajhan
Mega Sage
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 03-07-2022 11:04 PM
This examples is used to set the location of a CI based on a few characters available in the name.
Type: before Business Rule
(function executeRule(current, previous /*null when async*/) {
//Set two properties which has a 1v1 relation. 1 for string and the other for the sys_id of the record
var prpt1 = gs.getProperty('ci.location.string').toString();// This containts the string text that needs to be compared
var prpt2 = gs.getProperty('ci.location.value').toString(); // This contains the sysids of the related record
var str = prpt1.split(',');
var location = prpt2.split(',');
var name = current.name.toLowerCase().toString();
var mloc = []; //Locations matched in the name
var ploc = []; //Position of locations matched in the name
var floc = []; //First location in name
var sloc = ''; //First location in name as string
//Get array of locations matched
for(var i = 0; i < str.length; i++){
if(name.indexOf(str[i]) > -1){
mloc.push(str[i]);
ploc.push(name.indexOf(str[i]));
}
}
//End script if no locations found
if(ploc.length == 0) {
current.location = '';
return;
}
//Get first location if 2 locations found
if(ploc.length == 2) {
if(ploc[1] < ploc[0]){
floc.push(mloc[1]);
} else {
floc.push(mloc[0]);
}
} else {
floc.push(mloc[0]);
}
sloc = floc.toString();
var pos = str.indexOf(sloc); //Position in str array - Line 6
var loc = location[pos]; //Get value in same position in location array - Line 7
current.location = loc;
})(current, previous);
Comments
_ChrisHelming
Tera Guru
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
03-08-2022
07:10 AM
If you're looking for the first match in an array, you can just do .indexOf() on the array.
For example:
var arr = ["apple","cherry","banana","cherry","pear","peach"]
arr.indexOf("cherry");
// returns 1
so for locations matched, you can replace:
for(var i = 0; i < str.length; i++){
if(name.indexOf(str[i]) > -1){
mloc.push(str[i]);
ploc.push(name.indexOf(str[i]));
}
}
with
str.indexOf(name);
or, after you break your properties into arrays, you could just run
if (str.indexOf(name) > -1) {
current.location = location[str.indexOf(name)];
}