Monday 28 September 2015

Cell Variant: A way to use multiple editor in a single table column in webdynpro ALV

Cell Variant is a concept which comes handy when multiple cell editors need to be used for a single table column. 

Let's say we have a table with few columns. Now while using table as UI element, we create binding and while creating binding, we give information for cell editor also that this column of table will be say of type TextView, that column will be of type InputField. So as per the binding table columns will be displayed accordingly. 

That is the static way of creating cell editor for a particular column. Now one can have the requirement of using a different cell editor (than one which we have given during creation of binding) for a particular row based on some condition. At this point cell variant comes into picture. 

The context node which we will be displaying in table will have one extra attribute for cell variant key. This attribute's value will decide which cell editor will be coming into picture for a particular row. So we have to do our calculation in coding and assign appropriate value to this attribute.  

Column, which will be hosting different cell editors, will have the property 'selectedCellVariant' bind to the cell variant key attribute. Cell variant we can insert by right clicking on the column and further by right clicking on the cell variant we can insert the cell editor. In cell variant we have to give value to the property 'variantKey'. In run time for every record, cell variant attribute's value will be matched with this property's value. If it get's matched, corresponding cell editor will be displayed. Please make note that this value is case sensitive, so NOTE and Note will be considered different. 

So We will be doing a simple example on this. Example will have one table with 2 columns. First columns will have some numeric value and based on the first column's value, 2nd column will have different cell editors.

Make one WD component and in main view in context have one node with 2 attribute like below. 2nd attribute will serve the purpose of cell variant key.



In layout insert a table UI element and bind with the context node. In second column set the properties like below.


Under 2nd column insert cell variant like below. Insert one cell editor also per cell variant.








Do coding like below in WDDOINIT method of the view.





Do create one application and test it.


In case for any record the value of cell variant key attribute doesn't match with any existing cell variant's 'variantKey' property, below dump will occur.



Happy Learning :)

Sunday 20 September 2015

BADI implementation

Here we go witht he step by step guide on BADI implementation. In the first 2 posts, we have seen about the creation of BADI and about the fallback class. Here is the quick link of earlier posts. Post 1 & Post 2 . In this last post of this BADI series, we'll see about BADI implementation.
First Approach:
Go to the enhancement spot and click on the create BADI implementation button.


Give the Enhancement Implementation name and it's description. What enhancement spot is to BADI, the same is Enhancement implementation to BADI implementation. Enhancement implementation contains the BADI implementation.



Because we have a fallback class in place, we will get the below information.


Click on empty class.



Click on method to implement it.


activate every object. Now execute the program which we created in last post, where we use this BADI. Earlier because there was no active BADI implementation, fallback class's method was executed. This time because an implementation does exist, method from implementation should be called.

The highlighted text is coming from BADI implementation.

Second approach:
Because we have created this BADI, so we are aware about the enhancement spot name and everything. Otherwise in daily business when technical developer doesn't know about which BADI needs to be implemented, we take help of method GET_INSTANCE of class CL_EXITHANDLER. This class comes into picture whenever a BADI comes in a code. GET_INSTANCE method gives us all the BADI names which are being used in a screen. So all we have to do is to put a breakpoint at this method and do the normal execution. Everytime control stops at this breakpoint means a BADI has been used and parameter EXIT_NAME of method GET_INSTANCE gives us the BADI name. These BADIs are the classic BADIs. In SE19 we have that option of New BADI and Classic BADI. So now we have the BADI name and we need to implement it.

Goto SE19. In create implementation block, choose the classic BADI option and give the BADI name.
  
 Click on Create implementation button





We can see the implementing class here and we have 2 methods here. We can go inside that methods to implement it. 


So this is how we implement the classic BADIs.

Happy Learning :)

Monday 14 September 2015

Fallback class: A workaround of BADI implementation

In earlier post we discussed about why we need to create BADI and steps to do it. If you have missed that post Here You Go

As we have now created the BADI, we would be seeing how to use it in our code. I have created a simple ABAP program with one line to print.


Now I'll be calling that BADI in the code.


Now if we execute this program, it will go into dump.


The reason for this is that there is no active implementation exists yet for this BADI.

Note: Remember this dump occurs only if  'Multiple Use' option is unchecked in BADI definition. Means if you use in your code a BADI, for which multiple implementation CAN exists, but no active implementation yet exists, this dump will not occur.

So what is the remedy for this dump ? Well, we have not one but three :)

The very first is to catch the exception CX_BADI_NOT_IMPLEMENTED in your code. like below. Code will not go into dump instead will print the last line.



Second option is to implement the BADI, which we will cover in next post.

The third and the last is the magical Fallback Class :) . The dictionary meaning for fallback is 'an alternative plan that may be used in an emergency'. That's what exactly the fallback class is meant for.

Fallback class comes into picture when there is no active implementation exists for the BADI. In such case the code in fallback class's method executes. As soon as an implementation exists in system, the implementation call will be used automatically. This fallback class we give when we create the BADI definition.

We will be ticking the option for call Fallback class and a class name needs to be given there. Click on adjacent edit button.

Now if you execute the code, Both texts will be printed. One from the fallback class and one from the actual code.


Happy Learning :)

Friday 4 September 2015

Creation of BADI

Yes, you heard it right..It's about creation of BADI not the implementation of an existing ones. :)

So why do we even need to create a BADI? Well, SAP comes as a product to us SAP folks. Product means we are not allowed to touch the SAP standard code. When required we can only enhance the standard code. This enhancement works alongside the standard code. BADI is one of the few enhancement techniques SAP gives to us.

So, what if we create say some tool in SAP and we have few customers also who use that tool. Sounds cooool :). So to customers, this tool is a product. They must not be able to touch the standard/basic code of the tool. Now if user wants to do some sort of enhancement in the tool (of course they would be, they have paid for it), we have to provide the BADIs to accomplish this. So that's oen of the few cases where we would be creating BADIs.

Enhancement spot: Enhancement spot works as a container for BADI definition. Single enhancement spot may have multiple BADI definition.

BADI definition: BADI definition is the place which contains the interface. That interface will have methods in it. When we say we are implementing that BADI, we actually create a class, use that interface in it and implement the methods. These implementation of the methods are the place where user would be writing the custom code.

To start with, we would be creating an enhancement spot. Go in appropriate package, right click on it and follow the below path.



Now create BADI definition.




 You can check or uncheck the Multiple Use option, depending upon your requirement. If you want to do more than one implementation of the BADI, leave it as checked.

Create interface for BADI definition and activate everything.






What????? We are done with the BADI creation. Happy Learning :)