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

I would imagine that a copy would work, but at the same time, I would want an audit of created by and created at, so maybe that would require updating the ID?   I was also thinking of adding a work note at this point to indicate that the attachment was added from the email client.


The simplest way to do this would be a business rule on the sys_attachments table that checks for the table_name of   'sys_email'.



Something like this



if(current.table_name =='sys_email'){


var gr = new GlideRecord('sys_email');


gr.get(current.table_sys_id.toString());


var instance = gr.instance.toString();


var target = gr.target_table;


current.table_name = target;


current.table_sys_id = instance;


}



And then if you wanted to add a worknote do another glide query for your task and add a worknote.



If you decide to copy use this



GlideSysAttachment.copy('sourcetable', 'sys_id', 'destinationtable', 'sys_id');






Thanks, but I must not be doing something right.   This doesn't add the attachment to my record and when I added logging the gs.log(gr.get(current.table_sys_id.toString())) just returns true, and the gs.log(instance) is still blank.


Here's my code, and I have the condition of current.table_name == 'sys_email'


var gr = new GlideRecord('sys_email');


gr.get(current.table_sys_id.toString());


gs.log("Logging the gr.get statement result" + gr.get(current.table_sys_id.toString()));


var instance = gr.instance.toString();


gs.log("Logging the instance " + instance);


current.table_name = 'task';


current.table_sys_id = instance;


Your log statement is returning TRUE because it is successfully getting the record.  


Log this current.table_sys_id.toString() to see that you are getting the table_sys_id



Also after you send the email go check sys_email.list and see if the Target is being set on the email.   If it isn't that would explain why you are not getting an instance back.


Also it looks like you got my code before I edited it.   I updated it to get the Target Table instead of hardcoding 'Task'