BR not running using GlideSysAttachment();

surbhi_123
Tera Expert

I have written an after BR on incident table, but it is not working. Please help -

(function executeRule(current, previous /*null when async*/ ) {
    try {
 
        var gr=new GlideRecord('sys_attachment');
        var attachmentSysId = gr.sys_id;
      
        var file = gr.file_name;
        var type = gr.content_type;
   
        var apiEndpoint = 'https://******/api/v2/uploads?filename='+file;
        gs.log('The API endpoint is: ' + apiEndpoint);
 
 
        var attachment = new GlideSysAttachment();
        var gp = attachment.getAttachments('sys_attachment',attachmentSysId);
     
 
        if (gp.next()) {
 
            var r = new sn_ws.RESTMessageV2('Cyber IAM_Zendesk Integration', 'Get token');
            r.setEndpoint(apiEndpoint);
    
            r.setRequestHeader('Content-Type', type);
            r.setRequestBodyFromAttachment(attachmentSysId);
 
            var response = r.execute();
            var responseBody = response.getBody();
            gs.log('responseBody:from br ' + responseBody);
            //return response.getBody();
        }
    } catch (ex) {
        var message = ex.message;
        gs.log("attach fail");
    }
})(current, previous);
4 REPLIES 4

Vismit Ambre
Giga Guru

The second line in your code will not be defined as you are instantiating an object (gr) in line number 1. But gr won't hold any value with it. So gr.sys_id will not be defined. You would need to query the sys_attachment table first before continuing forward.

 

Regards,

Vismit

Sai Shravan
Mega Sage

Hi @surbhi_123 , 

The query(); is missing after the gliderecord. can you try with the below code

(function executeRule(current, previous /*null when async*/ ) {
    try {
        // Replace 'your_condition_here' with your attachment retrieval criteria.
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('your_condition_here');
        gr.query();
        
        if (gr.next()) {
            var attachmentSysId = gr.sys_id;
            var file = gr.file_name;
            var type = gr.content_type;
   
            var apiEndpoint = 'https://******/api/v2/uploads?filename=' + file;
            gs.log('The API endpoint is: ' + apiEndpoint);

            var r = new sn_ws.RESTMessageV2('Cyber IAM_Zendesk Integration', 'Get token');
            r.setEndpoint(apiEndpoint);
            r.setRequestHeader('Content-Type', type);
            
            // Assuming you want to send the attachment as the request body.
            r.setRequestBodyFromAttachment(attachmentSysId);

            var response = r.execute();
            var responseBody = response.getBody();
            gs.log('responseBody from BR: ' + responseBody);
        }
    } catch (ex) {
        gs.error("Error in BR: " + ex.getMessage());
    }
})(current, previous);

 

- replace "your_condition_here" in the line 5 eg: state=available

 

Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

It is still not running. I am putting a log in line 2 to check if this BR is running or not, and no logs is found. I am writing after BR on incidnet table and no conditions are there in br

In this case, there might be a mismatch between the BR when to run condition and the actual testing simulation that you are performing. 

 

Regards,

Vismit