- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2014 10:56 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2014 01:50 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2014 12:14 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2014 12:26 PM
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2014 12:47 PM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2014 12:52 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2014 12:53 PM
Also it looks like you got my code before I edited it. I updated it to get the Target Table instead of hardcoding 'Task'