To attach a file to a record you can use the following code snippet.
The class to use is the following: DocumentManagement::attachFileToCommon()
var mime = System.Web.MimeMapping::GetMimeMapping(filename);
DocuRef docrefOut = DocumentManagement::attachFileToCommon(
commonRec,
DocuType::typeFile(),
memStreamIOArExpo,
filename,
mime,
filename);
An example of use can be seen in the splitEROutputByItems method of the EMActionERExport class.