Sunday, April 5, 2009

Generate Simple Transformation for XML in ABAP - Part II

Just wanted to quickly add some more information to my last post on the same topic "Discovering a Hidden Gem :Generate Simple Transformation for XML in ABAP"

In this example, I will show how to use  "Edit Simple Transformation Graphically" feature of XSLT_TOOL to generate simple transformations for XML elements having attributes.

Let's say there is a requirement to generate XML document from the ABAP data/ internal tables. And the resultant XML document should have the following structure. Basically, POST_CODE should be an attribute of "Town" element. 
<?xml version="1.0" encoding="utf-8" ?>

<NewDataset>
   <Table>
      <Town POST_CODE="B5">BIRMINGHAM</Town>
      <County>WARWICK</County>
  </Table>
<Table>
      <Town POST_CODE="NP20">NEWPORT</Town>
      <County>WALES</County>
</Table>
</NewDataset>
Initial steps are similar to the example in my last post [ so I've just copied the screen shots from there ]
  1. Create Table Type ZNEWDATASET in transaction SE11. The hierarchy structure should be compatible with the XML hierarchy.

  2. Now create the ST program in transaction XSLT_TOOL. Spot the magic stick in the editor menu.

  3.  Click on 'Edit Simple Transformation Graphically' button (magic stick) and you will get the editor as below. Create a new root (right click - context menu ) in "Data Roots" section. I've named it NEWDATASET but the important thing is to enter the correct type, created earlier.

  4. Now you can see the data root hierarchy as below. Drag and Drop the NEWDATASET root on right panel ( Simple Transfromation ). It automaically generates the ST nodes corresponding to the ABAP structure. However, you need to adjust the names as per the XML element names.
  5. Now adjust the names in ST panel as per actual XML element names. Delete the PostCode element as we need this as an attribute of Town, rather than an element.

  6. Important thing to remember: Always use 'SAVE' before adding , deleting or modifying nodes. Otherwise you will face errors during node operations.
  7. Select node "Town", and then click on "First Child" button at the top toolbar. Further, use right click to get the conext menu for "Town" element and create a new attribute 'POST_CODE". Now drag the POST_CODE from left panel (data) to right panel on the POST_CODE attribute to create association. The index numbers shows the association between data nodes and xml nodes.
Save and Activate. The ST program (Z_RAM_TEST_ATTR1), generated by the utility, is as below:
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
    <tt:root name="NEWDATASET" type="ddic:ZNEWDATASET"/>
        <tt:template>
              <NewDataSet>
                    <tt:loop ref=".NEWDATASET">
                          <Table>
                                     <Town>
                                                      <tt:attribute name="POST_CODE" value-ref="$REF.POST_CODE"/>
                                                      <tt:value ref="TOWN"/>
                                               </Town>
                                     <County tt:value-ref="COUNTY"/>
                          </Table>
                    </tt:loop>
               </NewDataSet>
         </tt:template>
</tt:transform>
Example ABAP code to call the transformation is as below:
REPORT  z_ram_abap_to_xml.

DATA : lt_source TYPE znewdataset,
       wa_source LIKE LINE OF lt_source,
       xml_result TYPE xstring.  "xstring ensures UTF-8 encoding

wa_source-town = 'BIRMINGHAM'.
wa_source-county = 'WARWICK'.
wa_source-post_code = 'B5'.
APPEND wa_source TO lt_source.

wa_source-town = 'NEWPORT'.
wa_source-county = 'WALES'.
wa_source-post_code = 'NP20'.
APPEND wa_source TO lt_source.

CALL TRANSFORMATION z_ram_test_attr1
  SOURCE newdataset = lt_source[]
  RESULT XML  xml_result  .

CALL FUNCTION 'SCOL_TRACE_SHOW_XML'
  EXPORTING
    xdoc = xml_result.

In case you want  help on how to download the XML documents etc, please check the standard example transaction SSTDEMO1

Saturday, February 28, 2009

To SAP with Love: Focus on University Alliance than SAP Certifications

Please SAP, Focus on improving the University & Corporate Alliance Program. You will get your chance to certify them later. Think about Future.

Like any other checks, SAP Certifications can be useful but it is not a necessary or sufficient measure to judge the ability of an SAP Consultant. Anyone serious about solving the issue should focus on the root cause of the problem. My advice to someone who thinks SAP Certification, by itself, is the magic solution to the problem of failed SAP projects. With due respect, now please get back to earth. And welcome home, just in case.

A lot of digital ink ( and sound waves as well ) have been well spent on SAP Certification discussion and if you are feeling left behind then check this one by Dennis Howlett to start with.

In general, I've summarized the way different sectors of SAP ecohub value SAP Certifications:

1. SAP Aspirants: SAP Certifications is their only quick and possible hope of getting into a relatively lucrative career. Unfortunately, it turns out to be a false hope, in most of the cases.

2. SAP Buyers / Deal Makers / Management / Sales : In absence of any other quick measure to filter good consultants from bad ones they fall back on SAP Certifications as a measure of talent, knowledge & skill.
Consultancies and Sales guys are aware of the SAP Buyers' infatuations towards 'Number of certified SAP consultants' so they use it for their advantage, whenever possible.

3. Experienced and competent consultants: Most of them don't give a damn about SAP Certifications. In reality, if they interview a candidate, who is emphasizing on his SAP certifications more than his previous work and experience, then it's a negative sign.

Why do I think that SAP Certification is not an effective solution compared to University / Corporate Alliance? [ Actually, it's not even as good as filtering through normal interview selection ]

Because knowledge of syntaxes, semantics, few basic configuration scenarios, master / transaction data, is not enough , unless you have the TALENT, SKILL & EXPERIENCE to apply that knowledge in resolving real Business/IT problems. Surely I must have left a few other important attributes for success but then  I am not an expert.

I believe good universities/colleges have a better framework to attract talent and judge them through a long term process of examination compared to SAP Certifications. No offence meant to anyone who has just graduated and went for SAP Certifications but I've seen a disturbing trend in some parts of the world.

SAP Certifications are perceived as costlier but relatively easier way of getting into IT. Most of the fresh graduates, who could not qualify to the generic aptitude tests of IT companies and other qualification criteria, resort to SAP certifications. Much to their disappointment later. In a sense SAP Certification is not capable of attracting or filtering right talent.

In its current form, SAP Certifications are merely a check of your knowledge gained through a few days of SAP Training. It does not add much value to the skills or experience. It's not even an aptitude test. University and corporate alliance programs can surely help in enhancing your skill and provide some basic experience through corporate internships.

Further, it's important to have right academic / industry qualifications depending on the specific area of SAP. Also, experience as a business user in the related field should be considered valuable for a career in SAP Functional area.

There are always some exceptions to the rule but who cares about exceptions.

WHEN OTHERS " Probably a genius but deprived because of this university framework
*   Do Nothing , they will find their way.

Enough on certifcations. I will be back with a post on the XML ABAP transformation tool soon.


Disclaimer: I am also one of the non-certified SAP consultants. Luckily ( or whatever ),  I got a chance to work with SAP Projects from the begining of my career. Basically, I am still learning it for the past 11 years.

Wednesday, February 11, 2009

The 8th Habit of a Programmer !

Oh don't go on the title of this post. I am just trying to read this book called 'The 8th Habit' but my daughter won't approve. It seems only one person, who is allowed to read a book in my house, is my 2 years old daughter. Never mind.

There was an interesting post by Jeff Atwood, Don't Reinvent The Wheel, Unless You Plan on Learning More About Wheels. I won't go that far to expect programmers will rewrite the libraries and use it in live projects. But sometime a programmer should think about how the wheel was invented, at first place.

Interestingly, one of my favourite questions, which I ask during technical interviews and mainly to the junior developers:
Assume a particular programming language statement/Function does not exist as yet. Now explain how would you go about building a logic to replace the original statement / Function.

e.g. What if SAP has not provided a SORT statement for the internal tables? or
You need to print the amount in words and let's assume there is no function [ SPELL_AMOUNT ] available in SAP.
I don’t expect them to explain exactly the way it should be, neither do I know it by myself. But their approach to answer this question does help in identifying the real programmers.
 
How does a candidate react to these type of questions, is also very revealing. The programmers, who really like programming - logic, analysis etc. - they are pleasantly surprised. And those who are wrongly in this profession, will have a completely different reaction. They try to argue to prove it's an unnecessary question. They don't get the motive behind these questions. 
 
If spelling the amount seems to be too difficult then try with a coffee corner question. I like reading threads in the coffee corner at SDN and there was a funny question by Amit Khare.
 
WHO IS YOUR ROLE MODEL ?

Try it without looking at the answers.

1) Pick your favorite number between 1-9
2) Then Multiply by 3
3) Add 3, then again Multiply by 3
4) You'll get a 2 or 3 digit number.... right?
5) Add the digits together.
Example: if your number is 34, 3+4 so that your number is 7

