Skip to Content
0
Former Member
Jun 29, 2015 at 12:47 PM

How to add an attachment to contract / master agreement with beanshell?

110 Views

Hallo,

i have to read some files from a folder and then add them as new atachment to a master agreement. what i do is:

import com.sap.eso.api.contracts.ContractIBeanHomeIfc;
import com.sap.eso.api.contracts.ContractIBeanIfc;
import com.sap.odp.api.common.types.AttachmentIfc;
import com.sap.odp.api.comp.attachments.AttachmentSubIBeanIfc;
import com.sap.odp.api.ibean.OrderedSubordinateCollectionIfc;
import com.sap.odp.api.ibean.SubordinateCollectionIfc; IBeanHomeIfc getHome(String homeName) throws ApplicationException
{
IBeanHomeIfc home = IBeanHomeLocator.lookup(session, homeName);
return home;
}
ContractIBeanIfc findForEditByUniqueDocName(String uniqueName) throws ApplicationException, DatabaseException
{
ContractIBeanHomeIfc home = (ContractIBeanHomeIfc)getHome(ContractIBeanHomeIfc.sHOME_NAME);
return (ContractIBeanIfc) home.findForEditByUniqueDocName(uniqueName);
}
final String docName = "MA-000027-DBAG-2015";
contract = findForEditByUniqueDocName(docName);
if (contract == null) {
ApplicationException ae = new ApplicationException(session, "exception.contractservice.no_contract_found");
ae.setMessageModifiers(new Object[] { name });
throw ae;
} else {
OrderedSubordinateCollectionIfc maAttachments = contract.getAttachments();
// get file stream
FileInputStream fileStream = new FileInputStream(new File(bsh.cwd + "/testMe.pdf"));
// create a new attachment
AttachmentIfc newAttach = com.sap.odp.api.common.types.TypeFactory.createAttachment();
parentBean = com.sap.odp.api.common.types.TypeFactory.createParentObjectReference(contract);
newAttach.setFileData(searchFileName, fileStream, parentBean, session);
// create a new external representation of a Document Attachment
AttachmentSubIBeanIfc newAttachSub = maAttachments.create();
newAttachSub.setAttachment(newAttach);
maAttachments.add(newAttachSub);
// downgrade contract
getHome(ContractIBeanHomeIfc.sHOME_NAME).downgradeToViewAndRelease(contract);
}

i get no errors, but finally the attachment will not be saved.

Can you hlep me and explain me what i'm doing wrong?

with best regards

Waldemar