on 2024 Jun 05 4:24 PM
Hello dear community,
I try to use a case statement instead of an if. This is a global script where I am writing the active page into the variable "page". Then I say, case Page_KPI or Page_Division (which are the actual names of the pages).
My problem, the coding is not working. Apparently, the coding can't read the variable "page". I tried
case (Page_KPI), but this doesn't work either. And if I use case "Page_KPI" I get the error message "cannot convert from string to "Application Page".
But what is really confusing me, the exact same coding is working fine, when using the if.
So where is the difference and how can I get the case statement working?
Thanks and BR Clemens
P.S: I try to use the case statement, as I read in older posts, if there are more than 3 if, use case. Not sure, if this is still valid though.
var page = Application.getActivePage();
switch (page){
case Page_KPI: Chart_KPI_Sales_KF.getDataSource().setVariableValue("X_CALMONTH_05", G_YYYYMM);
case Page_Division: Chart_Division_Sales.getDataSource().setVariableValue("X_CALMONTH_05", G_YYYYMM);
}
var page = Application.getActivePage();
if (page === Page_KPI){ Chart_KPI_Sales_KF.getDataSource().setVariableValue("X_CALMONTH_05", G_YYYYMM);
}
The switch statement in JavaScript uses strict equality check (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#description). Therefore, the expression in the switch and case clause must be of same type. "getActivePage" returns the active page object so you need to compare with a page object not a string. From your question I can't see where e.g. "Page_KPI" comes from.
For me both options are working, see below. However, there are two constraints:
1. This sample only works in a script on page 2, because you can only access the current active page. Maybe someone else has a recommendation or trick to work around that. Unfortunately there seems to be no method to get the page name which would be one option to avoid the reference to the other page object.
2. In the switch case the page in case must be a local variable (here "other_page"). This seems to be a bug.
var activePage = Application.getActivePage();
var otherPage = Page_2;
if (activePage == otherPage) {
console.log("if-yes");
} else {
console.log("if-no");
}
switch(activePage) {
// only works with local variables (seems to be a bug)
case otherPage:
console.log("switch-yes");
break;
default:
console.log("switch-no");
break;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
66 | |
10 | |
10 | |
10 | |
10 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.