Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Order of execution of multiple BADIs within the same Enhancement Implementation

bryan_koetting2
Active Participant
0 Kudos
6,477

Hello ABAP'ers,

I have an Enhancement Spot BAdi (FAGL_LIB) that I want to call twice. (It's a kernel-type, new. It allows multi-use, but it has no filters) The first call will use an SAP-provided Class, which I don't want to modify. The second call will use my own Class with my custom logic. I need them to execute in that order. I am confused on the difference between creating multiple Enhancement Implementations, versus creating multiple BAdi Implementations within the same Enhancement Implementation.

Functionally both of these setups seem to work the same, in that both of my Classes are called up at the Spot, one after the other. I have seen numerous postings that explain that the execution order of multiple Enhancement Implementations for an Enhancement Spot cannot be controlled (without another special BAdi). However, in my testing with multiple BAdi Implementations within the same Enhancement Implementation, I have seen that it appears to always execute the BAdi Implementations in the order which they are shown in the Enhancement Implementations screen, which happens to be according to BAdi Implementation technical name. Therefore I feel like I can guarantee the order of execution by creating one Enhancement Implementation containing both BAdi Implementations and naming appropriately.

Can anyone confirm or deny this behavior?

Thanks,

Bryan

1 ACCEPTED SOLUTION

matt
Active Contributor
3,632

"The reason that I could understand behind this behavior is the implementations are stored in a table SXC_ATTR with Implementation Name (IMP_NAME) as the table key which when retrieved during implementation call are obviously sorted as per their names in alphabetical order."

Maybe I've misunderstood what you're saying, but you seem to say that the implementations are done in alphabetical order, as that is the order in which they're stored in the table. If that is what you are saying, then I'm afraid you are dangerously wrong.

In relational databases - Oracle, HANA etc. -, there is no concept of ordering on a table. The sort order is undefined. You absolutely cannot rely on it. To do so is to introduce a possibly very hard to find bug.

This misunderstanding crops up frequently, even among experienced programmers. See my comments on this blog: https://blogs.sap.com/2018/10/02/semi-automatic-custom-code-adaptation-after-sap-s4hana-system-conve... Records tend to remain in the table in the order they're entered - hence the idea that there is an order. But if some are removed, and others inserted and then there's database reorg, you'll find that the order is no longer as expected.

So, for the OP.

"Therefore I feel like I can guarantee the order of execution by creating one Enhancement Implementation containing both BAdi Implementations and naming appropriately."

No. You can't and you must not.

10 REPLIES 10

former_member208149
Participant
3,632

Hi Bryan,

Although SAP's standard documentation about multiple implementations suggests that we cannot control the order of implementation call, but I have also observed in couple of my experiences with multiple BAdI implementations that the implementations always seem to execute in the order in which they are shown in the Enhancement Implementations screen, which happens to be according to BAdi Implementation technical names. The reason that I could understand behind this behavior is the implementations are stored in a table SXC_ATTR with Implementation Name (IMP_NAME) as the table key which when retrieved during implementation call are obviously sorted as per their names in alphabetical order.

Not sure why it is a common understanding that order of implementation cannot be controlled.

Regards.


Sapeksh

0 Kudos
3,632

Great info Sapeksh.

Thanks,

Bryan

0 Kudos
3,632

See my answer below. This answer is not correct.

Florian
SAP Champion
SAP Champion
3,632

I also agree with sapeksh,

and I think the "not controlled" part are there, because noone can guarantee that there is another product / upgrade / developer, which do not know your naming and the wanted behaivor. So if you can say I'm the one and only I would agree with your findings.

0 Kudos
3,632

Florian,

Agreed, thanks for your feedback.

Bryan

matt
Active Contributor
3,633

"The reason that I could understand behind this behavior is the implementations are stored in a table SXC_ATTR with Implementation Name (IMP_NAME) as the table key which when retrieved during implementation call are obviously sorted as per their names in alphabetical order."

Maybe I've misunderstood what you're saying, but you seem to say that the implementations are done in alphabetical order, as that is the order in which they're stored in the table. If that is what you are saying, then I'm afraid you are dangerously wrong.

In relational databases - Oracle, HANA etc. -, there is no concept of ordering on a table. The sort order is undefined. You absolutely cannot rely on it. To do so is to introduce a possibly very hard to find bug.

This misunderstanding crops up frequently, even among experienced programmers. See my comments on this blog: https://blogs.sap.com/2018/10/02/semi-automatic-custom-code-adaptation-after-sap-s4hana-system-conve... Records tend to remain in the table in the order they're entered - hence the idea that there is an order. But if some are removed, and others inserted and then there's database reorg, you'll find that the order is no longer as expected.

So, for the OP.

"Therefore I feel like I can guarantee the order of execution by creating one Enhancement Implementation containing both BAdi Implementations and naming appropriately."

No. You can't and you must not.

3,632

Ok, message received, thanks Matthew. I was not very keen on relying on the "natural" order of entries in a table to guarantee the execution order.

Bryan

former_member557751
Participant
3,632

Hello,

you can control the execution of the BAdI's with BAdI BADI_SORTER. Here is a blog about it.

https://blogs.sap.com/2016/10/02/badisorter-with-kernel-badi-and-other-sorting-options/

Best regards, Tapio


0 Kudos
3,632

Hi Tapio,

Yes I am aware of that solution, but it seemed overly complex to implement an entire separate BADI just for that purpose, which is why I posted this to the group.

Regards,

Bryan

0 Kudos
3,632

Hi Brian,

if you have access to the BAdI reference there is an attribute "badi_reference->impls" where all implementations are available for the BAdi. So, you can create your own sorting. Anyway, BADI_SORTER is the tool which is made exactly for this.

Best regards, Tapio