Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Attachments are not getting copied through scheduled job

reddy8055
Tera Contributor

Hi,

I am trying to generate automatic task in custom table through scheduled job. I want to add attachment as well along with task creation. New record is getting created successfully but attachment is not getting added. How can I achieve this?

var gr = new GlideRecord("sys_attachment");
var x = gr.get("24c0f46d1b94eddc7f27db58b04bcb1d");
var ptask = new GlideRecord('u_property_it_tasks');
//ptask.get('d04fe0e51b9ce5187ccda687bd4bcbb7');
ptask.initialize();
ptask.short_description = 'Weekly Check Sun/Mon';
ptask.description = 'Perform maintenance please refer to the attached document.';
ptask.assignment_group = '92821b43db6db6401c7c5dd5ce9619ca';
var attachment = new GlideSysAttachment();
attachment.copy(gr, x, ptask, current.sys_id);
ptask.insert();

Thanks,

1 ACCEPTED SOLUTION

@reddy8055 

I think the code would be something like this.

var ptask = new GlideRecord('u_property_it_tasks');
ptask.initialize();
ptask.short_description = 'Weekly Check Sun/Mon';
ptask.description = 'Perform maintenance please refer to the attached document.';
ptask.assignment_group = '92821b43db6db6401c7c5dd5ce9619ca';
var sysId = ptask.insert();

var attachment = new GlideSysAttachment();
attachment.copy('u_property_it_tasks', <source sys_id>, 'u_property_it_tasks', sysId);

 

I am still not clear from where you are copying the attachment and that is why I put <source sys_id>. 

I assume this new task is related somehow to the source. Maybe it is a child of it. If that would be the case, I would have expected adding ptask.parent = <something> and that something is where you would be copying the attachments from. Then the <source sys_id> would be ptask.parent


If I helped you with your case, please click the Thumb Icon and mark as Correct.


View solution in original post

7 REPLIES 7

Sebas Di Loreto
Kilo Sage

@reddy8055 

I think the parameters you are providing to the copy command are wrong (check here).

The first and third parameter should be table names. I know the third one would be "u_property_it_tasks" but from what other record are you copying the attachments?

In addition, you would add the attachments after the new task is inserted.

 

SebastianDL_0-1673303096101.png

 


If I helped you with your case, please click the Thumb Icon and mark as Correct.


Hi,

I am copying from "u_property_it_tasks" table itself and copying from one record which have attachment to another one which will generate via scheduled job every day.

Thanks,

@reddy8055 

What are you trying to achieve with this command?

That sys_id is just a record on the sys_attachment table that does not represent the source record where you are trying to copy the attachments. That field would table_sys_id but it is irrelevant to what you are trying to achieve?

var gr = new GlideRecord("sys_attachment");
var x = gr.get("24c0f46d1b94eddc7f27db58b04bcb1d");

If I helped you with your case, please click the Thumb Icon and mark as Correct.


I have removed those two lines and not sure what to add in fourth parameter (ptask.getUniqueValue()), I cannot add sys_id because this schedule job will generate new record.

 

var ptaskSysID = '185119651b9029187ccda687bd4bcbfd';
var ptask = new GlideRecord('u_property_it_tasks');
ptask.getValue('incidentSysID');
ptask.initialize();
ptask.short_description = 'Weekly Check Sun/Mon';
ptask.description = 'Perform maintenance please refer to the attached document.';
ptask.assignment_group = '92821b43db6db6401c7c5dd5ce9619ca';
var attachment = new GlideSysAttachment();
attachment.copy('u_property_it_tasks', ptaskSysID, 'u_property_it_tasks', ptask.getUniqueValue());
gs.info('Copied attachments: ' + copiedAttachments);
ptask.insert();

 Thanks,