I am trying to call the JS method "returnToProposal" from an IF loop. Not sure what I am doing wrong here. I am getting the error

Error: Unknown function returnToProposal. Check spelling

Code: VF:

<apex:commandLink action="{!returnToQuote}" value="{!IF(riskRec.size>0 ,riskRec[0].Name,'')}" rendered="{!AND(!displayCartButton,isOppStageStageW == FALSE)}" rerender="theForm,pgMsgs" status="counterStatus" oncomplete="{!IF(riskRec.size > 0,returnToProposal('riskRec[0].id','closeTabFlag}'),'')}" id="theCommandLink1"/>


function returnToProposal(id,closeFlag){        
    if(closeFlag == 'true'){

What works:


But it gives error when riskRec list is empty

Subscript is invalid because list is empty

Error is in expression

returnToProposal('{!riskRec[0].id}','{!closeTabFlag}');' in component <apex:commandLink> in page wctaxanalysis_v2

What I need:

oncomplete="{!IF(riskRec.size > 0,returnToProposal('riskRec[0].id','closeTabFlag}'),'')}"

You're trying to call a JS function from within a Visualforce merge field, so Visualforce thinks you're trying to call a controller function/value. To fix this, you need to use the merge field just within the expression to evaluate on the server:

oncomplete="returnToProposal('{!IF(riskRec.size > 0,riskRec[0].id,''}','{!closeTabFlag}')"

You also could use an actual boolean value:

oncomplete="returnToProposal('{!IF(riskRec.size > 0,riskRec[0].id,''}',{!closeTabFlag})"

And change your script:

function returnToProposal(id,closeFlag){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.