Skip to Content

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.

 

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

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

    (0) 
  2. 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

    (1) 
  3. 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

    (0) 
  4. 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

    (0) 

Leave a Reply