Change Data Sources at Run Time in SAP Design Studio

Recently whilst working on quite a complex SAP Design Studio Dashboard for a valued client we came across the situation whereby the  custom graphs and charts that we had developed needed to be changed at run time. This seemed, and in reality was, quite simple although information was sparse and so it took a little bit of digging to find the answer to our solution.

What was very clear was that the out of the box components provided by SAP all had a mechanism by which this could be done , by use of the .setDataSource() method which takes the name of  a data source within your Design Studio project. What was also clear was that the custom components created by us (based upon the samples given, in this case the simple table example)  did not expose this property. We went thorough the normal responses,

1. Ignoring the fact that a squiggly line underneath our setDataSource call meant that it would not resolve at run time… and tried to call it anyway.

2. Cursing. A good option always… I am well versed.

3. Considering replicating all of our components, one for each data source.

4. More cursing….Cursing the fact that we had even considered 3 an option given the complexity of our situation.

5. Abused Google with various incorrectly worded searches.

6. Considered becoming a monk, there are many many reasons why I could not do so, mainly cursing and the fact that I don’t believe in God

7. Trawled the forums looking for examples.

In the end solution 7 yielded a nugget that prevented the opening of the ‘First Order of Cursing Monks’ (Good band name right?!)

The answer was in fact ridiculously simple as all solutions are once you know them! Firstly we need to return to the custom component editor which is of course Eclipse,  within your custom component is a file entitled contribution.ztl.  If you open this file and look at the class that this extends from it is by default Component. Component of course knows nothing about how to deal with DataSources and so instead you need to change this file to inherit from DataBoundComponent like so:-

class com.dscallards.sap.uitable.UITable extends DataBoundComponent {
}

You can then save and run up the custom component in Design Studio and now you may observe that upon writing the following code you are left with no squiggly line to indicate that you have a compilation error.

myCustomTable.setDataSource(myDataSource);

And better yet, when you run the code you can see that the data source does indeed change when your event (in this sample case a button onClick)  is triggered.

Go do it.