"packages" is not defined error in logs, var baos = new packages.java.io.ByteArrayOutputStream();

chitturiamala
Tera Contributor

Hi,

I'm trying to integrate two ServiceNow instances. 

Goal: If I create an incident along with attachment in the instance A that should replicate the record & attachments in the instance B. For that I have written one Async - insert BR. If I try to create a record I'm getting - "packages" is not defined error in logs. I'm pasting the code here.

(function executeRule(current, previous /*null when async*/ ) {
 
    try {
 
        var encodedFile;
        var fileName;
        var contentType;
 
        var att = new GlideRecord("sys_attachment");
        att.addQuery('table_name', current.getTableName());
gs.info('my table name' + current.getTableName());
gs.info('table name is :' + ' ' + current.getTableName());
        att.addQuery('table_sys_id', current.sys_id);
        att.query();
        if (att.next()) {
 
            var gsa = GlideSysAttachmentInputStream(att.sys_id.toString());
            var baos = new packages.java.io.ByteArrayOutputStream();
gs.info('my package' + baos);
 
            gsa.writeTo(baos, 0, 0);
            baos.close();
 
            encodedFile = GlideStringUtil.base64Encode(baos.toByteArray());
            fileName = att.file_name;
            contentType = att.content_type;
        }
        var r = new sn_ws.RESTMessageV2('demo_instanceA', 'incident_insert');
        r.setStringParameterNoEscape('short_description', current.short_description);
        r.setStringParameterNoEscape('correlation_id', current.number);
        r.setStringParameterNoEscape('description', current.description);
        r.setStringParameterNoEscape('encodedStr', encodedFile);
        r.setStringParameterNoEscape('fileName', fileName);
        r.setStringParameterNoEscape('fileType', contentType);
 
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;
gs.info('my error' + message);
    }
 
})(current, previous);
 
Could anyone please help me to resolve this issue. Please suggest the alternative method for packages function.
 
Thanks,
Amala.
9 REPLIES 9

Hi @Ankur Bawiskar 

Both the instances are in global scope.

Thanks,

Amala.

@santhoshini2 

then it should work fine

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

It is not working. When I try to attach pdf or excel it is working (attachments are getting copied but can't able to see the content.
When I try to attach excel or word document, docs are not getting copied.

 

Thanks,

Amala.

Hi @Ankur Bawiskar ,

 

Please have a look into this issue. I have tried the above code only PDF is getting attached (excel &  word docs are getting skipped). Moreover attached PDF content is not visible. It is throwing an error. Attaching the screenshot for your reference.

 

Could please check and help me to resolve the issue. Once check the onafter transform map script aswell.

Thanks,

Amala.

Hi @Ankur Bawiskar ,

 

Now I can be able to see the attachments, but the content is missing. Please help to resolve this.

Thanks, 

Amala.