Help with Inbound Email actions to update open SCTASK based on parent ID

Petra Zelenak
Tera Contributor

We have a rather complex workflow where several Tasks and events are triggered as the process goes on. In particular there is a section where after closing a Task it triggers an event, which then sends out an E-mail to a specific group.

Email runs on the Catalog Task [sc_task] table referencing the RITM number in the Subject so we can pull all relevant information. 

Now here comes my issues. The Group would reply to this E-mail with attachments, I need to add this reply via an Inbound E-mail action to the currently open Tasks (it may be several or only one).

 

On the Inbound Email Action:

Target table: Catalog Task [sc_task]

Action type: Record action

When to run:

Subject starts with: "xyz" and

Recipient is "abc"

 

As for the Actions and the Script we have so far a general one that updates the SCTASK if the Subject line contains the SCTASK

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

current.request.requested_for = gs.getUserID();
var emailSubject = email.subject;
var taskIndex = emailSubject.indexOf("SCTASK");
if (taskIndex > -1) {
//valid only if SCTASK is the LAST word in the subject. Failsafe for space in case of in the middle
// but that is not bulletproof.
var taskNumber = emailSubject.substr(taskIndex, emailSubject.length-taskIndex);

var taskGR = new GlideRecord("sc_task");
taskGR.addQuery("number", taskNumber.split(" ")[0]);
taskGR.setLimit(1);
taskGR.query();
if (taskGR.next()) {
taskGR.work_notes = email.body_text;
taskGR.update();
}
}

})(current, event, email, logger, classifier);

 

but this is not quite fitting for my new requirements and I am not so good with script, can someone help me extend to the script to add:

- based on the RITM Number in the Subject line update the open SCTASKs Activity with the Email and add Attachments as well.

 

Thank you.

1 REPLY 1

johnfeist
Mega Sage
Mega Sage

Hi Petra,

There are a couple of things that you need to do.  Once you have taken care of the initial record, you need to do a query against sc_task to get all tasks that are part of the open requested item.  Be sure to limit the query to open tasks and exclude the one that has already been updated.

 

If there are additional tasks, go through them one by one applying any any direct data, e.g. comments, etc.  Then you need to copy the attachments.  Here's a script that will do that

var attachments = new GlideRecord("sys_attachment");
var task_attach = new GlideSysAttachment();
attachments.addQuery("table_name", "sc_task");
attachments.addQuery("table_sys_id", current.sys_id);
attachments.query();
	
if (attachments.getRowCount() > 0) {
	attachments.next();
	task_attach.initialize();
	task_attach.copy("sc_task", attachments.table_sys_id, "sc_task", <sys_id of the record getting the attachments>);
}

The copy function will copy all attachments at once.

 

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster