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

@reddy8055 

Try this.  You will be copying the attachments on the "185119651b9029187ccda687bd4bcbfd" record to the newly created one.

 

var sourceSysId = '185119651b9029187ccda687bd4bcbfd';
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 targetSysId = ptask.insert();
var attachment = new GlideSysAttachment();
attachment.copy('u_property_it_tasks', sourceSysId , 'u_property_it_tasks', targetSysId);

 


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


@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.


Its working now, thank you!!