sap-tutorials / Tutorials

Tutorials on sap.com
https://developers.sap.com/tutorial-navigator.html
Creative Commons Attribution 4.0 International
711 stars 772 forks source link

Enhance an ABAP Core Data Services (CDS) View in SAP NetWeaver 7.5 #5479

Closed LarsBr closed 3 years ago

LarsBr commented 4 years ago

Tutorial URL: https://developers.sap.com/tutorials/abap-dev-enhance-cds-view.html

I think these are practical code examples for how some of the (probably) most used CDS features look like when actually put into the source code. These examples seem very well produced; which is of course a good thing.

My 2cts would be:

Again, I get that this is done here for show-casing the use of a CASE statement, but it shows an unfortunate example for it.

julieplummer20 commented 4 years ago

Hi Lars, Thanks a lot for the positive feedback - makes a difference. Re: Point 2 - Valid point. I can remove it. BUT Do you have ANY ideas on a good example to show-case the CASE statement functionality - I really want to conform to best practice here. Would really appreciate your real-world input here. Will look into Point 1 as soon as feasible. (Just for reference: I adapted an existing CodeJam, but maybe BP has evolved, or I need to look at the original more closely.)

Best wishes, Julie.

LarsBr commented 4 years ago

Ok, so what would be a "good" example for using a CASE expression?

I mentioned a reason for not using it, namely that business-domain level logic needs to be consistent across the system (ideally across the organisation, but well...). Implementing such logic directly in a CASE statement would likely mean that it will have to be implemented in many places individually. That's obviously not a great situation.

For HANA SQL: Another "non"-use-case is "simple equal mappings". Something in the shape of IF x = 1 THEN ... ELSEIF x = 2 ... ELSE y is very easily doable with a CASE expression, but for this specific case with only equality comparisons, there exists a more concise expression: MAP. ABAP CDS does not have the MAP expression, so the "simple equal mappings" use case could be used for ABAP examples. However, it would require an explanation about when and why this is preferable over e.g. a lookup table.

So, finally, when would I go for a CASE expression? Whenever the logic involved RANGE conditions and does not implement a core piece of domain logic. Common examples are data "scrubbing" and output formatting where present data is normalized and formatted to whatever way the consuming processes require it.

A simple example could be this (I just checked my bank statement online):

Now, the view should not only reflect the data in the table but also include a new column that explicitly states what "kind" any given line is (DEBIT, CREDIT, INFO). That's easy to achieve with the CASE expression:

CASE  
    WHEN Amount > 0 THEN 'CREDIT'
    WHEN Amount < 0 THEN 'DEBIT'
    ELSE 'INFO'
END as 'Transaction type'
thecodester commented 4 years ago

@julieplummer20 Can you provide update?

julieplummer20 commented 4 years ago

I have discussed this with my manager. We have thought up an alternative CASE statement, which I will test myself, and then, if appropriate propose to Lars after my vacation. BR Julie.

julieplummer20 commented 4 years ago

Hi Lars, We've come up with a CASE statement that highlights the booking status on screen for the booking clerk at the travel agency: //Highlight early bookings on screen case when _Booking.BookingDate < '20210131' then 'Early Bird' else ' ' end as EarlyBird,

How do you find that? BR Julie.

LarsBr commented 4 years ago

Hi Julie, thanks a lot for coming back to this.

For something like an "early bird" indicator column, I believe the logic needs to be flexible and not hard-coded (just like the in the currency conversion example). In my experience, "early bird" promotions/statuses are rarely fixed to a single date across the whole range of products and services.

And this would not work with the current CASE statement approach. If there was a PROMOTIONS table that contains dates for when the promotion is valid and what the cut-off date for each promotion is for bookers to be considered "early bird" then the CASE statement could look like this (and make sense for the application):

case
when _Booking.BookingDate BETWEEN _Promotion.BeginDate AND _Promotion.EndDate 
          AND _Booking.BookingDate <= _Promotion.EarlyBirdEndDate 
          then 'Early Bird'
else ' '
end as EarlyBird

Actually, I'm not sure if ABAP CDS supports the BETWEEN condition. So I checked the documentation (https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm) and found that it seems to be supported. While googling I stumbled over the old documentation (https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/index.htm?file=abenddic_cds_views.htm) that comes with an actually sensible example:

@AbapCatalog.sqlViewName: 'DEMO_CDS_SCASE'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_searched_case as
  select from spfli
    { key carrid,
      key connid,
      distance,
      distid,
      case
        when distance >= 2000 then 'long-haul flight'
        when distance >= 1000 and
             distance <  2000 then 'medium-haul flight'
        when distance <  1000 then 'short-haul flight'
                              else 'error'
      end as flight_type }
    where distid = 'MI'