Now with that number see who your ROLE MODEL is from the list below :

1. Mother Teresa
2. Nelson Mandela
3. Your mother
4. Your father
5. Bill Gates
6. Gandhi
7. Brad Pitt
8. Hitler
9. Amit Khare
10. Barack Obama

Well it does not take much of the available common sense to realize that you'll always get Amit Khare as your role model, in case you decided to calculate.

However, if you are a programmer, you may immediately get the urge to solve the logic (or maths) behind the equation, even if it's trivial.

Let's say the number that you choose ( between 1 to 9 ) is X.
Then the calculated number will be : 3(3X+3) =  9X+9 = (10X -X) + 9 = 10X + (9-X)
Basically, the idea is to arrange this in the form of (10*X + Y ) to represent any two digit number.
e.g. 24 =10*2 + 4

So the final number is 10X + (9-X) and that means the first digit of the number is X and 2nd digit has value (9-X). So the addition of the digits = X + (9-X) = 9.
Basically, you will always get 9 for this calculation, irrespective of your chosen number.
Well that does not require much of your logical skills.. so what about this? Write a program, which will generate these kind of questions, in favor of any of the selected role models :) . Have fun !!

Tuesday, February 3, 2009

Discovering a Hidden Gem :Generate Simple Transformation for XML in ABAP

