cancel
Showing results for 
Search instead for 
Did you mean: 

Variant update problem with variant management control ( sap.ui.fl.variants )

bilen_cekic
Explorer
98

Hi Everyone,
I haven an issue with VariantManagement from sap.ui.fl library where it creates variant without any problem on `save` event. After it is saved, if i try to update it by adding new filters on screen but it fails to update variant. There is no error or anything on screen. When i check network tab, i can see request during initial save for creation but not with update. Overrite attribute is set to true and same codes are triggered in save. 
sample:

  variantManagerXML.attachEvent("save", (event) => {
                    try {
                        const filterData = mainFilter.getData();
                        const mParameters = event.getParameters();
                        mParameters.contexts = {
                            content: JSON.parse(JSON.stringify(filterData))
                        };
                        this.byId("vm").setModified(true);
                    } catch (error) {
                        console.error("Error during save:", error);
                    }
                });

 Where can be the problem on update ?

Accepted Solutions (1)

Accepted Solutions (1)

NilPeksen
Contributor

Hi Bilen,

I was able to resolve the issue by ensuring that the variant data is persistently stored rather than being directly assigned to filterData. The key improvement was persisting variants in localStorage, merging new data with existing data before saving.

 

variantManagerXML.attachEvent("save", (event) => {
    try {
        const filterData = mainFilter.getData();
        const mParameters = event.getParameters();
        let currentVariantKey = mParameters.key || this.byId("vm").getCurrentVariantKey();
        if (!currentVariantKey) return;

        // Retrieve existing custom variants from localStorage
        const customVariantData = JSON.parse(localStorage.getItem("customVariantData")) || {};
        const existingVariant = customVariantData[currentVariantKey] || {};

        // Merge new filter values with existing variant data
        customVariantData[currentVariantKey] = {
            ...existingVariant,
            XYZ: { values: [...filterData.XYZ.values] }
        };

        // Persist the updated variant only if overwriting is enabled
        if (mParameters.overwrite) {
            localStorage.setItem("customVariantData", JSON.stringify(customVariantData));
        }

        const sanitizedData = JSON.parse(
            JSON.stringify(filterData, (key, value) => 
                key === "values" && Array.isArray(value) ? JSON.stringify(value) : value
            )
        );
        mParameters.contexts = {
            content: JSON.stringify(sanitizedData)
        };

        mParameters.executeOnSelection = true;
    } catch (error) {
        console.error("Error during save:", error);
    }
});

 

I hope this helps anyone else looking for a similar solution!
Best,
Nil

bilen_cekic
Explorer
Thank you Nil it worked 😄

Answers (0)