Writing to the Activity Log for User Records

jmiskey
Kilo Sage

So, when a user becomes inactive, we would like to remove all their groups (for security reasons - we have a lot of rehires).   I found an old thread with code on how to do that which works great (How do I remove a user from all groups using a script ).

However, we would like to write this activity to the Activity Log that can be seen on the User Record.   The reason for this is two-fold:

- In case we are ever asked what Groups a terminated person was a member of (audit purposes)

- In case someone is accidentally deactivated (it happens), it would be nice to easily see what groups that they were a part of, so we can set them back to what they should be

I have been searching around, and I cannot find anything that describes how I can write to the User Activity Log (preferably from the Business Rule shown in the script of the linked thread).   Can anyone help with that?

Thanks

1 ACCEPTED SOLUTION

Adding onto Christopher's answer:



You'll want your Comments to be a journal field. You'll also want to add an activity log - http://wiki.servicenow.com/index.php?title=Activity_Formatter#gsc.tab=0



Once you have that set up, try this script. If your comments field is not "u_comments", you'll have to change it on line 23 to match your field name :


removeMemberships();


function removeMemberships() {


var userID = current.sys_id;


var groups = [];


var rm = new GlideRecord("sys_user_grmember");


rm.addQuery("user.sys_id", userID);


rm.query();


while (rm.next()) {


groups.push(rm.group.name);


}


logMembership(groups);


rm.deleteMultiple();


}




function logMembership(groups) {


var userID = current.sys_id;


var str = 'User removed from the following groups:\n' + groups.join(", ");


var user = new GlideRecord("sys_user");


user.addQuery("sys_id",userID);


user.query();


while (user.next()) {


user.u_comments = str;


user.update();


}


}


View solution in original post

4 REPLIES 4

ccajohnson
Kilo Sage

By default ServiceNow does not have any digest fields that can be used for this purpose. You would need to create a comments field that can be filled in from your script. You would also need to adjust the script to capture those groups being removed so that you can write those groups to the comments field.


Adding onto Christopher's answer:



You'll want your Comments to be a journal field. You'll also want to add an activity log - http://wiki.servicenow.com/index.php?title=Activity_Formatter#gsc.tab=0



Once you have that set up, try this script. If your comments field is not "u_comments", you'll have to change it on line 23 to match your field name :


removeMemberships();


function removeMemberships() {


var userID = current.sys_id;


var groups = [];


var rm = new GlideRecord("sys_user_grmember");


rm.addQuery("user.sys_id", userID);


rm.query();


while (rm.next()) {


groups.push(rm.group.name);


}


logMembership(groups);


rm.deleteMultiple();


}




function logMembership(groups) {


var userID = current.sys_id;


var str = 'User removed from the following groups:\n' + groups.join(", ");


var user = new GlideRecord("sys_user");


user.addQuery("sys_id",userID);


user.query();


while (user.next()) {


user.u_comments = str;


user.update();


}


}


Thanks guys.   I will try this out and post back the results/questions.


Derek,


That worked awesome!   It does exactly what I want to do.



Thanks!