+ Start a Discussion
lopezclopezc 

Content cannot be displayed: Invalid parameter for function URLFOR

HI,

 

I have a visualforce page embedded in a detail page, after pressing a button within my VF page, the oncomplete need to redirect the parent page, I am using JavaScript for this:  

 

 

<script language="JavaScript">

function openUserStoryView(workid){    
	parent.location.href = '{!URLFOR($Action.ADM_Work__c.View,workid)}';
}
 </script>

..................

<apex:commandButton value="Create Story" action="{!createStory}" oncomplete="openUserStoryView('{!recordCreatedId}')"/>

 The error appears when I reload the page, the visualforce page embedded shows:

 

Content cannot be displayed: Invalid parameter for function URLFOR


Any ideas why?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
lopezclopezc

oh, so that means I can't use URLFOR in my javascript? I changed the code for the following:

 

 

function redirectParentPage(recordId){
	if(recordId!=null){
	    parent.location.pathname = '/' + recordId;
        }
}

 

It works but I am not sure if this is the best solution

 

All Answers

AvromAvrom

So, the issue here is that $URLFOR() is evaluated on the server, but you're trying to pass in a parameter that's being passed to a Javascript function, which doesn't even exist at the time $URLFOR needs to be evaluated.

 

In other words, $URLFOR() needs to be evaluated before the function's (Javascript)  source code can even be *created*, and you're trying to pass it a parameter that will only be passed in when the function is *called*.

 

 

 

lopezclopezc

oh, so that means I can't use URLFOR in my javascript? I changed the code for the following:

 

 

function redirectParentPage(recordId){
	if(recordId!=null){
	    parent.location.pathname = '/' + recordId;
        }
}

 

It works but I am not sure if this is the best solution

 

This was selected as the best answer
AvromAvrom

I can't see a way to solve your use case using $URLFOR inside the Javascript. For clarity, it's not that it can't be used in Javascript at all; it's just that you can't refer to Javascript variables or functions inside of it.

 

I think the following would work, though, if you don't want to hardcode a URL:

 

 

<script language="JavaScript">

function openUserStoryView(usvURL){    
	parent.location.href = usvURL;
}
 </script>

..................

<apex:commandButton value="Create Story" action="{!createStory}" oncomplete="openUserStoryView('{!URLFOR($Action.ADM_Work__c.View,recordCreatedId)}')"/>

 

 

lopezclopezc

I tried your solution but I am still getting the same error message: Content cannot be displayed: Invalid parameter for function URLFOR

I will leave it with the url hardcoded, it seems to work ok.

 

Thanks for the help