Sometimes performing even basics tasks in ABAP may confuse, especially if you are a newbie in ABAP. Today I'm going to show how to add new row (line, element) to context node collection in SAP CRM using ABAP. At the end of the post you can find the final code example, implementing the discussed task. Feel free to copy, paste and run this code snippet.
Theory. In order to add new element to context node you have to:
- Define a structure line of the context node element
- Create data reference, based on the structure line, defined at the previous step
- Define the new context node object that will contain the new values, we're going to add to the context node
- Fill the new context node object with the new values
- Add new context node object, filled with the data to the context node
Practice. The final code:
- " define structure line of the context node
- TYPES:
- BEGIN OF line_struct_type,
- personID TYPE numc5,
- firstName TYPE char30,
- lastName TYPE char30,
- END OF line_struct_type.
- DATA:
- cn_line_struct TYPE REF TO DATA,
- cn_single_element TYPE REF TO cl_bsp_wd_value_node.
- " create data object, based on defined structure line
- CREATE DATA cn_line_struct TYPE line_struct_type.
- " create new data container object according to the context node structure line
- CREATE OBJECT cn_single_element
- EXPORTING
- iv_data_ref = cn_line_struct.
- " fill the values into new data container object
- cn_single_element->set_property_as_string(
- EXPORTING
- iv_attr_name = 'personID'
- iv_value = '222'
- ).
- " push the new data container object to the existed context node collection
- comp_controller_ref->typed_context->%context_node%->collection_wrapper->ADD (
- EXPORTING
- iv_entity = cn_single_element
- ).
As you can see, even such basic stuff in ABAP SAP CRM may contain some pitfalls.