cancel
Showing results for 
Search instead for 
Did you mean: 

Store Values for later use SAP MDK

Ladidudidadacat
Explorer
0 Kudos
1,815

Hey Community,

i have a huge challenge. I have a analytic query in the backend and i want to compare a few values and calculate with them. I was able to create a rule which can compare on value with the other and i am also able to calculate. Unfortunately my program is really slow because i use around 4 of these rules on one page and everytime there is an odata call to the backend which takes a lot of time(10-20 sec per page).

Here a short description of what i want to do:

Get KPI grouped by Products for last month (Value1).
Get KPI grouped by Products for Last Year (Value 2).

Compare Value 1 with Value 2 and return the deviation.

etc...

Issue is that i am not possible to store the Value 1 and use it later in other rules. Result is here that i have to get the Value again from the backend when there is a new rule. I am also not able to use ".read(Services, Entity, QueryOption, etc)" 2 times in one rule and compare those values because they are objects when i try to use them 😞

So, at the end it is working but really slow. I heard something about clientData but i am not sure how to use to store values somewhere at runtime 😞 Would be great if you could help. Here is an example of my rule:

export default function GetPicturePath(context) {
//This is the first Value. I used the queryoptions to get the Values for this week. Later i compare this with the result of the .read
var binding = context.getBindingObject().SL; return context.read('/ServiceLevel/Services/AllSL.service', 'Z_Q_SL', [], '$filter=LastMonth eq \'' + 1 + '\'&$select=ArticleName,SL').then( function (result) { if (result.length > 0) { var ServiceLevel = result.getItem(0).SL; return (binding - ServiceLevel).toFixed(2); } else { return 200; } }).catch(function (error) { alert(error); }); }

Accepted Solutions (0)

Answers (3)

Answers (3)

bill_froelich
Product and Topic Expert
Product and Topic Expert

To get and set a value in application settings

export default function UseAppSettings(context) {
    // Get the appSettings module reference
    // Reference for the module is here https://docs.nativescript.org/ns-framework-modules/application-settings
    let appSettings = context.nativescript.appSettingsModule;
    var storedValue;
    // See if the key exists in appSettings
    if (appSettings.hasKey(`MyAppSettingsValue`)) {
        storedValue = appSettings.getString(`MyAppSettingsValue`);
    } else {
        appSettings.setString(`MyAppSettingsValue`,'My Stored App Settings String');
    }
}

Application settings values are persisted between application launches. If the user logs out of your application back to the welcome screen, as part of the logout process you should remove any application settings value you set. Do not blindly call the clear method on application settings as the client also stores values in there and clearing the application settings will cause problems.

bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Kudos

To set a value in Client Data

export default function SetClientData(context) {
// Client data is retrieved by calling getClientData() for the appropriate object
// In this example the next line is getting client data for the main page of the application
let clientData = context.evaluateTargetPathForAPI('#Page:Main').getClientData();
// Client Data is a javascript object.  Set whatever properties you want on the Client data
clientData.Message = 'My Client Data Value';
}

To retrieve the value in metadata use a dynamic target path binding

{
	"Message": "{{#Page:Main/#ClientData/#Property:Message}}",
	"OKCaption": "OK",
	"Title": "Main Page ClientData ",
	"_Type": "Action.Type.Message"
}

To retrieve the value in a rule just get the client data again and reference the property

export default function ReadClientData(context) {
// Client data is retrieved by calling getClientData() for the appropriate object
// In this example the next line is getting client data for the main page of the application
let clientData = context.evaluateTargetPathForAPI('#Page:Main').getClientData();
console.log(`Retrieved value from client data is: ${clientData.Message}`);
}
bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Kudos

Fabian,

There are two ways to easily persist values in your MDK application.

If the values need to be persisted across launches of the app then you can use application settings. Just remember to clean up your values if you reset/logout of the app. If you delete the app any saved values will be automatically cleared.

If you only need to store them in memory for the current session then you can use Client Data. Client data is associated to a UI element and will persist only for the life of that element. If you are looking to store state for the life of the application then typically you store it in Client Data at the Main Page level since the main page is usually loaded once and not reloaded during the application execution.

In your rule you can then retrieve the stored value and use it in the comparison without having to recalculate the value.

—Bill

Ladidudidadacat
Explorer
0 Kudos

Hey Bill,

thanks for your answer!
I still have issues to set this value and to get it later in another rule.
I have now two rules:
#1
export default function SetValueOne(clientAPI){

var pageproxy = clientAPI.evaluateTargetPath('#Page:Main/#Control:SectionedTable0/#Value');

pageproxy.setValue(5);

}

#2

export default function GetValueOne(clientAPI){
return clientAPI.evaluateTargetPath('#Page:Main/#Control:SectionedTable0/#Value');
}

But my result is still empty 😞
Would be sooo great if you could help here again 🙂 Some kind of example code snippet would be great. 🙂

Kind Regards
Fabian