0

Could you please help me on getting the value of the boolean variable from apex controller method to VF Page. First i will tell my requirement, If the user unable to update the record "eruObj" he should get a pop up saying "Sorry! you dont have permission to update this record"

Below is my apex controller method, Here i have set the boolean variable "NoPermission=true" in the catch black.

    public PageReference saveMethod(){
    try{
    update eruObj;
    }
    catch(Exception e){
    NoPermission=true;
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'ERRRRORRR'));
    }

}

On VF page:

<script>
function doAlert(yesno){
ForAlert();
if (yesno)
alert('Sorry! You dont have permission to update this record');

}
</script>

<apex:outputPanel id="tstpopup">
<apex:outputPanel styleClass="popupBackround" layout="block" rerender="{!displayPopUp}"/>
<apex:outputPanel styleClass="custPopup" layout="block" rerender="{!displayPopUp}"/>
<table>
<tr>
<td>
<center>
<apex:commandButton value="Save" onComplete="doAlert({!NoPermission})" rerender="tstpopup"/>
<apex:commandButton value="Cancel" action="{ClosePopup}" rerender="tstpopup"/>
</center>
</td>
</tr>
</table>
</apex:outputPanel>
</apex:outputPanel>

<apex:actionFunction name="ForAlert" action="{!saveMethod}"/>

I am not getting the pop up in the first attepmt, but its working fine from the second attempt. It is because the value which i have assigned in the catch block is not passing in the vf page. so, at the first attempt the boolean varialbe is set to False, From the second attempt it is setting as True.

can you please help me on this,

Thanks in Advance!

1

Have a look at below fixed VF code. I feel This will resolve your issue:

<script>
var yesno=false;
function doAlert(){
if (yesno)
alert('Sorry! You dont have permission to update this record');
}
</script>

<apex:outputPanel id="tstpopup">
<Script>
yesno={!NoPermission};
</Script>
<apex:outputPanel styleClass="popupBackround" layout="block" rerender="{!displayPopUp}"/>
<apex:outputPanel styleClass="custPopup" layout="block" rerender="{!displayPopUp}"/>
<table>
<tr>
<td>
<center>
<apex:commandButton value="Save" action="{!saveMethod}" onComplete="doAlert()" rerender="tstpopup"/>
<apex:commandButton value="Cancel" action="{ClosePopup}" rerender="tstpopup"/>
</center>
</td>
</tr>
</table>
</apex:outputPanel>
</apex:outputPanel>
  • Thanks so much Mukesh! its worked fine for me.. Appreciate help!! – Sab__Dev May 26 '16 at 14:32
  • i have a small doubt, Why you have added the below script seperately <Script> yesno={!NoPermission}; </Script> why we should not merge it with the below script itself<script> var yesno=false; function doAlert(){ if (yesno) alert('Sorry! You dont have permission to update this record'); } </script> – Sab__Dev May 26 '16 at 15:15
  • I Tried to merge both the script as below but it doesnt work faced the same issue. Just curious to know about it.<script> var yesno=false; yesno={!NoPermission}; function doAlert(){ if (yesno) alert('Sorry! You dont have permission to update this record'); } </script> – Sab__Dev May 26 '16 at 15:26
  • For your understanding here is sequence: 1. Commandbutton calls your controlller function and executes statements in it. 2. It comes back to VF Page and rerender the block with id tstpopup . This make sure revised value of yesno is assigned to your javascript variable. If this step is not followed your yesno value will remain same as it was on first page load. 3. After rerender oncomplete is called. As yesno value is assigned by now your function works as expected. I hope that makes it clear. – Mukesh Verma May 26 '16 at 16:46
0

Just declare an Apex Properties for your Boolean in your class that makes it public as below:

Public Boolean NoPermission {private set;} = new Boolean(); 
NoPermission = false; // this sets the default value
  • HI @crmprogdev Thanks so much for your qucik response, Sorry i am new to this apex and salesforce. I am getting compile error "Error: Compile Error: unexpected token: '}' at line 41 column 44" after placing that code. Could you please provide the correct syntax. – Sab__Dev May 26 '16 at 13:55
  • Apologies, there should have been a semi colon after private set as in {private set;} – crmprogdev May 26 '16 at 14:45
  • NO issues @cemprogdev :) Thanks so much for your help :) – Sab__Dev May 26 '16 at 15:05
0

I like this approach (1 line)

public Boolean noPermission {get {return noPermission == null ? false : noPermission;} private set;} 

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.