Skip to Content
Technical Articles
Author's profile photo Krishna Kishor Kammaje

Fiori OVP: Defaulting values into Global Filters

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 Anand 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.

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Pierfrancesco La Spada
      Pierfrancesco La Spada

      Hello Krishna,

      the post is really cool!

      Could you please provide the folders structure of your project? I would like to check the route to custom controller to fix my case.

       

      Thanks

      Author's profile photo Krishna Kishor Kammaje
      Krishna Kishor Kammaje
      Blog Post Author

      Updated it. Let me know where did you get it wrong. I too had issues before I got it right.

      Author's profile photo Chris Mills
      Chris Mills

      Thanks so much for this blog Krishna, made me finally activate my SAP profile again to post here 🙂

      I'd been struggling with how to get a default to be applied and was messing around with extensions but couldn't figure this one out.

      In my case i'm using a dynamic default by calling a function import on my OData service in the onBeforeRendering method in the extended controller and works great, defaulting in user specific values obtained from backend.

       

      Cheers

      Chris

      Author's profile photo Krishna Kishor Kammaje
      Krishna Kishor Kammaje
      Blog Post Author

      Hi Chris, Glad it helped. And congrats for getting back to the community. ?

      Author's profile photo Jocelyn Dart
      Jocelyn Dart

      Hi Krishna

      Thanks - I've added this to the Fiori elements wiki.

      Can you please add a small comment re S/4HANA - in S/4HANA you can also pass User Defaults from the Fiori launchpad target mapping as per https://blogs.sap.com/2017/09/14/setting-user-defaults-in-sap-s4hana/

      Rgds

      Jocelyn

      Author's profile photo Tanveer Shaikh
      Tanveer Shaikh

      Thanks Krishna ! This is exactly what I was looking for.

      One more thing I was trying to do is hide/show 'shared' variants for various users based on some conditions.

      Can we show/hide variants to users using Variant Management API ? or any other way would you recommend ?

      Thanks,

      Tanveer

      Author's profile photo Krishna Kishor Kammaje
      Krishna Kishor Kammaje
      Blog Post Author

      Hi Tanveer, It can be done, but you need to find the backend services which are supplying these variants to the UI and filter them using ABAP implicit enhancement points.

      Author's profile photo Ashish Anand
      Ashish Anand

      Hi Krishna Kishor Kammaje

      From SAP Version 1.58, OVP support new break out named “modifyStartupExtension”, which can be used to set the default value of global filter dynamically. Please refer to the example below:

      modifyStartupExtension: function (oCustomSelectionVariant) {
       
          oCustomSelectionVariant.addSelectOption("SupplierName", "I", "EQ", "Talpa");
       
      }

      Official documentation for the same:

      https://sapui5.hana.ondemand.com/1.58.3/#/topic/4564eed27ead43cdb43f8d420470ddfd

      Hope this helps ?

      Thanks and Regards

      Ashish

      Author's profile photo Janith Illangakoon
      Janith Illangakoon

      How to set a value which gets from config parameter in launchpad instead of hardcoding it here