- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2014 10:25 AM
I am trying to import Word Documents to ServiceNow Knowledge Articles with out losing any images. I have tried using the Word Cleaner and SED installs without any success. Is there any fix that works?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2014 07:52 AM
IMPORTANT UPDATE - 03/03/2016:
I updated the entire script as I ran into some errors. And now it can also be used with journal fields!
See my reply: Re: Import of Word Documents to ServiceNow Knowledge base
You can also read more about it here: Create Knowledge Base Article (HTML field) or Comments (Journal field) from Email
you corrected the thrown exception errors on 4 of the lines in the script.
example on your line 18 with the part:
while(match == regex.exec(searchBody)){
the actual script is
while(match = regex.exec(searchBody)){
I know it throws an errors, but that's somehow required for the script to work. Because later you're going to use the value in the variable 'match' that has been set in line 18... If somebody has any idea how to formulate this correctly, you're welcome to help out.
Here is the full script that I use:
var gdt = new GlideDate();
gdt.addDays(2);
//current.valid_to = gdt;
var desc = email.subject;
desc = desc.replace("Knowledge:","");
current.short_description = desc.trim();
current.workflow_state = "draft";
current.topic = "General";
//current.category = "";
current.roles = "knowledge";
//Find and replace the image tags with the proper source.
//Get the number of attachments so the loop can be exited
//so it will stop no mater what.
var currentCount = 0;
var newBody = email.body_html;
var searchBody = email.body_html.replace(/\n/g, " ");
var regex = /<img(.*?)>/ig;
var match;
var match2;
while(match = regex.exec(searchBody)){
//Add a style float tag next to the align tag.
var alignText = match[0].replace(/align=['"]?left['"]?/gi, 'align="left" style="FLOAT: left"');
alignText = alignText.replace(/align=['"]?right['"]?/gi, 'align="right" style="FLOAT: right"');
searchBody = searchBody.replace(match[0], alignText);
var regex2 = /src=("(.)*?"|'(.)*?'|(.)*?\s+$)?/ig;
while(match2 = regex2.exec(alignText)){
findAndReplaceImage(match2[1].replace(/\s+$/,"").replace(/"+/g,"").replace(/'+/g,""));
}
currentCount += 1;
if(currentCount >= 100)
break;
}
searchBody = searchBody.replace(/<o:p>(.*?)<\/o:p>/ig, "");
currentCount = 0;
var regex2 = /<!--\[if(.*?)<!\[endif\]-->/ig;
while(match = regex2.exec(searchBody)){
searchBody = searchBody.replace(match[0], "");
currentCount += 1;
if(currentCount >= 100)
break;
}
currentCount = 0;
var regex2 = /<!\[if !vml\]>(.*?)<!\[endif\]>/ig;
while(match = regex2.exec(searchBody)){
searchBody = searchBody.replace(match[0], match[1]);
currentCount += 1;
if(currentCount >= 100)
break;
}
//gs.log("Create Morning Post: " + searchBody);
current.text = searchBody;
current.insert();
event.state="stop_processing";
function getEmailSYSID(emailuid) {
var em = new GlideRecord('sys_email');
em.addQuery('uid', emailuid);
em.query();
while(em.next()) {
//we execute the return only within a certain time difference between the creation of the attachment and now (in seconds)
var dif = gs.dateDiff(em.sys_created_on, gs.nowNoTZ(), true);
//gs.log("difference: " + dif + "eid: " + em.sys_id + " uid: " + em.uid); //debug
if(dif < 300 && dif > -300){
//gs.log('passed dif if: ' + em.sys_id + " uid: " + em.uid); //debug
return em.sys_id;
}
}
return "";
}
function findAndReplaceImage(imageText){
var img = imageText;
var imgName = img.substring(4, img.search(/@/i));
var imgCode = "sys_attachment.do?sys_id=";
//Get the sys_id of the attachment
var gr = new GlideRecord("sys_attachment");
gr.addQuery("file_name", imgName);
gr.addQuery("table_sys_id", getEmailSYSID(email.uid));
gr.query();
if (gr.next()) {
imgCode += gr.sys_id;
}
searchBody = searchBody.replace(img, imgCode);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2015 07:04 AM
Hi,
Im trying to get this to work in fuji as well. We have eureka now and its working like a charm there. I have my dev-instance with fuji where I try to get it to work as well. I stumple into an error that is above my knowledge and looking for some help.
If I Use the code and comment the whole part with images it works like it should, but if I try to uncomment it and run it, it doesnt work.
I get on the inbound mail: Skipping 'Create KB', did not create or update kb_knowledge
And looking in the log it says:
worker.0 WARNING *** WARNING *** Javascript compiler exception: The undefined value has no properties. (sysevent_in_email_action.f591746d4f6f4600028f7ab28110c7a6; line 25) in:
var gdt = new GlideDate();
gdt.addDays(2);
//current.valid_to = gdt;
var desc = email.subject;
desc = desc.replace("Knowledge:","");
current.kb_knowledge_base = 'a7e8a78bff0221009b20ffffffffff17';
current.short_description = desc.trim();
current.workflow_state = "draft";
current.topic = "General";
current.roles = "knowledge";
//Find and replace the image tags with the proper source.
//Get the number of attachments so the loop can be ex...
Here is the script:
var gdt = new GlideDate();
gdt.addDays(2);
//current.valid_to = gdt;
var desc = email.subject;
desc = desc.replace("Knowledge:","");
current.kb_knowledge_base = 'a7e8a78bff0221009b20ffffffffff17';
current.short_description = desc.trim();
current.workflow_state = "draft";
current.topic = "General";
current.roles = "knowledge";
//Find and replace the image tags with the proper source.
//Get the number of attachments so the loop can be exited
//so it will stop no mater what.
var currentCount = 0;
var newBody = email.body_html;
var emailuid = email.uid;
var searchBody = email.body_html.replace(/\n/g, " ");
//If I comment the all the below beside the last 3 lines it works.
var regex = /<img(.*?)>/ig;
var match;
var match2;
while(match == regex.exec(searchBody)){
//Add a style float tag next to the align tag.
var alignText = match[0].replace(/align=['"]?left['"]?/gi, 'align="left" style="FLOAT: left"');
alignText = alignText.replace(/align=['"]?right['"]?/gi, 'align="right" style="FLOAT: right"');
searchBody = searchBody.replace(match[0], alignText);
var regex2 = /src=("(.)*?"|'(.)*?'|(.)*?\s+$)?/ig;
currentCount += 1;
if(currentCount >= 100)
break;
}
searchBody = searchBody.replace(/<o:p>(.*?)<\/o:p>/ig, "");
currentCount = 0;
var regex2 = /<!--\[if(.*?)<!\[endif\]-->/ig;
while(match == regex2.exec(searchBody)){
searchBody = searchBody.replace(match[0], "");
currentCount += 1;
if(currentCount >= 100)
break;
}
currentCount = 0;
var regex2 = /<!\[if !vml\]>(.*?)<!\[endif\]>/ig;
while(match == regex2.exec(searchBody)){
searchBody = searchBody.replace(match[0], match[1]);
currentCount += 1;
if(currentCount >= 100)
break;
}
current.text = searchBody;
current.insert();
event.state="stop_processing";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2015 07:16 AM
Where did your sys_id come from in the line:
current.kb_knowledge_base = 'a7e8a78bff0221009b20ffffffffff17';
I ran into the same error and was using the wrong ID. The key I used is under Knowledge Base -> Administration -> Properties
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2015 08:25 AM
I tried both that sys_id which is the "knowledge" knowledge base and I tried testing with another one as well.
the error lies must somewhere in the attachment code part since if I comment out the part below. the script works and I get knowledge documents made. But if I uncomment this part the error appears.
var regex = /<img(.*?)>/ig;
var match;
var match2;
while(match == regex.exec(searchBody)){
//Add a style float tag next to the align tag.
var alignText = match[0].replace(/align=['"]?left['"]?/gi, 'align="left" style="FLOAT: left"');
alignText = alignText.replace(/align=['"]?right['"]?/gi, 'align="right" style="FLOAT: right"');
searchBody = searchBody.replace(match[0], alignText);
var regex2 = /src=("(.)*?"|'(.)*?'|(.)*?\s+$)?/ig;
currentCount += 1;
if(currentCount >= 100)
break;
}
searchBody = searchBody.replace(/<o:p>(.*?)<\/o:p>/ig, "");
currentCount = 0;
var regex2 = /<!--\[if(.*?)<!\[endif\]-->/ig;
while(match == regex2.exec(searchBody)){
searchBody = searchBody.replace(match[0], "");
currentCount += 1;
if(currentCount >= 100)
break;
}
currentCount = 0;
var regex2 = /<!\[if !vml\]>(.*?)<!\[endif\]>/ig;
while(match == regex2.exec(searchBody)){
searchBody = searchBody.replace(match[0], match[1]);
currentCount += 1;
if(currentCount >= 100)
break;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2015 08:48 AM
I found the error.
I needed to put these:
var match;
var match2;
as
var match = []; |
var match2 = [];

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2015 09:01 AM
The reason you had to do this is because of this line
while(match == regex.exec(searchBody)){
The original is
while(match = regex.exec(searchBody)){
Only 1 equals sign.