Skip to Content
0

SAP Business One DI API (Java) - How to add a Blanket Agreement

Mar 17, 2017 at 02:55 PM

239

avatar image
Former Member

Hello, this is my first post here.

I need to create and add a Blanket Agreement using the Java DI API.

I am able to update an existing Blanket Agreement using the following code:

com.sap.smb.sbo.api.BlanketAgreementsService BAService = (com.sap.smb.sbo.api.BlanketAgreementsService) com.sap.smb.sbo.api.SBOCOMUtil.newBlanketAgreementsService(company.getCompanyService());
BlanketAgreementsParams BAParams = (BlanketAgreementsParams) BAService.getBlanketAgreementList();
BlanketAgreementParams  BAParam =  (BlanketAgreementParams) BAParams.add();


// Suppose I want to update the Blanket Agreement number 350
BAParam.setAgreementNo(350); 


IBlanketAgreement BA;
BA = (IBlanketAgreement) BAService.getBlanketAgreement(BAParam);


// Exemple
BA.setBPCode(myBP.getBPCode());
BA.setDescription(myBP.getDescription());
//...


 BAService.updateBlanketAgreement(BA);

But what I really want to do now is to add a new Blanket Agreement.

I can't find any Java example for doing this and I am having no success to declare a new Blanket Agreement object.

I tried many things, like the following code for example:

com.sap.smb.sbo.api.BlanketAgreementsService BAService = (com.sap.smb.sbo.api.BlanketAgreementsService) com.sap.smb.sbo.api.SBOCOMUtil.newBlanketAgreementsService(company.getCompanyService());
BlanketAgreementsParams BAParams = (BlanketAgreementsParams) BAService.getBlanketAgreementList();
BlanketAgreementParams  BAParam =  (BlanketAgreementParams) BAParams.add();




IBlanketAgreement BA;
BA = (IBlanketAgreement) myBPSInicio.getDataInterface(SBOCOMConstants.BlanketAgreementsServiceDataInterfaces_basBlanketAgreement);
// I also tried BA = (com.sap.smb.sbo.api.BlanketAgreement) myBPSInicio.getDataInterface(SBOCOMConstants.BlanketAgreementsServiceDataInterfaces_basBlanketAgreement);

With this code I get an exception:

java.lang.ClassCastException: com.sap.smb.sbo.wrapper.com.Dispatch cannot be cast to com.sap.smb.sbo.api.BlanketAgreement

Can someone teach me how to declare and add a new Blanket Agreement Object using the DI API and Java?

Thanks!

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

avatar image
Former Member Sep 15, 2017 at 08:54 AM
0

Hello!

I had the same problem for months, except with the ProfitCenters - you should first initialize the myBPSInicio.getDataInterface to an Object, and also when you initialize BA variable, you should leave the "I" before the BlanketAgreement like this:

IBlanketAgreement BA;
Object obj = myBPSInicio.getDataInterface(SBOCOMConstants.BlanketAgreementsServiceDataInterfaces_basBlanketAgreement);
BA = new BlanketAgreement(obj);

I didn't try your code, but this is my working Java code for adding/updating profitcenters with the same SAP Service thing like your BlanketAgreementService:

protected int getProfitCenter(String pProfitCenter){

        try {
            com.sap.smb.sbo.api.ProfitCentersService pcservice = (com.sap.smb.sbo.api.ProfitCentersService) com.sap.smb.sbo.api.SBOCOMUtil.newProfitCentersService(company.getCompanyService());
            com.sap.smb.sbo.api.ProfitCentersParams PcParams = (com.sap.smb.sbo.api.ProfitCentersParams) pcservice.getProfitCenterList();
            ProfitCenterParams pcparam = (ProfitCenterParams) PcParams.add();
            pcparam.setCenterCode(pProfitCenter);

            IProfitCenter pc = null;
            boolean exists = true;
            try{
                pc = (IProfitCenter) pcservice.getProfitCenter(pcparam);
                if (pc.getCenterCode() == null ? pc.getCenterName() != null : !pc.getCenterCode().equals(pc.getCenterName())){
                    pc.setCenterName(pProfitCenter);
                    pc.setActive(1);
                    pc.setEffectivefrom(new Date());
                    pcservice.updateProfitCenter(pc);
                }
            } catch (Exception ex){
                serr("ProfitCenter not exists? " + ex.getMessage());
                exists = false;
            }

            if (!exists){
                Object obj = pcservice.getDataInterface(SBOCOMConstants.ProfitCentersServiceDataInterfaces_pcsProfitCenter);
                pc = new com.sap.smb.sbo.api.ProfitCenter(obj);
                pc.setCenterCode(pProfitCenter);
                pc.setCenterName(pProfitCenter);
                pc.setInWhichDimension(4);
                pc.setEffectivefrom(new Date());

                pcservice.addProfitCenter(pc);
            }

            pc.release();
            pcparam.release();
            PcParams.release();
            pcservice.release();
        } catch (SBOCOMException ex) {
            Logger.getLogger(PcExport.class.getName()).log(Level.SEVERE, null, ex);
        }
}
Share
10 |10000 characters needed characters left characters exceeded