cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

former_member207877
Active Participant
0 Kudos

Hello Waldemar,

When we try to set any attachment in the Master Agreement Attachments section, it doesn't thow any error and at the sametime it doesn't add it as well.

But when you click on Attchments section you can see  attachment getting added but with few errors.

Basically while setting an attahcment you have to set the ownerRef, ownerEmail, ownerName for that specfic attachment, then it will be succesfully added without any errors.

FYI...... fine below script.

ownerRef = doc.getDocumentOwnerUserReference();

ownerEmail = IapiAccountLocator.lookup(session,doc.getDocumentOwnerUserReference()).getEmail();

ownerName = IapiAccountLocator.lookup(session,doc.getDocumentOwnerUserReference()).getFullName();

maAttach = doc.getExtensionField("Z_ATTACH").get();

attachmentsList= doc.getAttachments();

attachmentsListMem = attachmentsList.create();

attachmentsListMem.setAttachment(maAttach);

attachmentsList.add(attachmentsListMem);

attachmentsListMem.setOwnerRef(ownerRef);

attachmentsListMem.setOwnerName(ownerName);

attachmentsListMem.setOwnerEmail(ownerEmail);

Regards,

Raj

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Waldemar,

Thank you for posting this!

Sorry for the beginner-level question... but exactly where does this add the file to?

On the Master Agreement UI form, is it the "Attachments" tool bar button or on the "Contract Documents" tab

Thanks again,

Mike

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Michael,

the attachment will be readed as file from a local hard disk and attached to a corresponding master agreement. after the scipt was executed you can see the attachments at  the "attachment-tab" in the master agreement.

background:

we have to read daily a folder with newly signed and scanned attachments and attach them to the corresponding ma's. 

Waldemar