Tuesday 6 October 2009

SAP Web Dynpro Java - Adding a new Item row to an existing model object

As part of a proof of concept (PoC) for one of my clients, I had to create a purchase order form which had multiple items against it - using an Adobe Form in Adobe Lifecycle Forms Designer. Some articles suggested that a JavaScript client side call (e.g. Table1.Row1.instanceManager.addInstance(1); would work - but in Adobe Document Services, this would never render for me - even if I forced the render to the most recent version of Adobe (9.0). It was suggested by SAP consultants that we should just be doing a full post back to the server and adding items in the server side Java code instead.

For adding items in a dynamic table like this on the server side (using the SAP Purchase Order Create Web Service (PurchaseOrderCreateRequestConfirmation_In, SAP ECC604), see below:


public void onActionAddItemRow(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
//@@begin onActionAddItemRow(ServerEvent)

// Here we get the current parent node of the model from the view
// context to which we want to add the item row

com.sap.demo.jrd.readpobyws.wd.readpowsmodel.PurchaseOrder poN = wdContext.currentPurchaseOrderElement().modelObject();

// Here we create a new, empty instance of the item row casting
// to the correct model class by using the associated model property
// of the parent node
// Note that at this point the instance is not yet bound to the item

com.sap.demo.jrd.readpobyws.wd.readpowsmodel.Item itemN = new Item((ReadPOWSModel) poN.associatedModel());

// Here we create an instance of a subnode of Item using the same
// approach
com.sap.demo.jrd.readpobyws.wd.readpowsmodel.SHORT_Description descN = new SHORT_Description((ReadPOWSModel) poN.associatedModel());

// If you wish to set default values in the leaf elements this is how
descN.set$0023SimpleContent("New item");
itemN.setID("99");

// Here we put the subnode for the Description into the parent Item
itemN.setDescription(descN);

// Here we create an instance of the subnode for the Accounting Coding
// Block Assignment using the same approach
com.sap.demo.jrd.readpobyws.wd.readpowsmodel.PurOrdERPItmByAcctgCodgBlkDistrRsp_SAcctgCodgBlkAssgmt aCBAsN = new PurOrdERPItmByAcctgCodgBlkDistrRsp_SAcctgCodgBlkAssgmt((ReadPOWSModel) poN.associatedModel());

// If you wish to set default values this is the example:
aCBAsN.setAccountDeterminationExpenseGroupCode("0000112000");

// Here we create an instance of the subnode for the Accounting Coding
// Block Distribution, i.e. the parent of the ACB Assignment
com.sap.demo.jrd.readpobyws.wd.readpowsmodel.PurOrdERPIdRsp_SAcctgCodgBlkDistr aCBDN =
new PurOrdERPIdRsp_SAcctgCodgBlkDistr((ReadPOWSModel) poN.associatedModel());

// Here we put the subnode for the ACB Assignment into the
// parent ACB Distribution
aCBDN.addAccountingCodingBlockAssignment(aCBAsN);

// Here we put the subnode ACB Distribution into the parent item
itemN.setAccountingCodingBlockDistribution(aCBDN);

// Finally – once the substructure has been built, we add the item
// to the purchase order node – i.e. set the item as a new row
poN.addItem(itemN);


//@@end
}

3 comments:

Anonymous said...

Hi Klein,
can you tell me step by step how i can create a button on Interactive Form and how i can link the source code to this button? I have a dynamc table and i would like add dynamically a row.

Thank You David.

Marco G.

Unknown said...

Hello, sir i would like to ask that what is the scope of SAP webdynpro ABAP training what all topics
should be covered and has anyone studies from this course
http://www.wiziq.com/course/8153-sap-web-dynpro-pro-abap-training of SAP webdynpro ABAP training ??
or tell me any other guidance…
would really appreciate help… and Also i would like to thank for all the information you are
providing on SAP Webdynpro ABAP training.

Unknown said...

Business Intelligence Consultant
SQIAR (http://www.sqiar.com/services/bi-strategy/) is a leading global consultancy which provides innovative business intelligence services to small and medium size (SMEs) businesses.