Even in its most nascent form, this less known wizard of simple transformation, could be really useful for ABAP developers. I think, ABAP developers would love to have a graphical utility to generate simple transformation/XSLT code.

Almost a year back,  I was writing some happy-code to consume an external web service from ABAP. As the web service was returning the result data in the form of an XML string, I ended up writing a Simple Transformation to get the values in ABAP internal table.

Consuming webservice from ABAP is not the topic of this post but if you need help on this then please check the blogs on SDN. However, If you need to post an answer on a twitter-interview ( similar to a telephonic interview but held on twitter ) then there you go :
Xn SE80->Repository Browser->Enterprise Services->[ right click to bring the context menu ] -->Create Proxy [ use WSDL URL e.g. http://www.webservicex.net/uklocation.asmx?WSDL to generate proxy through wizard ]. Xn LPCONFIG to configure the logical port.

Back to the topic, I spotted this magic-stick in Simple Transformation editor [ Transaction XSLT_TOOL ] and tried to make it work. But could not decipher and wrote the ST manually, instead. Interestingly, I could not find any information  about this so called "Edit Simple Transformation Graphically" feature. In my defence, I searched on SAP OSS, SAP Help & SDN. There you go ...Well search feature on SAP Sites.. is great anyway ;)
A year later, I just happened to be adoring ( developers do this often ) my old code and it reminded me of this utility. I tried figuring out once again and managed to discover something worth sharing.
Service proxy call provides data in the form of XML result. Code for proxy call is as below:

CREATE OBJECT LO_UK_LOCATION.
TRY.
lv_input-POST_CODE = p_post.

CALL METHOD LO_UK_LOCATION->GET_UKLOCATION_BY_POST_CODE
  EXPORTING
    INPUT  = lv_input
  IMPORTING
    OUTPUT = lv_output.

 CATCH CX_AI_SYSTEM_FAULT .
   WRITE : 'CX_AI_SYSTEM_FAULT'.
 CATCH CX_AI_APPLICATION_FAULT .
   WRITE : 'CX_AI_APPLICATION_FAULT'.
