cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

RAP: Creating a child entity with 'create by association' for an existing root entity fails

kheba
Discoverer
0 Kudos
12,619

Hello dear SAP community!

I would like to create an action in my RAP model that creates a child entity with the keys plant, material number and workcenter for an existing root with the keys plant and material number. The work centre is specified by the user in a dialogue window. The action is located on the object page of the root node.

If I click on the action and enter a work centre, I do not encounter an error but end up with an empty object page. A table entry with the new child is also created, but as soon as I click on the entry in the table in the app, the message 'The requested data was not found' is displayed.

Below are the corresponding screenshots of the code. Thanks in advance 🥳


Accepted Solutions (0)

Answers (3)

Answers (3)

patrick_winkler
Product and Topic Expert
Product and Topic Expert

@DanielSchneider 

To add an action to the object page header of entity A to create a child entity B, add the following to the behavior definition of entity A.

 

  factory action CreateChild parameter ZD_CreateChildP [1..*]  { default function GetDefaultsForCreateChild ; }
  side effects { action CreateChild affects $self; }

 

To add the action to the object page use the following annotation for entity A:

 

  @UI.identification: [ {
    position: 1 
  },{
    type: #FOR_ACTION, 
    dataAction: 'CreateChild', 
    label: 'Create Child'
  } ]

 

Example action implementation:

 

  METHOD CreateChild.
    MODIFY ENTITY IN LOCAL MODE ZI_Cubco4
      CREATE BY \_Cubco4Child
      AUTO FILL CID
      FIELDS ( ParentId ChildId ) WITH VALUE #( FOR key IN keys (
        %is_draft = if_abap_behv=>mk-on
        id = key-Id
        %target = VALUE #(
         ( %is_draft = if_abap_behv=>mk-on
           ParentId = key-Id
           ChildId = key-%param-Id )
          ) ) )
      REPORTED reported FAILED failed MAPPED mapped.
  ENDMETHOD.

 

Defaulting: https://community.sap.com/t5/technology-blogs-by-members/abap-rap-defaulting-action-parameters-with-...

DanielSchneider
Participant
0 Kudos

Hi Patrick,

wow, thanks for the super fast response and for the provided snippets!!

It works like a charm. Those were the issues why it didn't work for me:

  • The EML create statement was missing the "auto fill cid" part
  • I had [1] instead of [1..*] in the action definition
    • When creating an associated entity the cardinality of [1] does work (the entity gets created), but after creation the generated app tries to navigate to the newly created root instance (which does not exist, since we did not create any)
    • Here a screenshot of the ABAP doc, maybe the part with "factory action always need to have a cardinality of [1]" could get adjusted

DanielSchneider_0-1726648706172.png

 

Thanks again for your awesome support and have a nice day!
Daniel

patrick_winkler
Product and Topic Expert
Product and Topic Expert
0 Kudos
@DanielSchneider you should be able to use cardinality [1] when you enable inline creation mode for the child table https://sapui5.hana.ondemand.com/sdk/#/topic/cfb04f0c58e7409992feb4c91aa9410b
bkeys
Newcomer
0 Kudos
I'd really like the button to be on top of the child entity list (inside of the parent entity page). Is there a way to do this?
SamAriekela
Explorer
0 Kudos

Hi Patrick,  Very similar to your GIF but in my case , I need to create multiple child entities. I ended up using your initial suggestion of  static factory action in main page. It worked well . But I am getting below error. I am using Odata V4 and this error seems to be intermittent.  I see my header and child entities in mapped table after create by association fine. I am being navigated to the object page but only see key values of the header and nothing else.

SamAriekela_1-1713049436961.png

 

 I followed this while creating the child entities:

 

https://help.sap.com/docs/abap-cloud/abap-rap/modify#loio9000a42dcd604a7799527cbb00bc4a69__create_ex...

 

Can you tell what the issue might be. Thanks again for your time!

patrick_winkler
Product and Topic Expert
Product and Topic Expert
0 Kudos
Remote diagnosis is difficult. I haven't seen that error yet. Double check whether you use %tky and %is_draft correctly in all places. If you have character-like key fields, try to create a root entity and then create child entities with uppercase keys to ensure that the issue is not case-sensitivity.
SamAriekela
Explorer
0 Kudos
Hi Patrick, After spending a lot of time debugging, this morning I found out the issue was indeed due lower case key value . I hope this is documented somewhere as I couldn't find anywhere. Really appreciate your inputs. Thanks!
patrick_winkler
Product and Topic Expert
Product and Topic Expert
0 Kudos

If you define a static factory action for the child entity, you do not get the parent keys.
When you define a factory action for the child entity, you must select an existing entity as a reference.

When you define a factory action for the parent entity, you have the parent keys, but in a standard Fiori elements app, the action button is in the header area instead of next to the child table. If the table is not refreshed after executing the action try defining a side effect in the behavior definition.

Another approach could be to enable inline creation mode for the child table. The user can quickly create new entries where the parent keys are prefilled. It's faster than clicking on an action button.

Factory action on root level to create child entity:

factory_action.gif

SamAriekela
Explorer
0 Kudos
Yep, worked with inline creation mode while awaiting your answer and it worked out well. I agree this is the best approach. Appreciate taking time to answer my questions, Patrick!
SamAriekela
Explorer
0 Kudos
Hi Patrick, Although Inline creation mode worked well, client seems to be not happy with it. So still exploring. I am trying to follow this approach but facing the same issue as here. https://community.sap.com/t5/technology-q-a/can-we-fill-child-entity-in-rap-app-before-create-the-ne... Child entity is getting created when I click on save but the child entry is not showing up on the UI even with side effects initially when you come to the object page. Any thoughts? Thanks in advance!
patrick_winkler
Product and Topic Expert
Product and Topic Expert
0 Kudos
@SamAriekela: I've added a GIF to my answert; is that what you are looking for?
DanielSchneider
Participant
0 Kudos

Hi @patrick_winkler ,

may I ask you how you achieved the result you are showing in the GIF?

Is it a factory action or a non-factory action? And on which level did you define it?
If you still have it, it would be great if you could show parts of the behavior definition.

My goal is to have an action on an object page (on the header, not the childs table), which results into a new child entity. In the popup for the parameter input I want to set default values.

Any help is very appreciated!

Thank you

Daniel