- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2022 11:10 PM
Hello All,
I want to fetch the topmost parent business unit and set it in the user table. I am stuck in between with the below after insert/update business rule.
1. In the user table there is a filed called "Department".
2. Every "Department" has a "Business Unit" associated with it.
3. Every "Business Unit" has a parent "Business Unit" set in the "Parent" field.
4. We have created a custom string field "Business Unit" on the user table.
5. On change of department, the topmost parent "Business Unit" should be set.
I have written the below after business unit on the user table, but it's limited to fetch only the first parent. Can someone help me in fetching the topmost parent from the business unit table?
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('cmn_department');
gr.addQuery('sys_id', current.u_d);
gr.query();
while (gr.next()) {
var bu = gr.business_unit;
var grb = new GlideRecord('business_unit');
grb.addQuery('sys_id', bu);
grb.query();
while (grb.next()) {
var parent = grb.parent;
current.u_business_unit = prent.name;
}
}
current.update();
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2022 02:29 AM
Hi @anirban300
Please find updated code below:
(function executeRule(current, previous /*null when async*/ ) {
current.u_business_unit = getBUParent(current.u_d.bussiness_unit);
current.update();
})(current, previous);
function getBUParent(bu)
{
var grb = new GlideRecord('business_unit');
grb.addQuery('sys_id', bu);
grb.query();
if (grb.next()) {
if (!grb.parent) {
return grb.sys_id;
} else {
return getBUParent(grb.parent);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2022 11:43 PM
Can you try below script?
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('cmn_department');
gr.addQuery('sys_id', current.u_d);
gr.query();
if (gr.next()) {
current.u_business_unit = getBUParent(gr.getValue('business_unit'))
current.update();
}
function getBUParent(bu)
{
var grb = new GlideRecord('business_unit');
grb.addQuery('sys_id', bu);
grb.query();
if (grb.next()) {
if (grb.getValue('parent')=='')
return grb.name;
else
getBUParent(grb.getValue('parent'));
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2023 02:58 AM
The above script returns 'undefined' when used in Business rule by calling the script include to get the parent.
Any suggestions please.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2022 02:07 AM
Hi @anirban300 ,
Try below code.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('cmn_department');
gr.addQuery('sys_id', current.u_d);
gr.query();
if (gr.next()) {
var bu = gr.business_unit;
var grb = new GlideRecord('business_unit');
grb.addQuery('sys_id', bu);
grb.query();
if (grb.next()) {
var parent = grb.parent;
current.u_business_unit = parent.name;
current.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2022 02:29 AM
Hi @anirban300
Please find updated code below:
(function executeRule(current, previous /*null when async*/ ) {
current.u_business_unit = getBUParent(current.u_d.bussiness_unit);
current.update();
})(current, previous);
function getBUParent(bu)
{
var grb = new GlideRecord('business_unit');
grb.addQuery('sys_id', bu);
grb.query();
if (grb.next()) {
if (!grb.parent) {
return grb.sys_id;
} else {
return getBUParent(grb.parent);
}
}