Adding attachments to Email

Adityanshu Sing
Tera Contributor

We want to send attachment available in sctask1 through email .The email gets trigger after closure of sctask 6. We wrote the business rule at certain condition but it is not working . can anyone please help me how to get this ?

 

 

 

 

 

 

(function executeRule(current, previous /*null when async*/ ) {

//    var ins = current.instance;
    var at = new GlideRecord("sc_task");
	at.addQuery('short_description', 'STARTSWITH', 'Attach Signed Lease');
	at.query();
	while(at.next()){
		var syid= at.getUniqueValue();
	}
	var gr = new GlideRecord("sc_task");
    var ins = gr.get(current.instance);
    if (ins) {
        var grSysAtt = new GlideRecord('sys_attachment');
        grSysAtt.addQuery("table_sys_id", syid);
        grSysAtt.query();
        while (grSysAtt.next()) {
            var content = new GlideSysAttachment().getContentStream(ins.sys_id);
            new GlideSysAttachment().writeContentStream(current, grSysAtt.getValue('file_name'), grSysAtt.getValue('content_type'), content);
        }
    }

})(current, previous);

 

 

Capture21.PNG

 

 

4 REPLIES 4

The Matrix
Tera Contributor

HI @Adityanshu Sing Try below Code 

(function executeRule(current, previous /*null when async*/) {

    var task1 = new GlideRecord("sc_task");
    task1.addQuery('short_description', 'STARTSWITH', 'Attach Signed Lease');
    task1.query();
    var task1SysId = '';
    if (task1.next()) {
        task1SysId = task1.getUniqueValue();
    }

    if (task1SysId) {
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', task1SysId);
        attachmentGR.query();
        
        var attachments = [];
        while (attachmentGR.next()) {
            attachments.push(attachmentGR.sys_id.toString());
        }

        if (attachments.length > 0) {
            var email = new GlideEmailOutbound();
            email.setSubject('Attachments from sc_task');
            email.setBody('Please find the attachments from sc_task.');
            email.addAddress('to', 'recipient@example.com');
            for (var i = 0; i < attachments.length; i++) {
                email.addAttachment(attachments[i]);
            }

            email.send();
        }
    }

})(current, previous);

 

Regards,

Sid

Hi @The Matrix - I haven't found any official documentation or evidence that GlideEmailOutbound includes an addAttachment method. Is this code perhaps generated by a GPT model? From my understanding, there isn’t a built‑in way to attach files directly with GlideEmailOutbound. This is supported by KB0789188:

 

As of now there is no attachment manipulation is available in GlideEmailOutbound from scripting layer.

Adityanshu Sing
Tera Contributor

@The Matrix , We want to include the sctask description in email body and also need to send email with assignment group in cc. can you please help me how to get this with your logic or how can we leverage the notification (created on sc_task table) in this logic  ? 

Najmuddin Mohd
Mega Sage

Hi @Adityanshu Sing ,

In the related link of the Notification, click Advance View

Then, in 'What it will contain', click 'Include Attachments'

 

NajmuddinMohd_1-1739724574119.png



If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.