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

I'm glad you got your question answered. Thank you for participating in the community.


For what it's worth, I'll be building a variation of this on a live programming session today (Google Hangouts On Air) using JSON to store the results which make it easier to backup and restore those values.



Very informal programming session. Not a rehearsed demo. See how we: build stuff from scratch. share best practices. debug methods when things doing work right.



I'll share the URL and time here as soon as I have it.


Join dave.slusher, josh.nerius and me on a Google Hangout (on Air) at 12PM PT



ServiceNow Live Coding Happy Hour for 2016-08-12 - Google+


Hey Chuck,   thanks for the reply.



So i see your code said it wasn't tested, and that is fine.   it got me something to go on.



However,   nothing works in the script.


so i looked it over and over and over, until i found out one thing.


your line 1.   it's starts with splitting,   but it never said what to get to start the splitting.



so i added this:     var str = current.u_information;


then it would add only the email address.


See my comments, there are few errors in chuck's code.