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

eisenbathb
Kilo Contributor

To parse the string you would be able to use the .split() function to split the string into an array and split at the colon so you can get after the colon in the array for each of the lines.


String.prototype.split() - JavaScript | MDN


As a quick second thought though. I just did something very similar but instead of adding the information to the user table I created a "bad user" table and it moves the information that we wanted to keep to a new table just by mapping over the values. That way you can see which users you have automatically moved the the "bad user" table. You can then put a UI action on the bad user to reactivate that account by moving the information with the fields.


michal29
Mega Guru

Hello Steven,



1.


Primitive solution would be indexOf.


Since you can manipulate the field u_information put something like



e-mail: 'example@com',


groups: ['group1', 'group2', 'group3'],



Then you could indexOf('email') and substring untill termination indexOf, lets say in this case its the coma so:


var user_email   = u_information.substring(u_information.indexOf('email'), u_information.indexOf(',groups'));


But this solution is hard to maintain, and not really flexible.



2.


Although I havent tested it yet, You could try w JSON.


I mean


var strArr = Object.toJSON(u_information);



and then,


var arr = new JSON().decode(strArr);



And access the properties like Object or Array.


But again, I havent tested it and I don't know if it is possible to use JSON there to encode u_information.



Regards,


Michal


Chuck Tomasi
Tera Patron

Here's a first draft, and completely untested script. one assumption, u_default_group is your field as I didn't see it OOB and it's a reference to sys_user_group.




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];


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


  var grMember = new GlideRecord('sys_group_grmember');


  grMember.newRecord();


  grMember.user = current.sys_id;


  grMemeber.group.setDisplayValue(groupList[i]);


  grMember.insert();


  }


}



if (defaultGroupLine != '') {


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


  current.u_default_group.setDisplayValue(defaultGroup);


}



if (emailLine != '') {


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


  current.email.setDisplayValue(email);


}


current.update();


action.setRedirectURL(current);