Need to populate the bottom most department of a user

Lak
Tera Contributor

For any given user(Mike), We would need to show the bottom most department(XYZ).

 

Note: User could be associated to any department in the hierarchy, any help or suggestions

 

User: Mike

 

Department:

                      --ABC

                         --BCD

                             --CDE

                                 --XYZ

1 ACCEPTED SOLUTION

Ok , got it, you can use this code. you might required some minor adjustments for input and output parameters. 

 

created a new column on my PDI for this Nth level and used the calculated value to display Nth value based on current department.

AshishKMishra_0-1701524173597.png

 

updated the script code for this

AshishKMishra_0-1701525944493.png

 

you can copy exact code in calculated value, its generic as its taking the current record department for nth level

 

 

(function calculatedFieldValue(current) {

	var deptArray = []; 
	getLastLevel(current.department);

	function getLastLevel(dept){
		var grDept = new GlideRecord("cmn_department");
		    grDept.addQuery("parent",dept);
			grDept.query();
			if(grDept.next()){
					deptArray.push(grDept.sys_id);
					getLastLevel(grDept.sys_id);
				}
}
return deptArray[deptArray.length-1];
})(current);

 

 

result

AshishKMishra_2-1701524366662.png

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

View solution in original post

14 REPLIES 14

Lak
Tera Contributor

Hi @AshishKM ,

 

The challenge with calculated field is that I don't have an i icon to refer the details of that filed and which will be available only through reference field hence I have tried calling the script include through qualifier reference qualifier but unfortunately it isn't working.

Regards,

Lakshmi

@Lak , let me know if you can share the screen and we are review the code. 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Lak
Tera Contributor

Hi @AshishKM , Thanks for the logic provided here, I was able to manage and built the same into my existing script include function and I am able to transform the data and was able to get the nth level department. Thanks again Ashish for the support you have enabled.

 

regards,

Lakshmi 

@Lak,thanks for replying and confirming, Please accept solution and close this thread for others.

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Amit Gujarathi
Giga Sage
Giga Sage

HI @Lak ,
I trust you are doing great.

 

Here's a template to modify the script for integration:

 

// Script Include Template
var BottomMostDepartment = Class.create();
BottomMostDepartment.prototype = {
    initialize: function() {
    },

    getBottomMostDepartment: function(userId) {
        var deptArray = [];
        var grUser = new GlideRecord("sys_user");
        grUser.addQuery("user_name", userId); // Replace with actual user field
        grUser.query();
        if(grUser.next()){
            this.getLastLevel(grUser.department, deptArray);
            return deptArray[deptArray.length - 1];
        }
        return ''; // Return empty if no department found
    },

    getLastLevel: function(dept, deptArray) {
        var grDept = new GlideRecord("cmn_department");
        grDept.addQuery("parent", dept);
        grDept.query();
        if(grDept.next()){
            deptArray.push(grDept.name);
            this.getLastLevel(grDept.sys_id, deptArray);
        }
    },

    type: 'BottomMostDepartment'
};

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi