- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 02:37 PM
I am trying to create a notification when an incident is logged against a location.
If the location doesn't have a contact then check the parent for the location and keep going up the ladder until a contact is found. Below is the business rule I created to trigger the event:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var location = getLocationContact(current.location.sys_id.toString());
if (!location.contact.nil()) {
gs.eventQueue('inc.logged_location', current, location.contact.email, location.contact.name);
} else {
return;
}
function getLocationContact(rec) {
var loc = new GlideRecord('cmn_location');
loc.get(rec);
// Check if location has a contact
if (!loc.contact.nil()) {
var str = "location.";
return loc; //return location
} else {
//Check if location has a parent
if (!loc.parent.nil()) {
getLocationContact(loc.parent.sys_id.toString()); //Call the function again using parent record
} else {
return loc; //return location
}
}
}
})(current, previous);
gs.eventQueue('inc.logged_location', current, location.contact.email, location.contact.name); - I am getting an undefined return from the function.
Not sure if the above is the best solution. Thanks in advance
Scenario:
Incident 1 is created against Location A
Location A and B doesn't have contact populated. Location C has contact populated.
Location A is child of Location B. Location B is a child of Location C.
In the above case, I want the notification to be sent to Contact of Location C.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 07:35 AM - edited 08-08-2023 07:36 AM
Hi @Suresh11 ,
Can you please try this.
var location = getLocationContact('25ab9c4d0a0a0bb300f7dabdc0ca7c1c');
if (!location.contact.nil()) {
//gs.info(location.contact)
gs.eventQueue('inc.logged_location', current, location.contact.email, location.contact.name);
}
function getLocationContact(rec) {
var loc = new GlideRecord('cmn_location');
loc.get(rec);
gs.info(loc.name)
// if the location has a contact
if (!loc.contact.nil()) {
return loc; // return location
} else {
// if location has a parent
if (!loc.parent.nil()) {
//recall with parent
return getLocationContact(loc.parent.sys_id.toString());
} else {
return loc; // return location
}
}
}
I have tested this in background, its working fine. Modify per your use case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 07:35 AM - edited 08-08-2023 07:36 AM
Hi @Suresh11 ,
Can you please try this.
var location = getLocationContact('25ab9c4d0a0a0bb300f7dabdc0ca7c1c');
if (!location.contact.nil()) {
//gs.info(location.contact)
gs.eventQueue('inc.logged_location', current, location.contact.email, location.contact.name);
}
function getLocationContact(rec) {
var loc = new GlideRecord('cmn_location');
loc.get(rec);
gs.info(loc.name)
// if the location has a contact
if (!loc.contact.nil()) {
return loc; // return location
} else {
// if location has a parent
if (!loc.parent.nil()) {
//recall with parent
return getLocationContact(loc.parent.sys_id.toString());
} else {
return loc; // return location
}
}
}
I have tested this in background, its working fine. Modify per your use case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 09:16 AM
Thanks for looking into this Sonam. It is working fine now. I missed the return before the recursive function call, didn't think it is needed.