Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
kammaje_cis
Active Contributor
Fiori Overview Pages are a great way to show a snapshot of the business data. Many a time you have Global filters to limit what do you want to see in those cards. Most of the times you will have a date range filter which will report data for current financial year, or last month so on.

I had such a date range filter on my OVP, and the requirement is to default the date range to last 90 days, so that user does not have to set it manually after opening it.

The first solution I tried was with a Global Variant. I created a Global variant, so that all users have it available. However there are couple of issues.

  1. You cannot set a Global variant as 'default' for all your users. (even if you are a power/key user). Hope SAP provides this functionality.

  2. For filters like Date range, you cannot maintain a static value there. It has to be dynamic, for example "last 90 days". There is no way to do that with Date Range control. However there is a 'DynamicDate' UI5 control, which provides you to specify "Last X Days" kind of values. But currently OVP pages do not respect the annotations for rendering a DynamicDate control, rather renders a Date Range control. (SmartFilterBar when used alone, respects those annotations though).


So I was exploring a programmatic solution and it turned out to be a simple solution. Here is how.

Step 1. Setup Extension Controller. In the manifest.json file of your OVP project, under "sap.ui5" node, maintain below extension.
"extends": {
"extensions": {
"sap.ui.controllerExtensions": {
"sap.ovp.app.Main": {"controllerName":"my.namespace.controller.customMain"
}
}
}
}

Step 2. Create the controller extension file. Create a controller folder, and then create a Controller file like below with name customMain.controller.js.
sap.ui.define([], function() {
"use strict";
return sap.ui.controller("my.nameSpace.controller.customMain", {

onBeforeRendering: function() {
//Get reference of Global FIlter
var oGlobalFilter = this.getView().byId("ovpGlobalFilter");

//Create JSON data to be defaulted
var oToday = new Date();
var o90DayesEarlier = new Date();
o90DayesEarlier.setDate(o90DayesEarlier.getDate() - 90);
var oDefaultFilter = {
InspectionDate: {
low: o90DayesEarlier,
high: oToday
}
};

//Default the Goabl filter values
oGlobalFilter.setFilterData(oDefaultFilter);
}
});
});

 

Step 3. Ensure there is no conflict with Variant Management

If you have enabled Variant Management, and if user has selected a default variant within OVP, then above code will overwrite that default variant. So you need to check if there is a default variant, only otherwise you have to set your defaults. Here is the updated code.
sap.ui.define([], function() {
"use strict";
return sap.ui.controller("my.nameSpace.controller.customMain", {

onBeforeRendering: function() {
//Get reference of Global FIlter
var oGlobalFilter = this.getView().byId("ovpGlobalFilter");

//Ensure that there is no default Variant set by the user.
//In such a case, do not set default values to the Gloabl Filter.
var sDefaultVariantKey = oGlobalFilter.getVariantManagement().getDefaultVariantKey();
//If No variant is set, default variant is "standard"
if (sDefaultVariantKey !== "*standard*"){
return;
}

//Create JSON data to be defaulted
var oToday = new Date();
var o90DayesEarlier = new Date();
o90DayesEarlier.setDate(o90DayesEarlier.getDate() - 90);
var oDefaultFilter = {
InspectionDate: {
low: oToday,
high: o90DayesEarlier
}
};

//Default the Goabl filter values
oGlobalFilter.setFilterData(oDefaultFilter);
}
});
});

Note: Instead of using setFilterData, you can set a Global Variant using oGlobalFIlter.getVariantManagement().setCurrentVariantId(<variant_id>) as well.

If you want the Project structure with the custom controller, here it is.



That's it. Hope it was useful!!

Important note: Like many of my blogs, this is a hack. Which means this is not an SAP documented supported scenario. So SAP does not guarantee that these APIs will work seamlessly across upgrades. Thanks prasita.prabhakaran for pointing that.

If you are on S/4HANA

and if you want to set up static default values on Fiori Elements applications, there is a solution needing no coding. You can choose default values at the Fiori Launchpad level and this gets updated to all the Fiori Elements applications. Blog here explains how to get this done. Thanks to jocelyn.dart for the context.

ashish.anand4 has mentioned a new way to achieve this in the comment section below. Standard S/4 Fiori elements can be extended using the extension method modifyStartupExtension to set the default variant. Read more here. Please blog if you achieve this.
9 Comments
Labels in this area