0

I am calling an apex method from javascript using an actionFunction. In the apex method i am setting a variable to true. I am using this variable in my javascript.

Javascript:

function fraudCheck1(){
funcCheckFraud();     
var check = {!fraudCheckFlag}; 

if(check == false){
    var c = confirm('Mismatch found. Do you want to continue?');
    if(c == true)
        return true;
    else
        return false;
    }
 else{            
    return true;
    }
} 

<apex:actionfunction name="funcCheckFraud" action="{!checkFraud}" onComplete="alert('After apex method');"  reRender=""/>

Apex:

public void checkFraud() {
    fraudCheckFlag = true;
}

fraudcheckflag is getting set to true but when I'm using it in my javascript it is always giving false.

0

There are two thing here.

When you call actionfunction and its doing its processing in the meantime Javascript exceute the remaining code so it will always give you old value. To make this working you need to make few changes

function fraudCheck1(){
   funcCheckFraud();  
}
<apex:outputpanel id ="opPanel">
function fraudCheck2(){   
var check = '{!fraudCheckFlag}'; 

if(check == 'false'){
    var c = confirm('Mismatch found. Do you want to continue?');
    if(c == 'true'){
       //call action function here
        return true;
    }
    else
        return false;
    }
 else{
       // or call action function here
        return true;
    }
} 
</apex:outputpanel>
<apex:actionfunction name="funcCheckFraud" action="{!checkFraud}" onComplete="fraudCheck2()"  reRender="opPanel"/>

Call your Javascript method from your oncomplete of actionfunction

and second thing it will gie you strng in javascript so use '' quote to handle this.

  • I am calling fraudCheck1 on button click and if it returns true then only my form submits. How should i handle this now? – Krish Gambhir Jan 18 '17 at 8:22
  • @KrishGambhir instead of return true. Just call actionfuntcion to submit the data. – Tushar Sharma Jan 18 '17 at 8:32
  • where do i call my action function? @TusharSharma – Krish Gambhir Jan 18 '17 at 8:42
  • @KrishGambhir check my updated answer. – Tushar Sharma Jan 18 '17 at 8:45
  • fraudCheckFlag is getting set on call of actionfunction. So ill have to call it before@TusharSharma – Krish Gambhir Jan 18 '17 at 8:47

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.