Forum Discussion
remco_van_oosterhout
8 years agoIdeator III
Happy to report the problem is solved. The method did already copy the file but failed to append it to the Document item, so all I had to do was create that relationship item. I also added a check at the beginning of the code so I don't accidentally overwrite any existing file. This method is now fired on the onAfterAdd and onAfterUpdate events.
//Description: If the document item is based on a template, copy the base file and append it to the Document item
Item thisItem = this;
Innovator inn = thisItem.getInnovator();
string thisItemId = thisItem.getID();
string templateId = thisItem.getProperty("from_template", "");
//First check if the Document item doesn't already have an Office file attached
string aml = "<AML><Item action='get' type='Document File'><source_id>" + thisItemId + "</source_id></Item></AML>";
Item alreadyHasAFile = inn.applyAML(aml);
//If a relationship item was found, skip the rest of the method
if (!alreadyHasAFile.isError()){
return thisItem;
}
//If item is not based on a template skip this entire method
if (templateId.Equals("")){
return thisItem;
}
//Get the relationship item pointing to the template file
aml = "<AML><Item action='get' type='Document File'><source_id>" + templateId + "</source_id></Item></AML>";
Item officeFileItem = inn.applyAML(aml);
if(officeFileItem.isError()){
return thisItem;
}
//Get the file ID
string fileId = officeFileItem.getProperty("related_id");
Item templateFile = inn.getItemById("File", fileId);
//Recycle the old filename for the new one
string fileName = templateFile.getProperty("filename");
//
Item myFile = inn.newItem("File","copyAsNew");
myFile.setAttribute("id", fileId);
myFile.setAttribute("useInputProperties", "1");
myFile.setProperty("filename", fileName);
myFile = myFile.apply();
string newFileId = myFile.getID();
aml = "<AML><Item action='add' type='Document File' ><source_id>" + thisItemId + "</source_id><related_id>" + newFileId + "</related_id></Item></AML>";
Item newRelItem = inn.applyAML(aml);
return thisItem;