The CreatorCon Call for Content is officially open! Get started here.

Upload attachment from email to the existing Data source

vijay39
Giga Expert

HI Folks,

I have created a Data Source (Testexample.csv) and it has an attachment. My requirement is when ever service-now receives email (from inbound email action) that has an attachment also, i want to update the attachment in the Data source with this new one. I want to delete the already existing attachment in data source and want to attach the new attachment that has come up with email. I written the following code but in vain,

var dataSourceID = "e50bc6bbdb92bf80f0463ccb7c963476";

//Query the attachments for this data source and delete them
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name','sys_data_source');
gr.addQuery('table_sys_id',dataSourceID);
gr.query();

gr.deleteMultiple();

//Copy attachment from the Email to the Data Source
GlideSysAttachment.copy('sys_email',sys_email.sys_id,'sys_data_source',dataSourceID);

 

So can some one help me on this with code and clear explanation.

 

Regards,

Vijay

6 REPLIES 6

asifnoor
Kilo Patron

Hi Vijay,

The code looks correct. Could you tell me what issue you are facing?

Records are not getting deleted or the new record not being copied?

Try adding this code before gr.multiple() to check the output

if(gr.next()) {

gs.log("reocrds exist" +gr.sys_id);

}

Mark the comment as helpful if it helps to solve.

 

Hi Asif,

 

Thanks for your response.

I have added if(gr.next()){

gr.deleteMultiple();

}

 

But the existing attachment is not getting updated with the one that is coming from mail. Existing attachment is not getting deleted.

I have scheduled an import, when i add the attachment manually to the existing data source and click on execute now in scheduled import, then the data is updating in target table. But i want to add the attachment that comes from mail after deleting the existing attachment.

i am getting "No Records to display" when i keep gs.log("reocrds exist" +gr.sys_id);

 Please Note: This data source got created by system when i was uploading the CSV file through import sets and transform maps.

Regards,

Vijay

Narendra Kota
Mega Sage

Hi,

What sys_id's you are using in GlideSysAttachment function?

GlideSysAttachment.copy('sys_email',current.sys_id,'sys_data_source','data_source_sys_id');

Use current.sys_id and proper data_source_sys_id there.

Hope this helps.
Mark Helpful or Correct based on impact.

Thanks.

Hi Narendra,

Thanks for your reply. Actually now i am able to delete the existing attachment and attach the attachment that is coming from email. Its happening perfectly. But, in Email logs Target is showing empty and Processed is showing as Skipped. It is Processing another Inbound Action.Its strange.

So what should i do here?

 

Regards,

Vijay