Want to get an Existing Vaulted File and Save it with a New Document

You want to get an existing File from the Vault and attach it to a new Document

Technique

This is similar to the last recipe, but uses the getItemByKeyedName() method to get an existing File Item and copyAsNew() to create it as a new File Item.

JavaScript

// Create the Document item
var docItem = this.newItem(“Document”,"add”);
docItem.setProperty(“item_number”,"456");
// Get the File item
var innovator = this.getInnovator();
var fileItem = innovator.getItemByKeyedName(“File”,"My Document.doc”);
if (fileItem.isError()) {
top.aras.AlertError(fileItem.getErrorDetail());
return;
}
// Duplicate File Item as files should be 1 to 1
var newFile = fileItem.apply(“copyAsNew”);
// Create the relationship between the Document and File
var relItem = this.newItem(“Document File”,"add”);
docItem.addRelationship(relItem);
relItem.setRelatedItem(newFile);
var results = docItem.apply();
if (results.isError()) {
top.aras.AlertError(results.getErrorDetail());
} else {
// Show the new Document
top.aras.uiShowItemEx(results.getItemByIndex(0).node, ‘tab view’, true);
}