Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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

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

Tags:
replied

Hi Raj,

thank you very much but i have found myself yesterday a working solution without any errors. i will post it, so maby it can help some other developers:

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;

void addAttachment(ContractIBeanIfc contract, AttachmentIfc attach) throws Exception
{  
  AttachmentSubIBeanIfc att = (AttachmentSubIBeanIfc)contract.getAttachments().create();
  att.setAttachment(attach);
  att.setOwnerRef(session.getUserAccount().getObjRef());
  att.setOwnerEmail(session.getUserAccount().getEmail());
  att.setOwnerName(session.getUserAccount().getFullName());
  contract.getAttachments().add(att);
}

void addFileAttachment(ContractIBeanIfc contract, String filePath, String displayName) throws Exception {

   FileInputStream fileStream = new FileInputStream(new File(filePath));

   AttachmentIfc newAttach = com.sap.odp.api.common.types.TypeFactory.createAttachment();

   parentBean = com.sap.odp.api.common.types.TypeFactory.createParentObjectReference(contract);

   newAttach.setFileData(displayName, fileStream, parentBean, session);

   addAttachment(contract, newAttach, null);

}


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);
    }

contract = findForEditByUniqueDocName("MA-000027-TEST-2015");
if (contract == null) {
ApplicationException ae = new ApplicationException(session, "exception.contractservice.no_contract_found");
ae.setMessageModifiers(new Object[] { name });
throw ae;
} else {
try {
// add file attachment to ma
addFileAttachment(contract, bsh.cwd + "/testMe.pdf", "new attachment");
getHome(ContractIBeanHomeIfc.sHOME_NAME).save(contract);
getHome(ContractIBeanHomeIfc.sHOME_NAME).downgradeToViewAndRelease(contract);

} catch (Exception e) {

        log.setLogMessage("error : " + e.getMessage());

        Logger.debug(log);

}


}

with regards

Waldemar

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question