I want to write a code to read local path and insert all the files in the folder into Attachments table of servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2018 08:06 AM
Hi ,
I have a requirement to insert thousands of files into ServiceNow sys_attachment table. All the files are in local folder. It is one time activity which is part of Data migration. How can I achieve this. Is there any file related API's that ServiceNow supports?
Thanks
Ravikiran
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2018 08:59 AM
Hello,
I had the same problem. What I did is to write a piece of Java code and use ServiceNow Attachment API.
Here is the code:
package importattach;
import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
/**
*
* @author pdragos
*/
public class ImportAttach {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
File[] filelist = GetFiles();
// for (File filelist1 : filelist) {
// System.out.println(filelist1.getAbsoluteFile());
// }
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("YourUser", "YourPaswd");
provider.setCredentials(AuthScope.ANY, credentials);
CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();
HttpPost post = new HttpPost("https://SNOWINSTANCE/api/now/attachment/upload");
for (File filelist1 : filelist) {
String textFileName = filelist1.getAbsoluteFile().toString();
File file = new File(textFileName);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
StringBody stringBody1 = new StringBody("kb_knowledge", ContentType.MULTIPART_FORM_DATA); //You should link the attachement to a table
StringBody stringBody2 = new StringBody("5a89a7eedbfcd7803b08fd651d9619b8", ContentType.MULTIPART_FORM_DATA); // You should link the attachement to an record in that table.
builder.addPart("table_name", stringBody1);
builder.addPart("table_sys_id", stringBody2);
builder.addPart("f", fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(post, responseHandler);
System.out.println(responseBody);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static File[] GetFiles() {
File folder = new File("\\YourFolder\\Subfolder");
File[] listOfFiles = folder.listFiles();
for (File listOfFile : listOfFiles) {
System.out.println(listOfFile.getName());
System.out.println(listOfFile.getAbsoluteFile());
}
return listOfFiles;
}
}
Hope this will help you.
Catalin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2021 06:17 AM
I have a similar need, to insert a large number of files into sys_attachment, and link each sys_attachment record to a knowledge article record.
My question:
Can I use GlideSysAttachment.write to write the attachment record? Also, in ServiceNow, what open can I use to Open local file and get its content, so that I can insert the file content to GlideSysAttachment.write?
Thanks for the help.
Marco...