ENDTRY.

Now the result variable lv_output will be having the data in the form of an XML string as below.
Tip: You can use a function module SCOL_TRACE_SHOW_XML to show the XML data on screen.
<NewDataSet>
                    <Table>
                              <Town>Balthangie</Town>
                              <County>Aberdeenshire</County>
                              <PostCode>AB53</PostCode>
                     </Table>
                     <Table>
                              <Town>Bankhead</Town>
                              <County>Aberdeenshire</County>
                              <PostCode>AB51</PostCode>
                     </Table>
</NewDataSet>
In order to transform the XML to ABAP internal table, I decided to use Simple Transformation. Now I will generate the simple transformation as below:

  1. Create Table Type ZNEWDATASET in transaction SE11. The hierarchy structure should be compatible with the XML hierarchy.

  2. Now create the ST program in transaction XSLT_TOOL. Spot the magic stick in the editor menu.

  3.  Click on 'Edit Simple Transformation Graphically' button (magic stick) and you will get the editor as below. Create a new root (right click - context menu ) in "Data Roots" section. I've named it NEWDATASET but the important thing is to enter the correct type, created earlier.

  4. Now you can see the data root hierarchy as below. Drag and Drop the NEWDATASET root on right panel ( Simple Transfromation ). It automaically generates the ST nodes corresponding to the ABAP structure. However, you need to adjust the names as per the XML element names.
  5. Now adjust the names in ST panel as per actual XML element names.

  6. Save and Activate. The ST program (Z_TEST), generated by the utility, is as below:
    <?sap.transform simple?>
    <tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
        <tt:root name="NEWDATASET" type="ddic:ZNEWDATASET"/>
            <tt:template>
                  <NewDataSet>
                        <tt:loop ref=".NEWDATASET">
                              <Table>
                                         <Town tt:value-ref="TOWN"/>
                                         <County tt:value-ref="COUNTY"/>
                                        <PostCode tt:value-ref="POST_CODE"/>
                              </Table>
                        </tt:loop>
                   </NewDataSet>
             </tt:template>
    </tt:transform>
  7. Now use the code below to get data into ABAP internal table.

    data : result type ZNEWDATASET, "itab of table type ZNEWDATASET
           wa_result like line of result.
    
            call transformation Z_TEST 
              source xml lv_output-GET_UKLOCATION_BY_POST_CODE_R
              result     newdataset = result[].
    
    LOOP AT result into wa_result.
      WRITE :/ wa_result-TOWN, wa_result-COUNTY, wa_result-POST_CODE.
    ENDLOOP.
    
It seems to be a very useful utility. I hope this will be further enhanced by SAP or the SAP Community.
By the way, I checked this on SAP ECC6.0. I will try to explore further for complex XML e.g. elements having attributes.

Please do add your inputs, if you managed to check this out or have done this before.

Updated: 
SAP Help document for ST is at http://help.sap.com/abapdocu/en/ABENABAP_XML.htm
* It's better to Display XML from ABAP as below :
cl_abap_browser=>show_xml( EXPORTING xml_string = xml_string ).

Sunday, January 18, 2009

Saving a few lines of ABAP Code

Well whatever you could save now a days :)
Not that it's going to earn much interest from the Banks.

Just in case you are tired of reading numerous thoughts on the global credit crunch, business ethics, corporate social responsibility, sustainability and would like to enjoy the quantum of technical solace then you are in luck. Thomas Jung & other enterprise geeks have started a new Blog Enterprise Geeks . Also, if you are not very particular about reading SAP / ABAP horrors only :) then Coding Horror is another good read.

And if you want some plain geeky fun then grab a coffee and hold it tight ... Coffee Corner at SDN :) ..there you go.....