I would assume that those groupings are rather stable over time and across organisations. In fact, I'd guess that the travel industry as a whole would probably have a common understanding of what short-/medium-/long-haul flights are. This is quite different from the ever-changing/adapting marketing ideas of loyalty levels and promotions.

So, for this, I'd probably go for the CASE statement, even though I find the "Early Bird" example more interesting for a tutorial.

julieplummer20 commented 3 years ago

"So, for this, I'd probably go for the CASE statement, even though I find the "Early Bird" example more interesting for a tutorial."

Yes, you’re right regarding the stability of flight distance. This is a really nice example. However, in this tutorial, I have not included an association to Booking (and thence to Connection). I am looking at the idea of including an Object Page for Booking(s) per Travel. If so, I could then implement the CASE statement as you suggest. (I am also thinking about including other info, e.g. by pop-up.) I also want to include parameters but am considering how best to implement this. Right now, that has to be Priority 2, tbh.

In the meantime, I could implement the “Early Bird” approach, but make clear that the hardcoded date is for tutorial purposes only – in real life, the value would be contained in a table.

So I would suggest that I implement the Early Bird for now, but remove it, if / when I implement the Object Page. Would this be ok? BR Julie.

LarsBr commented 3 years ago

Ok, what I'm reading is that this has no importance and that the example won't be changed. My goal was to have the example code improved in a way that it shows how functions are reasonably be used in "real-life" code and your conclusion of this discussion is to still include the how-not-to-do-it-example but tell readers that this is not how to do it. I guess we all know that this does not work. But hey, it's your decision after all.

From my end, I have put in way more time and effort into this improvement proposal than it was worth. Based on this experience I don't see the point in providing free, constructive feedback to SAP.

julieplummer20 commented 3 years ago

" this has no importance and that the example won't be changed." No. That's not what I meant at all. For clarity, my conclusion is: The distance example is much better. However, it fits (imo) better in an Object Page. This is not an insuperable hurdle, but, it will take longer to describe than simply swapping one CASE statement for another. I need to complete a different mission. Once that's done, I will do this. In the meantime, I will remove the bad CASE statement. I appreciate that you put a lot of work into this. I just don't want to pretend that I am going to drop everything right now, when I can't. That was my meaning with Prio 2, not "I'm not doing it." If I have no capacity to do something, I’ll say honestly “I can’t do it.” Anyway, I am sorry that you are dissatisfied with the conclusion, but I have benefited a lot from your feedback. I did not think, for example, of pulling the value for the promotion date from a table, and my manager did not mention it. This is where real-world experience is invaluable. Therefore, I would still like to thank you personally for your commitment. I will let you know when I have added the new CASE statement, via this thread. BR Julie.

greg2git commented 3 years ago

I'm here to find out what will/has happened to this:

CASE
WHEN Amount > 0 THEN 'CREDIT' WHEN Amount < 0 THEN 'DEBIT' ELSE 'INFO' END as 'Transaction type'

and I have no further comments.

Thank you, gm

julieplummer20 commented 3 years ago

Hi gm, "CASE WHEN Amount > 0 THEN 'CREDIT' WHEN Amount < 0 THEN 'DEBIT' ELSE 'INFO' END as 'Transaction type'"

... was an example of output formatting. It was never intended to be used in the current tutorial, which is based on a travel agency. I will be implementing a new CASE statement in the tutorial asap. BR Julie.

MichaelCzcz commented 3 years ago

Thank you for your feedback. The issue seems to be resolved, so I am closing the issue.

If you still have questions, feel free to reopen the issue.

julieplummer20 commented 3 years ago

Hi Lars, hi everyone, I apologise for the delay. I have now added a CASE statement to the tutorial in the following tutorial: https://developers.sap.com/tutorials/abap-environment-enhance-cds-view.html - ie for SAP Cloud Platform, ABAP Environment. As soon as I can test this on an on-Premise system, I will add it to this tutorial as well. At present, I cannot access the system. However, there should be no difference, provided you use an S/4HANA 1909 system. Best wishes, Julie.

LarsBr commented 3 years ago

Hi Julie, thanks for your continued work on this bit. I appreciate your work and the constraints and priorities that apply to it. I certainly did not mean to imply that you personally didn't take this "serious enough" or didn't work with your best intentions and efforts on it. On the contrary! The ongoing and growing need for good tutorials and documentation that show how to best use the technology makes the work of authors like yourself even more important; which is why I am passionate about good knowledge-sharing, too.

The long and short of it is: thanks again for your work. Cheers, Lars

julieplummer20 commented 3 years ago

Hi Lars, No problem. Your commitment to sharing the knowledge inside the SAP Community is well-known and exemplary. I am just about to watch your SOT on “If your SQL looks like Assembler…”. However, I am well aware that I have a lot to learn about ABAP, especially in the real world, so I hope, the next time your find something that could be better, you won’t hesitate to let me know.

Thanks for all this – it really helps us all. Best wishes, Julie.