Select specific attachments to copy

Rob Sestito
Mega Sage

Hey SN Comm,

I have a couple of things that I want to see if they can be enhanced. For my first one, we have a UI Action that grabs the attachments from the HR Case, and posts them to the HR Task: (this is in production)

find_real_file.png

GlideSysAttachment.copy(current.parent.sys_class_name, current.parent, current.getTableName(), current.sys_id);

current.update();
action.setRedirectURL(current);

gs.addInfoMessage("Attachments have been added!");

gsftSubmit(null, g_form.getFormElement(), 'sysverb_update_and_stay');

Then, I have a Business Rule to do the following (which is NOT in production yet): This BR automatically allows the case worker, to have the attachments added to the email (from email client) from the Case/Task (depending on which table I give as the target).

find_real_file.png

(function executeRule(current, previous /*null when async*/) {
	//gs.log('M in loop');
	
	// Add your code here
	var lookatt=new GlideRecord('sys_attachment');
	lookatt.addQuery('table_sys_id',current.instance);
	lookatt.query();
	while(lookatt.next())
		{
		//	gs.log('M in loop again');
		GlideSysAttachment.copy('sn_hr_core_case_operations', current.instance, 'sys_email', current.sys_id);
	}
})(current, previous);

I really want to see if there is a way, at least for the UI Action,'Grab Attachments', to make my options selectable. I think it would be really beneficial to be able to do that with these features.

Anyone able to see/or know if this is possible at all?

Thanks in advance!

-Rob

1 ACCEPTED SOLUTION

Hi Rob,

 

Yeah I was able to replicate the issue in my Personal Development instance as well. Took me a while and have updated the Business Rule. Just update your Business Rule as per the script mentioned below. 

(function executeRule(current, previous /*null when async*/) {
	
	// Add your code here
	var tic_num = '';
	var sub =  current.subject;
	var number = sub.replace(/[^\d.]/g,'');//current.subject.toString().substr(0,10);
	var num = number.substr(0,7);
	var num2 =sub.indexOf(num);
	if(num2 > 3)
	   tic_num = sub.substr(num2-3,num.length + 3);
	else
	  tic_num = sub.substr(0,num.length + num2);
	var ticket_number = tic_num.replace(/\s/g,'');
      // The first 14 characters of the subject are the ticket number. 
      // We need to subtract this number to get the sys_id of the task record.

     var gr = new GlideRecord('incident');  //Replace with your Table Name from which you want to copy to Email Client
     gr.addQuery('number',ticket_number);
     gr.query();
     gr.next();
     
     var arrayAttachment = GlideSysAttachment.copy(gr.sys_class_name, gr.sys_id ,'sys_email', current.sys_id);

     for (var i = 0; i< arrayAttachment.size(); i++) { 
             var attachmentSidsPiece = String(arrayAttachment.get(i)).split(',');
             var sourceAttachmentSysid = attachmentSidsPiece[0];
             var targetAttachmentSysid = attachmentSidsPiece[1]; 
             var sourceRec = new GlideRecord('sys_attachment');
             var targetRec = new GlideRecord('sys_attachment');

     if (sourceRec.get('sys_id',sourceAttachmentSysid) &&       
                               targetRec.get('sys_id',targetAttachmentSysid)) {

       if (sourceRec.u_send_to_email_client==false) {
            //delete all other attachments that are not marked to send via the email client        
            targetRec.deleteRecord();

     } else { //reset the flag
           sourceRec.u_send_to_email_client= false;
           //as soon as the email record is created, reset the flag to false again
           sourceRec.update();
    }
 }
}
	
})(current, previous);

Rest of the code remains the same. Hopefully this time it should work:) 

 

Please let me know if still there is an issue and we can check further.

 

Hope this help. Please mark the answer as helpful/correct based on impact.

 

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

32 REPLIES 32

Hey Sanjiv!

Yes, I have the u_send_to_email_client on the sys_attachment table - default value is false.

find_real_file.png

 

But, I feel like the business rule is doing something. Because when I go to use the normal OOTB email client Icon from the HR Case, the attachments from the Case are being copied to the email then as well. So, it's almost like if I use the UI Action to do what we want, it works - but the BR is still taking all of them regardless.

Thanks,

-Rob

Hi Rob,

 

Yeah I was able to replicate the issue in my Personal Development instance as well. Took me a while and have updated the Business Rule. Just update your Business Rule as per the script mentioned below. 

(function executeRule(current, previous /*null when async*/) {
	
	// Add your code here
	var tic_num = '';
	var sub =  current.subject;
	var number = sub.replace(/[^\d.]/g,'');//current.subject.toString().substr(0,10);
	var num = number.substr(0,7);
	var num2 =sub.indexOf(num);
	if(num2 > 3)
	   tic_num = sub.substr(num2-3,num.length + 3);
	else
	  tic_num = sub.substr(0,num.length + num2);
	var ticket_number = tic_num.replace(/\s/g,'');
      // The first 14 characters of the subject are the ticket number. 
      // We need to subtract this number to get the sys_id of the task record.

     var gr = new GlideRecord('incident');  //Replace with your Table Name from which you want to copy to Email Client
     gr.addQuery('number',ticket_number);
     gr.query();
     gr.next();
     
     var arrayAttachment = GlideSysAttachment.copy(gr.sys_class_name, gr.sys_id ,'sys_email', current.sys_id);

     for (var i = 0; i< arrayAttachment.size(); i++) { 
             var attachmentSidsPiece = String(arrayAttachment.get(i)).split(',');
             var sourceAttachmentSysid = attachmentSidsPiece[0];
             var targetAttachmentSysid = attachmentSidsPiece[1]; 
             var sourceRec = new GlideRecord('sys_attachment');
             var targetRec = new GlideRecord('sys_attachment');

     if (sourceRec.get('sys_id',sourceAttachmentSysid) &&       
                               targetRec.get('sys_id',targetAttachmentSysid)) {

       if (sourceRec.u_send_to_email_client==false) {
            //delete all other attachments that are not marked to send via the email client        
            targetRec.deleteRecord();

     } else { //reset the flag
           sourceRec.u_send_to_email_client= false;
           //as soon as the email record is created, reset the flag to false again
           sourceRec.update();
    }
 }
}
	
})(current, previous);

Rest of the code remains the same. Hopefully this time it should work:) 

 

Please let me know if still there is an issue and we can check further.

 

Hope this help. Please mark the answer as helpful/correct based on impact.

 

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

That was it!

Thanks Shloke! This is amazing!

I appreciate your help with this request!

Cheers,

-Rob

Question on possible enhancement in testing this out.  The first thing I did was add a condition on the UI action of current.hasAttachment() and also set the table to global so I could use it anywhere.  But then I thought maybe I don't have the email client enabled on all tables.  Is there a way to add to the condition so that it only shows if the record has attachments (this part is already done) and email client is enabled for the table?

Hey Brian,

That would be a good enhancement - something I will probably explore for this.

Thanks,

-Rob