Actually, I just wanted to wish you all a very happy new year. So I am wishing now ( It's never too late ).

And if you are still reading....
Off the top of your head, do you remember a few ways to save some coding effort through better use of ABAP statements?  To add a few from my side :

I've found that some developers still use function 'FORMAT_MESSAGE' or a table T100 to get the message text  [ e.g. After a function call or BDC CALL TRANSACTIONS ]. This function 'FORMAT_MESSAGE' is also used in stanadard SAP programs [ may be in old ones ].

I think a better way is to use '...INTO text' variant of message statement rather than calling functions or actually writing 60 lines of code to select the data from table T100 and then replace the & with variables.

DATA lv_message_text TYPE string.
CALL FUNCTION ... EXCEPTIONS error_message = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
INTO lv_message_text
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* Now use lv_message_text to prepare your error report
Write : lv_message_text.

Another important point : Do you know what is the effect of having an exception error_message in the function call? Like 'When_Others', this is not a defined exception in the function interface. However, it can be specified for any function call.

Some of the standard function modules throw error messages without the raising clause and that means you can't capture/handle these messages from your calling program...unless you copy that standard function and make it civilized or else use this exception error_messages while calling the function.

If the error_message addition is specified after EXCEPTIONS, then you should be able to capture error messages in the calling program, even if the function module does not have the RAISING addition for those messages.

I guess, it's always safer to call standard functions with the error_message exception. I wonder why SAP does not add this in the pattern for function call.... like WHEN_OTHERS.

Add your tips/views in the comment section. But I wish you a very happy new year anyway :)

Friday, July 11, 2008

How to get a quick response from SAP OSS?

  • If you are still dealing with on old SAP version then tell them it's affecting your plan to upgrade or eSOA transformation planned for 2012 :). A 'High' priority call for an old version does not carry same weight as it does for a new version. Thumb rule is that any SAP ERP version having characters [ e.g. B, C, D etc. ] should be considered dead and should not be treated.

  • Take a look at one of the SAP OSS Notes and prepare a very similar document [ except the OSS Note Number, that will be generated for you by OSS later ]. The document should clearly indicate the problem, prerequisite, version affected, scenario and of course a hint on correction required :). Make sure that no custom scenario/code is mentioned in the problem description. It means you should first be able to replicate the problem in a SAP standard scenario. If relevant, provide the information on which lines they should put the break-point along with the test data.
  • I wonder why SAP does not have a Big Switch to On/Off all custom code enhancements. For urgent situations, it can be really useful in establishing whether the problem is due to custom code or SAP standard. [ Deactivating specific BADIs and enhancements is OK but there is no Big Switch ]
  • Assign correct component responsible for the issue. In case you've found the exact problem in the code then check the component of that particular package. For example, if you are facing an issue with file download in one of the FI transactions that does not essentially mean it belongs to FI area. You may find that issue is due to Unicode or GUI related functions hence the component should be selected accordingly.

  • Remember that initially the message might get assigned to junior consultants. Their aim is to buy some time till an experienced consultant can have a look. At first stage, be prepared to get some not so perfect workarounds or even wrong suggestions. Your aim should be to counter incorrect suggestions and get your message assigned to a senior consultant, unless you want to get caught into a chain of OSS Notes and correction programs.

  • Always be ready with the system open for SAP to check even if you have replicated the problem for a standard scenario, they will check it on your system.

  • Understand that there are some good/practical reasons why SAP OSS does not work the way you think they should.

All said and done, I've high regards for SAP OSS as a SAP knowledge warehouse. There is a wealth of information, enough to resolve most of your SAP related issues. I use SAP OSS, not just to find corrections for the problems with SAP standard, but also while preparing my own custom solutions. There is good chance that the kind of solution you want to implement, the technical aspects of the solution might well have been used in a standard SAP solution.

If you know how to search for technical stuff, in other words, you don't have to feel lucky to find something relevant then SAP OSS is a very useful resource for learning, customising solutions and resolving bugs. You can use SAP OSS to find relevant APIs, application/config details, Guides, correction programs, code-snippets [ to decide an exact position for break-points rather than debugging a complete SAP application ].

Comments on your experience with SAP OSS will be appreciated.