Rick Forristall
Tera Guru

Are you having trouble copying attachments from one record to another when submitting a catalog item in service portal? 

So was I. Here's what I found to fix it.

In a workflow, using a run script activity, I was trying to copy attachments from the submitted requested item to a created sc_task record. It simply would not work. I spent half a day scratching my head.

Here's the line I was using:

GlideSysAttachment.copy('sc_req_item', current.sys_id, 'sc_task', workflow.scratchpad.newTaskSysID);

In scripts background It worked - the same code - so I knew the code was correct, but why was it failing in the workflow!?

Turned out that the workflow was not the problem.

The problem was that the catalog item was being submitted in the service portal. 

C'mon Rick - no way it was the service portal.

I kid you not! Here's what I found out.

I did a sys_attachment.list to see the list of attachments being added to that table. Immediately after I submitted the service portal catalog item I noticed something really strange in the newly added attachments. The table name was NOT sc_req_item! Instead - for a brief time - the table name was 'sp_portal'.

So I made an assumption. 

When the GlideSysAttachment.copy() method ran, there was no record in sys_attachment for sc_req_item for the given sys_id. Only after a few seconds did the list of attachments change so the newly created attachments had 'sc_req_item' as the table name.

find_real_file.png

So, I said to myself,

"Rick, maybe if you can force a wait for a few seconds it will work!?"

And what do you know - it did.

I set a timer activity in the workflow (4 seconds) immediately prior to the run script activity where I was trying to copy the attachments. That 4 seconds did it. After that I was getting all my sc_req_item attachments in the newly created sc_task records.

Here are two specific use cases - each needing a different approach to inserting a wait time.

1. From a catalog item's list collector I needed to create a separate catalog task for each of the selected options in the list collector. I used a run script activity in the workflow to create separate catalog tasks for each option - I also stored the new catalog task sys_ids in an array and stored in a workflow.scratchpad variable. Then I needed to copy the sc_req_item attachments to each of the new catalog tasks. This was a simple fix - create a timer and wait 4 seconds before running the next run script activity. In that next run script activity, I cycled through the array of catalog task sys_ids and ran the GlideSysAttachment.copy() method - bingo -- it worked.

2. The second was more tricky. It was a straightforward workflow where I create a catalog task (using the workflow create catalog task activity). But then I needed to somehow create a pause before trying the GlideSysAttachment.copy() method. I also needed to wait for the catalog task to be completed before moving forward in the workflow, but I had to turn off the [ ] wait for completion option so I could do the attachments copy process. I addressed this by

(1) storing the new task's sys_id (var newSysID = task.setNewGuid()) into a workflow.scratchpad variable.

(2) adding a timer to wait 4 seconds after the catalog task was created.

(3) adding a run script activity that copied the attachments from sc_req_item to sc_task

(4) Creating a 'wait for condition' on the catalog task to wait until the task was completed (code below) - I needed to do this to replicate the 'wait until completed' option in the create catalog task activity.

// Set the variable 'answer' to true or false to indicate if the condition has been met or not.
answer = false;
var ctask = new GlideRecord('sc_task');
if (ctask.get(workflow.scratchpad.newTaskSysID)) {
	if (ctask.getValue('state') == 3) {
		answer = true;
	}
}

(5) Once that condition hit (the sc_task was closed) then the workflow continued - closing the ritm and request.

Let me know if you have any questions.

 

(Updated):

Here's a screen shot of the workflow activities - circled in red are those that allow the #2 use case above to work:

find_real_file.png

 

Version history
Last update:
‎08-20-2018 02:02 PM
Updated by: