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
Consider that you have created a Fiori application (Smart or not) with multiple filters and tables. In such a case you might want to help the users by providing default values to various filters, table columns etc. You can achieve that by creating Global Variants. But users still have to explicitly select this Global Variant you created and set as 'default'.  There is no SAP provided way as of now to set a Global Variant as default to all the users. Hope SAP provides this feature one day. Till then here is the programmatic solution.

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.

So lets get started!

Step 1. Create a new Workbench Transport (TCode SE10) in your Gateway system. This is used to store the Global Variant that you are going to create and move it all the way to Production.

Step 2. Open the Fiori application under consideration. Set the application status by selecting various filter values, selecting columns of tables, selecting width of layouts so on.

Step 3. Save this Variant as a Public Variant, by selecting the checkbox as shown below. Moment you check this box, you will be prompted for a transport request. Select the transport request you created in Step 1.



Step 4. At this point, you have the Global Variant and it is available to all users. Now you need to set this as default to all. First we need to find the technical id of this Variant. Refresh the application with Developer Tools open. Under network tab, filter the requests by search term 'lrep/flex'. Under the Preview tab, open the response. Under 'changes', there will be two entries with 'fileType' as "variant". First entry is for the SAP delivered "Standard" variant. Second one is for the new Global Filter you created in Step 3. Copy the content under property "fileName". This is the technical name of your Global Variant.



 

Step 5. Open the Fiori application where you need to set the default Variant. If it is a free-style application, you can do it in onAFterRendering event of the Controller. If it is an OVP, you need to create a custom controller and write in onAfterRendering event handler.

 
onBeforeRendering: function(){
//Get reference to a control which has Variant management
var oSmartFilter = this.getView().byId("ovpGlobalFilter");

//Set the Global Variant as the current Variant
oSmartFilter.getVariantManagement().setCurrentVariantId("id_1535046664297_171_page");
}

 

Step 6. If the user has already defaulted a Variant, then you do not want to overwrite that Variant. So ensure that you set the default Variant only if the user does not have a default Variant. Check the self explanatory code below.
onBeforeRendering: function(){
//Get reference to a control which has Variant management
var oSmartFilter = this.getView().byId("ovpGlobalFilter");

//Ensure that there is no default Variant set by the user.
//In such a case, do not set default Variant.
var sDefaultVariantKey = oGlobalFilter.getVariantManagement().getDefaultVariantKey();

//If No variant is set, default variant is "standard"
if (sDefaultVariantKey !== "*standard*"){
return;
}

//Set the Global Variant as the current Variant
oSmartFilter.getVariantManagement().setCurrentVariantId("id_1535046664297_171_page");
}

 

Hope it was helpful !!

 

Approach 2: (Update on Feb 19th, 2019)

If you are explicitly using control SmartVariantManagement in your application, you can do it in a better way by enhancing the SmartVariantManagement control.

Step 1. Create a 'controls' folder inside your 'webapp' folder and create a file by name 'SmartVariantManagement.js'.

Step 2. Enhance the SmartVariantManagement control as below.

Copy paste the below code into SmartVariantManagement.js. Ensure that <your component name> is replaced by your actual component name.
sap.ui.define(
['sap/ui/comp/smartvariants/SmartVariantManagement'],
function (SmartVariantManagement) {

return SmartVariantManagement.extend("<your component name>.controls.SmartVariantManagement", {
metadata: {
properties: {
fallbackDefaultVariant: {
type: "string"
}
}
},
renderer: function (oRm, oControl) {
SmartVariantManagement.getMetadata().getRenderer().render(oRm, oControl);
},
_getDefaultVariantKey:function () {
var defaultVariant = "";
if (this._oControlPersistence) {
defaultVariant = this._oControlPersistence.getDefaultVariantIdSync();
if (defaultVariant === "*standard*" || defaultVariant === "") { //No default variant was set by the user
defaultVariant = this.getFallbackDefaultVariant();
}
}
return defaultVariant;
}
});
}
);

Step 3. Add the namespace for your custom control in your XML view.
xmlns:cp="<your component name>.controls"

Step 4. Replace SmartVariantManagement with your new control.
<!--<smartVariantManagement:SmartVariantManagement id="pageVariantId" persistencyKey="PageVariantPKey"/>-->
<cp:SmartVariantManagement id="pageVariantId" persistencyKey="PageVariantPKey"/>

Step 5. Pass your default variant id in the above declaration as below.



 

Thats it.!
14 Comments
Labels in this area