Query for Document ID

amkatulak
Giga Expert

Hi,

I'm trying to use a GlideRecord query to search for a document ID, but it doesn't appear to be working.   Does anyone have an example of how to query for a Document ID?

Here's what I have so far.

var email = new GlideRecord('sys_email');
email.addQuery('sys_id',current.table_sys_id);
email.query();
while (email.next()) {
   gs.log("Target record sys_id " + email.target_table + email.instance);
   gs.log("Attachment created by " + email.sys_created_by);
   gs.log("Attachment created at " + email.sys_created_on);
   gs.log("Attachment " + current.sys_id.toString());

}

 

The email.instance is the document ID field and it does not log anything.

1 ACCEPTED SOLUTION

The issue is the order that things are done.


1) You hit the Client Email button this creates an entry in the sys_email table (with no Target).


2) You add an attachment to the email - This creates the Attachment triggering your business rule (still no target).


3) You hit send the Target (instance) is updated on the email.



Second thought:



Business Rule


when: After


Table: sys_email


Condition: !current.instance.nil()


Script:


var instance = current.instance;


var targetTable = current.target_table;


var gr = new GlideRecord('sys_attachment');


gr.addQuery('table_sys_id',current.sys_id);


gr.query();




while(gr.next()){


gr.table_sys_id = instance;


gr.table_name = targetTable;


gr.update();


}


View solution in original post

16 REPLIES 16

paul_mcnamara
Giga Expert

I ran this in the Scripts - Background and it returns values for the instance field.   I also did a filter for a specific record using the sys_id.   Have you confirmed that the emails you are looking for have the Target set?   Not all records do.




var email = new GlideRecord('sys_email');  


email.query();  


while (email.next()) {  


  gs.print(email.instance);  


}


eican
Kilo Guru

The query looks ok. Can you provide a bit of information what you are trying to accomplish?


Seems like you are running this query in a BR on the attachment table, correct?



When you say "It does not log anything" do you mean that also your custom string is not logged at all?


Yes, this is designed to be an After Insert business rule on the sys_attachment table.   What I'm trying to capture is for when an email is sent out from a task using the Email Client and has an attachment.   From what I have seen, attachments are not captured in the record in this scenario.


Right the attachment is actually associated to the Email not the Task.   Do you want to copy the attachment to the task or try to update the ID on the attachment table and move it?