query to get the records created in current month

Priti18
Tera Expert

below is my script which is copying the data from attachment table to data source and if data source already contains some file then first delete it and then attach it and it should only take those attachments from sys_email to sys_attachment which are received on first of current month.

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

deleteattachment('123'); // SYS_ID of the Data Source

var gr = new GlideRecord('sys_attachment');
gr.addEncodedQuery('file_name=test.csv^table_name=sys_email^table_name=sys_email^

sys_created_onONToday@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()

');

gr.query();
while (gr.next()) {
GlideSysAttachment.copy('sys_email', gr.table_sys_id, 'sys_data_source', '123'); // SYS_ID of the Data Source

}

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

function deleteattachment(id) {
var docattach = new GlideRecord('sys_attachment');
docattach.addEncodedQuery('table_name=sys_data_source^table_sys_id=' + id);
docattach.query();
while (docattach.next()) {
docattach.deleteRecord();
}
}

5 REPLIES 5

please share screenshot of query builder for this 

My requirement is:-

 first check the data source to see if attachment is present then first delete the it and then attach the latest one

the above script does not delete the attachment it simply keeps copying attachment from email to data source table

 

give me query where if run this script twice today then it should first delete the existing attachment from data source first then attach the file which is received form the latest mail.