Strip or replace quotes " from Work notes

Pierre5
Kilo Expert

Hoping someone can help. My knowledge is very limited and I didn't write any of the code mentioned here.

We have a business rule (Task [task] that updates the body of a Zendesk ticket (third party ticketing system) via TASK. Everything works fine in regards to adding/posting updates to Zendesk, with the except of Quotes.

find_real_file.png

The only issue we have is; IF the Work notes contain Double Quotes (") the Zendesk ticket is not updated.

find_real_file.png

The error refers to this part of code I think... lines 111 - 121 - I think I need some extra code added to strip or replace the ", Not sure to be honest, but a string of some kind needs adding.

find_real_file.png

This is the full code:

(function executeRule(current, previous /*null when async*/) {
	
	gs.addInfoMessage("Task Type : "  + current.sys_class_name);
	if(current.work_notes.changes()) {
		gs.addInfoMessage("Worknotes changed ");
	}
	
	if(current.comments.changes()) {
		gs.addInfoMessage("comments changed ");
	}	
	
	if(current.state.changes()) {
		gs.addInfoMessage("state changed ");
	}
	

	
	if (gs.getProperty('integrations.zendesk.enable') == 'true') {
		var status = 'new';
		var fileToken = '';
		
		//State Mapping
		if (current.sys_class_name == 'incident'){
			if (current.state == '1' ) {
				status = 'new';
			} else if (current.state == '2') {
				status = 'open';
			} else if (current.state == '3') {
				status = 'hold';
			} else if (current.state == '6') {
				status = 'solved';
			} else if (current.state == '7') {
				status = 'solved';
			} else if (current.state == '8') {
				status = 'solved';
			}
		} else if (current.sys_class_name == 'incident_task'){
			if (current.state == '1' ) {
				status = 'open';
			} else if (current.state == '2') {
				status = 'open';
			} else if (current.state == '-5') {
				status = 'open';
			} else if (current.state == '3') {
				status = 'solved';
			} else if (current.state == '4') {
				status = 'solved';
			} else if (current.state == '7') {
				status = 'solved';
			}
		}
		
		if(current.hasAttachments()){
			var max_size = 5000000; //5 MB
			var count = 1;
			var cgiAttch = new GlideRecord('sys_attachment');
			cgiAttch.addQuery('table_sys_id',current.sys_id);
			cgiAttch.addQuery("sys_created_on", '>=', 'javascript:gs.minutesAgo(30)'); //Created in last 30 mins
			cgiAttch.query();
			while(cgiAttch.next()){
				var sa = new GlideSysAttachment();
				var binData = sa.getBytes(cgiAttch);
				var size = parseInt(cgiAttch.size_bytes);
				if(size > max_size) {
					//gs.addInfoMessage("Ignoring sending of attachment to remedy, size exceeds 2MB");
					gs.log('Ignoring sending of attachment to remedy, size exceeds 2MB','Remedy Integration');
					//current.setAbortAction(true);
				} else {
					if (count == '1' ||  gs.nil(fileToken)) {
						fileToken = getAttachmentToken(cgiAttch.file_name,GlideStringUtil.base64Encode(binData),'');
						count = count + 1;
						gs.log("File Token: " + fileToken);
					} else {
						getAttachmentToken(cgiAttch.file_name,GlideStringUtil.base64Encode(binData),fileToken);
					}
				}
			}
		}		
				
		
		var r = new sn_ws.RESTMessageV2('Zendesk', 'Update');
		r.setStringParameterNoEscape('zendesk_domain', gs.getProperty('integrations.zendesk.domain'));
		r.setStringParameter('fileToken',fileToken);
		r.setStringParameterNoEscape('ticketId', current.correlation_id);
		if (status == 'solved'){
			r.setStringParameterNoEscape('closed_code', 'closed_in_sn');
		}
		r.setStringParameterNoEscape('status', status);
		

		if (current.sys_class_name == 'incident_task' && current.work_notes.changes()) {
			r.setStringParameter('comments', new global.JSON().encode(current.work_notes.getJournalEntry(1)));
		}
		
		if ((current.sys_class_name == 'incident')&&(current.state == '6')) {
			var notes = '';
			notes = 'Restoration Item: ' + current.u_closure_item;
			notes += '\n Restoration Code: ' + current.close_code;
			notes += '\n Restoration Notes: ' + current.close_notes;
			
			comm=current.work_notes.getJournalEntry(1).toString();
			notes += '\n Work Notes: ' + comm;
			r.setStringParameter('comments', new global.JSON().encode(notes));
			
			comm=current.comments.getJournalEntry(1).toString();
			notes += '\n Additional Comments: ' + comm;
			r.setStringParameter('comments', new global.JSON().encode(notes));
		}
		
		
		var response = r.execute();
		var responseBody = response.getBody();
		var httpStatus = response.getStatusCode();
		if (httpStatus == '200') {
			var parser = new JSONParser();
			var parsed = parser.parse(response.getBody());
			var ticketId = parsed.ticket.id;
			gs.addInfoMessage("Zendesk incident "+ ticketId.toString() + " updated successfully");
		} else {
			gs.addErrorMessage("Error while updating zendesk incident " + responseBody);
		}
		

	}
	
	function getAttachmentToken(fileName, fileData, tokenData) {
		var returnToken = '';
		var attachmentRequest = new sn_ws.RESTMessageV2('Zendesk','CreateAttachment');
			attachmentRequest.setStringParameter('zendesk_domain', gs.getProperty('integrations.zendesk.domain'));
			attachmentRequest.setStringParameter('file_name', fileName);
			attachmentRequest.setStringParameter('file_data', fileData);
			if (!gs.nil(tokenData)){
				attachmentRequest.setStringParameter('file_token', tokenData);
			}
			

		var responseResponse = attachmentRequest.execute();
		var attachmentHttpStatus = responseResponse.getStatusCode();
		if (attachmentHttpStatus == '201') {
		var parsedAttachment = new global.JSON().decode(responseResponse.getBody());
			returnToken = parsedAttachment.upload.token;
			gs.log("Attachment Response fileToken: " + returnToken + 'Zendesk Integration');
			return returnToken;
		} else {
			gs.addErrorMessage("Error while sending attachment to zendesk " + fileName);
		}
		return returnToken;
		
	}		
	
	
})(current, previous);

Your help would be much appreciated,

Kind regards,

Pierre

57 REPLIES 57

Okay, looks like the issue is not related to json parsing, you are not getting correct response body at all.

You are getting httpStatus as 422 which is not success[ok]. We need to find out why you are not getting response status of 200 and proper body.

Thanks.

Okay, thanks ASHA. What's my next step please? I had a full head of hair when I first created this post. 😞

Hi,

@Pierre 

from what I could see is you are getting response from the endpoint that your request is not valid json

please try printing what request body is being sent over

That would help you know where it is failing

var response = r.execute();

gs.info('Request body is: ' + r.getRequestBody());
var responseBody = response.getBody();

Also why you want to encode comments it is already a string value

please try to update below lines and test once

if (current.sys_class_name == 'incident_task' && current.work_notes.changes()) {
r.setStringParameter('comments', current.work_notes.getJournalEntry(1));
}

if ((current.sys_class_name == 'incident')&&(current.state == '6')) {
var notes = '';
notes = 'Restoration Item: ' + current.u_closure_item;
notes += '\n Restoration Code: ' + current.close_code;
notes += '\n Restoration Notes: ' + current.close_notes;

comm=current.work_notes.getJournalEntry(1).toString();
notes += '\n Work Notes: ' + comm;
r.setStringParameter('comments', notes);

comm=current.comments.getJournalEntry(1).toString();
notes += '\n Additional Comments: ' + comm;
r.setStringParameter('comments', notes);
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Can you replace

r.setStringParameter('comments', new global.JSON().encode(notes));

with

r.setStringParameterNoEscape('comments', new global.JSON().encode(notes)); 

in the script above where you have worknotes & comments updated.

It appears in two places.. I tried in both areas to be sure, but none worked

find_real_file.png

Replacing with:

r.setStringParameterNoEscape('comments', new global.JSON().encode(notes)); 

as you suggested, but no luck..