Import of Word Documents to ServiceNow Knowledge base

welterp
Giga Contributor

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?

1 ACCEPTED SOLUTION

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);


}


View solution in original post

76 REPLIES 76

I am using Fuji--Yes, it is set for New, and where would I look for the log? I went to System Policy>Events>Event Logs and it said email.read and then gave me some sys_parms


At the bottom of the email that was processed there is a related list I believe.   You may have to modify the form to see it.   Also look in the syslog to see if there are any warnings or errors around the time the email came in.


Ok. I also tried going into System Logs > Logs > ALL and it gave me a huge list of what looked like urls because I saw mozilla a lot as well as email action and error.


java.lang.NullPointerException: null value in entry: kb_knowledge_base=null


Caused by error in <refname> at line 1


com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:33)


com.google.common.collect.ImmutableMap.entryOf(ImmutableMap.java:135)


com.google.common.collect.ImmutableMap.of(ImmutableMap.java:89)


com.snc.knowledge3.core.UserCriteria.isInKBContributeBlacklist(UserCriteria.java:100)


com.snc.knowledge3.core.UserCriteria.canContributeToKB(UserCriteria.java:185)


com.snc.knowledge3.core.UserCriteria.canContribute(UserCriteria.java:155)


com.snc.knowledge3.js.KnowledgeHelper.jsFunction_canContribute(KnowledgeHelper.java:310)


sun.reflect.GeneratedMethodAccessor1151.invoke(Unknown Source)


sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


java.lang.reflect.Method.invoke(Method.java:597)


org.mozilla.javascript.FunctionObject.doInvoke(FunctionObject.java:597)


org.mozilla.javascript.FunctionObject.callVarargs(FunctionObject.java:613)


org.mozilla.javascript.FunctionObject.call(FunctionObject.java:457)


org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1227)


Well there you go, the kb_knowledge_base field is null and it is a required field in Fuji.   You will have to update the code to set the field to a KB that all of these should go into.


I see. Also, it won't let me even open the email from the inbox. I tried and then I get the spinning wheel of death and a timer that shows how long it is taking to respond...Thanks.