TransWikia.com

Call JS method from IF loop in Visualforce

Salesforce Asked by Shaleen Yadav on December 17, 2021

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"/>

JS:

function returnToProposal(id,closeFlag){        
    if(closeFlag == 'true'){
        window.open('/'+id,'_self');
    }
}

What works:

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

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}'),'')}"

One Answer

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){
    if(closeFlag){
        window.open('/'+id,'_self');
    }
}

Answered by sfdcfox on December 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP