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

Capture.JPG


The current.table_sys_id is returning a sys_id when I log that.   The target table and instance both return nothing in my log.


When I view the sys_email.list the target is there.   See attached


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();


}


Thanks, that seems to work.   Making the business rule run after update does the trick.


Actually, I did one additional thing.   With the condition set as !current.instance.nil() the attachments were being stripped before they were actually sent.   I changed the condition to !current.instance.nil() && current.type == 'sent' which waits to strip the attachment from the email until after it has been sent to the recipient.


You said earlier it is set up in an "after" business rule.


Then you need to add a "current.update()" to save the changed current record.