cancel
Showing results for 
Search instead for 
Did you mean: 

error on using "case" instead of "if" with "Application.getActivePage()"

paumcl
Explorer
0 Kudos
369

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);
}
View Entire Topic
Bob0001
Product and Topic Expert
Product and Topic Expert
0 Kudos

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;
}

 

 

paumcl
Explorer
0 Kudos
Hello Bob, thanks for your answer. I tried your solution and now at least it jumps into the coding. But the issue with the local variable seem to go much further.
paumcl
Explorer
0 Kudos
It looks like the case statement is not working at all within a global script as it does not trigger the coding Chart_KPI_Sales_KF.getDataSource().setVariableValue("X_CALMONTH_05", G_YYYYMM); at all. I will reise a ticket at SAP and would post their answer here as well.
paumcl
Explorer
0 Kudos
Hello Bob, looks like this is a bug which works as designed 😉 But htanks to your workaround it is working fine anyway.
paumcl
Explorer
I just received the following information from SAP: Dear Customer, The fix is ready. It will be inlcuded in the next patch. Best Regards, Tao