Jira to ServiceNow integration using scripted api

manju5
Tera Contributor

Hi,

 

I am sending the attachments from Jira to ServiceNow, it is attaching twice a time, Could anyone help me ?

I have shared the code below please check and let me know?

 

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 
    // implement resource here
 
var att = request.body.data.fields.attachment;
    var attLength = parseInt(att.length);
var id = 0;
    var filename;
for (var i = 0; i<attLength; i++) {
if (id == 0) {
            id = att[i].id;
            filename = att[i].filename;
            attachment(id, filename);
} else if (id < att[i].id) {  
id = att[i].id;
             filename = att[i].filename;
            attachment(id, filename);
         }
}
function attachment(id, filename) {
try {
                    inc = new GlideRecord('incident');
                    inc.addQuery('u_jira', request.body.data.key);
                    inc.query();
                    if (inc.next()) {
                        inc.u_attachment_correlation_id = id;
                        inc.update();
                        var attachment =  new sn_ws.RESTMessageV2('JIRA', 'Default GET');
                        attachment.setStringParameterNoEscape('id', id);
                        attachment.saveResponseBodyAsAttachment('incident', inc.sys_id, filename);
                        var response = attachment.execute();
                        var Status = response.getStatusCode();
                        if (Status != '200')
                            gs.log("Jira issue" + response.getErrorMessage());
                        else
                            gs.log('Jira issue: ' + filename);
                    }
                } catch (ex) {
                    gs.log('ERROR in Jira ' + ex, true);
                }
}
 
})(request, response);
 
Thanks,
Manju
3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi

without having the chance to execute your code it is pretty difficult to answer. 

What debugging activities have you performed so far?

Have you checked how many iterations the outer for loop has? Is it running once or twice?

Maik

manju5
Tera Contributor

Hi @Maik,

 

When I uploaded the first attachment it will execute one time only, When I uploaded second attachment then it will execute twice like attaching First + second attachment.

 

Thanks,

Manju 

 

 

Mathieu Lepoutr
Mega Guru

Hi Manju

Please ensure that each attachment is processed only once. You could maintain a list or a set of processed attachment IDs and check against it before processing a new one!

 

You can also make this integration easier by implementing a 3th party decentralized solution like Exalate.

It is an integration solution, where you can granuarly decide which data will be send over. For attachments, all the meta data will also be sent over!

 

Kind regards,

Matthew