JSON.parse() stopped a Business Rule running
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2024 01:49 AM
Hi all. I have a Business Rule that triggers a REST API call that sends an attachment to one of our vendors. It's worked fine for two years with no updates. Last week it stopped working for no reason - no update sets deployed, no updates to the BR, no patches\updates installed. After adding gs.info messages to almost every line, I concluded the following line was preventing the API call and nothing after this line was being run.
var parsedJSON = JSON.parse(responseBody);
As soon as I commented out this line, the API worked and the rest of the code after this line worked. Why would this suddenly kill the BR?
Complete code below:
(function executeRule(current, previous /*null when async*/ ) {
// Get the attachment object from the sys_attachment table using the sys_id from the current record
var att = new GlideRecord('sys_attachment');
att.get(current.u_attachment_file_sysid);
var gsa = GlideSysAttachmentInputStream(att.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos, 0, 0);
baos.close();
var encodedFile = new GlideStringUtil.base64Encode(baos.toByteArray());
var fileName = att.file_name.toString();
var contentType = att.content_type.toString();
// get the incident object as we'll need to update it later
var incGr = new GlideRecord('incident');
incGr.get(current.u_task);
var body = {};
body.ParentId = incGr.getValue('u_vendor_sys_id');
body.Name = fileName;
body.Body = encodedFile;
var request = new sn_ws.RESTMessageV2('XXXX Incident Integration', 'Send Attachment'); // REST Message Name, Method Name
request.setRequestBody(JSON.stringify(body));
// Set the endpoint, subscription key, contact email, and account ID HTTP Headers dynamically based on our instance
var endpoint = '';
var subkey = '';
var contactEmail = '';
var accountId = gs.getProperty('XXXX.by.integration.account_id');
if (gs.getProperty('instance_name') == 'XXXX') {
endpoint = gs.getProperty('XXXX.by.integration.endpoint.send.attachment.prod');
subkey = gs.getProperty('XXXX.by.integration.subscriptionkey.prod');
contactEmail = gs.getProperty('XXXX.by.integration.contactemail.prod');
} else {
endpoint = gs.getProperty('XXXX.by.integration.endpoint.send.attachment.test');
subkey = gs.getProperty('XXXX.by.integration.subscriptionkey.test');
contactEmail = gs.getProperty('XXXX.by.integration.contactemail.test');
}
request.setEndpoint(endpoint);
request.setRequestHeader('Ocp-Apim-Subscription-Key', subkey);
request.setQueryParameter('account-id', accountId);
request.setQueryParameter('user-name', contactEmail);
var response = request.execute();
var requestBody = request.getRequestBody();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var errorMsg = response.getErrorMessage();
//var parsedJSON = JSON.parse(responseBody); // Commented this out because it seemed to stop the code past this point and the request being sent
// If we get a successful status code, update our incident
if (httpStatus == '200') {
current.u_attachment_sent = true;
current.update();
} else gs.info('Send Attachment to XXXX - Failed to update incident! Status code: ' + httpStatus + ', with error message: ' + errorMsg);
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2024 02:08 AM
Hi,
Check the content of responseBody. There is a possibility that the format of API response is changed from JSON to a different format which could have caused this issue.
Thank you,
Palani
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2024 02:38 AM
Interesting idea, Palani. Now that you mention it, when I reviewed the payload from Postman, I saw HTML content in the response.