Create a new task for each line in Multi Line text

MattKing
Tera Contributor

Hi All,

Hopefully someone can help:

I have a multi line text variable on a catalog item that is workflow driven. The end user enters data which contains a date and email address for multiple employees, each employee is separated by a new line, and each date/email address is separated by a tab, eg.: (the space is a tab, I can't paste a tab)

7/1/2022 firstname.surname@testemail.com
22/3/2023 first.last@example-email.com

I would like to add a run script activity in the workflow that would:

  1. look up the corresponding user from the given email address and grab the user_name from sys_user
  2. format the date to DD-mm-YYYY
  3. Create a new catalog task for each line with the description in the following format:
    "Example Text" + <user_name> + "Example Text" + <DD-mm-YYYY>
  4. Set the short description of each task to the same thing each time
  5. Assign the task to the same assignment group each time

The amount on the list changes each time, so one request could have 3 users listed, another could have 50.

I hope this makes sense. I could probably do this myself if the description was the same each time, but I need it to change for each task.

Thanks in advance for your help.

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

your script would be like this

createTasks();

function createTasks(){

	var val = current.variables.myVariableName; // give name of variable here
	var groupSysId = 'sysId'; // give the group sysId here

	var arr = val.toString().split('\n');
	for(var i in arr){

		var date = arr[i].toString().split(' ')[0];
		var email = arr[i].toString().split(' ')[1];

		var userName;
		var gr = new GlideRecord("sys_user");
		gr.addQuery("email", email);
		gr.query();
		if (gr.next()) {
			userName = gr.user_name;
		}

		var myFormattedDate = getMyDate(date); // call the function to get the formatted date

		var desc = "Example Text" + userName + "Example Text" + myFormattedDate;

		var rec = new GlideRecord("sc_task");
		rec.initialize();
		rec.request_item = current.sys_id;
		rec.request = current.request;
		rec.short_description = 'same text';
		rec.description = myFormattedDate;
		rec.assignment_group = groupSysId;
		rec.insert();
	}

}

function getMyDate(date){

	var dateFormat = 'dd/MM/yyyy HH:mm:ss';
	var myDate = date + ' 00:00:00';

	var gdt = new GlideDateTime();
	gdt.setDisplayValue(myDate, dateFormat);

	var dt = new GlideDate();
	dt.setValue(gdt.getLocalDate());
	var myFormat = dt.getByFormat("dd-MM-yyyy");

	return myFormat;
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@MattKing 

Hope you are doing good.

Did my reply answer your question?

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Weird
Mega Sage

For starters you need to figure out how to separate the text based on tab and the new line.

Let's start with the line break. It should be splittable with "\n":

var text = current.description.split('\n');

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

}

Here we'd get the different lines on their own indexes
text[0] = 7/1/2022 firstname.surname@testemail.com
text[1] = 22/3/2023 first.last@example-email.com

Then we need to handle what's inside each index and have that information parsed as well:
For this you could try using split('\t');

var text = current.description.split('\n');

for(var i = 0; i < text.length; i++){
 var context = text[i].split('\t');
}

Now we have a for loop which will loop through each row and inside that an array which holds content of the row in separate indexes.
Loop 0 goes through = "7/1/2022 firstname.surname@testemail.com" and inside it the array starts with "7/1/2022"

If the format is always the same, you can then just process everything with that assumption.

createTask();

function createTask(){

	var text = current.description.split('\n');

	for(var i = 0; i < text.length; i++){
		taskCreation(text[i]);

	}
}

function taskCreation(content){
	var context = content.split('\t');
	var date = context[0];
	var gd = new GlideDate();
	gd.setDisplayValue(date , "dd/MM/yyyy");
	var dateValue = gd.getByFormat("dd-MM-yyyy");
	var userName = getUser(context[1]);
	var description = 'Example Text ' + userName + ' Example Text ' + dateValue;

	var gr = new GlideRecord('table_name');
	gr.initialize();
	gr.setValue('short_description', 'what you want');
	gr.setValue('description', description);
	gr.setValue('assignment_group', '<group_sys_id>');
	gr.insert();
}

function getUser(mail){
	var userRec = new GlideRecord('sys_user');
	userRec.addQuery('email', mail);
	userRec.query();

	if(userRec.next()){
		return userRec.getValue('user_name');
	}else{
		//no user found...
		return 'user not found!';
	}

}

 

In full we handle each row separately and for each we format the date, get a user and then we create a record into whatever table we want to. Here you'll need to replace the table_name, short_description and group sys_id.

Now I didn't test it in a SN instance, but you can give it a try in background script first.
I'm assuming here that servicenow recognizes tab as \n and new line as \n