CMDB Scripting

Vivek kumar10
Tera Expert

whenever a user becomes part of 'managed by' or 'owned by' to any ci then he should be added to a group.

 

I have created a new group for that named as cmdb_members.

 

And Similarly, if user is removed from managed_by or owned_by from any ci then he should be removed from that group if he is not already part of any other ci.

 

I have used Async business rule 

and given filter as Managed_by  changes or owned_by changes

 

here is code that I have tried:-

(function executeRule(current, previous /*null when async*/) {

var mByName = current.managed_by;//to store managed by name
var oByame = current.owned_by;
var poByName = previous.owned_by ;
var pmByName = previous.managed_by;// to store owned by name


var newGroup = new GlideRecord('sys_user_grmember');// to get group table name
newGroup.addQuery('user', '=', mByName ,'||', oByame);
newGroup.query();

while(newGroup.next()){
if(mByName != pmByName || oByame != poByName){ // to check user is changed or not
newGroup.group = current.variables.cmdb_members;
newGroup.insert(mByName);
}

if(pmByName.hasAttribute(false)){
newGroup.deleteRecord(pmByName);
}

}
})(current, previous);

 

1 ACCEPTED SOLUTION

(function executeRule(current, previous /*null when async*/ ) {

if (current.managed_by != previous.managed_by) { // condition check

addUserToGroup(current.managed_by);// to call function add user to group

var ci = new GlideRecord('cmdb_ci');
ci.addQuery('group', '0902052547b2911058a3f088436d4323');// add filter where to remove
ci.addQuery('user', previous.managed_by);
ci.query();
if (!ci.next()) {
removeUserFromGroup(previous.managed_by);
}
}

function addUserToGroup(sys_id) {
var addUser = new GlideRecord('sys_user_grmember');
addUser.initialize();
addUser.group = '0902052547b2911058a3f088436d4323';
addUser.user = sys_id;
addUser.insert();
}

function removeUserFromGroup(sys_id) {
var remUser = new GlideRecord('sys_user_grmember');
remUser.addQuery('group', '0902052547b2911058a3f088436d4323');
remUser.addQuery('user', sys_id);
remUser.query();
if (remUser.next()) {
remUser.deleteRecord();
}

}

View solution in original post

5 REPLIES 5

Jan Cernocky
Tera Guru

Hi Vivek,

before going any deeper in this topic, notice this

(function executeRule(current, previous /*null when async*/) {

so the previous GR is null I guess. 

If you need to access previous values, you would need to run after BR.

 

Anyway, I would go with different approach.

Instead of your IF statement, I would add that condition directly into the BR condition:

current.owned_by.changes() || current.managed_by.changes()

In this case you can guarantee that BR only runs for these 2 scenarios, in your case it runs all the time, queries DB to get GR and only then checks if the managed_by or owned_by changed. So you do extra queries that have performance impact.

Hi Jan, 

Yes, you are right.

Can you please tell me how to add current user to group. group name is cmdb_members??

Hi Vivek,

I wrote this after BR that adds users to the table

var groupID = '710b50f51bf6911054952f08b04bcb6a';
	
	if (current.managed_by.changes()) {
		var managedByGR = new GlideRecord('sys_user_grmember');
		managedByGR.addQuery('group', groupID);
		managedByGR.addQuery('user', current.getValue('managed_by'));
		managedByGR.setLimit(1);
		managedByGR.query();
		if (!managedByGR.hasNext()) {
			managedByGR.user = current.getValue('managed_by');
			managedByGR.group = groupID;
			managedByGR.insert();
		}
	}
	
	if (current.owned_by.changes()) {
		var ownedByGR = new GlideRecord('sys_user_grmember');
		ownedByGR.addQuery('group', groupID);
		ownedByGR.addQuery('user', current.getValue('owned_by'));
		ownedByGR.setLimit(1);
		ownedByGR.query();
		if (!ownedByGR.hasNext()) {
			ownedByGR.user = current.getValue('owned_by');
			ownedByGR.group = groupID;
			ownedByGR.insert();
		}
	}

The BR condition will be as I mentioned in the previous post with changes()

I guess you need to remove also users when the user is removed from managed by or owned by. This one is a bit more tricky because you need to cover

a) When user is removed (old value is user A, new value is empty) - check the cmdb_ci table if the user is not set as managed_by or owned_by anywhere. If not, delete him from the group members table

b) When user A is replaced by user B. That is combination of those 2 above. You need to run the logic for both, user A to remove and user B to add.

 

 

 

Hey Jan,

Thank you for time and solution it is not working for my condition

My problem has been solved, as you have said async will not work but mine script was little different