Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to parse through text?

Steven Young
Tera Guru

Hey Folks,

I'm trying to parse through text and get values to use within a script.

Since this is very confusing i'm asking to see if any of you scripting masters can do this, or point me in the correct direction.

We are automatically deactivating itil users after they haven't logged in within 30 days.

We are removing them from groups, and removing their email address from their account and putting it in a field on the user record "u_information"

The blub being put in this "Information" field is like so.

User removed from the following groups due to inactivation on 08-08-2016

Groups: Interfaces,Dept IT Analysts,Applications, Provisioning

Default Group: Provisioning

Email:   jparker@test.com

What i'm looking to do is to create a UI Action to reactivate users who have been disabled.

So i want this UI Action to be a 1 click action, to set:

1.   active = 'true'

2.   locked_out = 'false'

3.   get the email address from the information field and put it back in the email field.

4.   get the default group and put it back in the default group reference field.

5.   get the "Groups"   and add the user back to all of the groups listed.

Does this seem possible?

Any help will be greatly appreciated.

1 ACCEPTED SOLUTION

Here you go




var str = current.u_information;  


var lineArr = str.split('\n');    


var groupLine = '';    


var defaultGroupLine = '';    


var emailLine = '';    


 


for (var i = 0; i < lineArr.length; i++) {    


  if (lineArr[i].indexOf('Groups:') == 0)    


  groupLine = lineArr[i];    


 


  if (lineArr[i].indexOf('Default Group:') == 0)    


  defaultGroupLine = lineArr[i];    


 


  if (lineArr[i].indexOf('Email:') == 0)    


  emailLine = lineArr[i];    


}    


 


current.active = true;    


current.locked_out = false;    


 


if (groupLine != '') {    


  var groupList = groupLine.split(':')[1].split(',');    


  for (var g = 0; g < groupList.length; g++) {    


  var grMember = new GlideRecord('sys_user_grmember');    


  grMember.newRecord();    


  grMember.user = current.sys_id;    


  grMember.group.setDisplayValue(groupList[g].trim());    


  grMember.insert();    


  }    


}    


 


if (defaultGroupLine != '') {    


  var defaultGroup = defaultGroupLine.split(':')[1];    


  current.u_default_group.setDisplayValue(defaultGroup.trim());    


}    


 


if (emailLine != '') {    


  var email = emailLine.split(':')[1];    


  current.email.setDisplayValue(email.trim());    


}    


current.update();    


action.setRedirectURL(current);  


View solution in original post

22 REPLIES 22

Yes, After playing with this a little i saw your post.   adding the g   to the field worked.



thank you so much.



i just have 1 more issue now.


the Groups:   line looks like


Groups: Oconee - Interfaces,Dept IT Analysts,Oconee - Applications,



there is one last comma at the end, which is causing 1 blank group. to be added




EDIT:     I was able to edit the deactivation script to prevent the last , (comma) from being added to the line.


Hi Steve,



Try this...



if (groupLine != '') {  


  var groupList = groupLine.split(':')[1].split(',');  


  for (var g = 0; g < groupList.length; g++) {  


      if (groupList[g] == '')


                  continue;


  var grMember = new GlideRecord('sys_user_grmember');  


  grMember.newRecord();  


  grMember.user = current.sys_id;  


  grMember.group.setDisplayValue(groupList[g].trim());  


  grMember.insert();  


  }  


}


Here you go



var str = current.u_information;


var lineArr = str.split('\n');  


var groupLine = '';  


var defaultGroupLine = '';  


var emailLine = '';  



for (var i = 0; i < lineArr.length; i++) {  


  if (lineArr[i].indexOf('Groups:') == 0)  


  groupLine = lineArr[i];  



  if (lineArr[i].indexOf('Default Group:') == 0)  


  defaultGroupLine = lineArr[i];  



  if (lineArr[i].indexOf('Email:') == 0)  


  emailLine = lineArr[i];  


}  



current.active = true;  


current.locked_out = false;  



if (groupLine != '') {  


  var groupList = groupLine.split(':')[1].split(',');  


  for (var g = 0; g < groupList.length; g++) {


  if(groupList[g].trim()!=''){


  var grMember = new GlideRecord('sys_user_grmember');  


  grMember.newRecord();  


  grMember.user = current.sys_id;  


  grMember.group.setDisplayValue(groupList[g].trim());  


  grMember.insert();  


  }  


}


}  



if (defaultGroupLine != '') {  


  var defaultGroup = defaultGroupLine.split(':')[1];  


  current.u_default_group.setDisplayValue(defaultGroup.trim());  


}  



if (emailLine != '') {  


  var email = emailLine.split(':')[1];  


  current.email.setDisplayValue(email.trim());  


}  


current.update();  


action.setRedirectURL(current);


Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I'm after the reason for this, why are you doing this? I might have some ideas myself, but would like to hear them from you Steven.



//Göran


Our leadership wants to deactivate users if they have not logged in for 30 days.   (licensing issues)  


to keep SN clean, they want inactive users removed from groups, and emails cleared.



Do to reasons beyond my control,   this is what is required of me and my team.


So we have a script that after a user is deactivated, it clears the email, removes the default group, and removes them from all group membership.


that all works fine.



So a manager must be notified that the user has been deactivated and requires the manager to submit a reactivation request.


After the manager submits a reactivation request, we get a ticket to turn the user account back on.


that requires doing all of this manually.