3

I have a Visualforce Page that uses a standard controller for custom object and an extension. In the extension I have a method that get details of selected record, which is selected in select control. The extension is "with sharing". I am setting relational field's values from selected object to inputHidden control in VisualForce page. So I can use that values in javascript. When I try to do that I receive this error:

"system.security.NoAccessException: Create access denied for custom object, controller action methods may not execute"

I found one solution which is mentioned below post, I used Apex:Variable for storing values. system.security.NoAccessException: Update access denied for RecordType

But I am not able to access apex:variable in javascript.

Any help will be greatly appreciated

Apex Controller

public with sharing class SNQuoteController {

public SN_Quote__c objQuote { get; set; }
public list<SN_Product__c> lstProducts { get; set; }
public SN_Product__c selectedProduct { get; set; }

public SNQuoteController(ApexPages.StandardController stdController){
    try{
        selectedProduct = new SN_Product__c();
        lstProducts = [SELECT Active__c, Allow_Multiple__c, Allow_Multiple_Per_Service_Type__c, Allow_Overlapping__c, BusinessId__c, 
                       ConfiguratorType__c, ExternalId__c, IsRenewable__c, MaxQuantity__c, MinQuantity__c, Order_Types__c, OwnerId, 
                       Product_Base__c, Product_Line__c, Product_Line__r.Name, Name__c, Name, Id, Renewal_Terms__c, Required_Contact_Types__c, 
                       Service_Type__c, Start_Date_Rule__c FROM SN_Product__c WHERE Pricing_Type__c = 'Quote' AND Active__c = true ORDER BY Name];
    }
    catch(Exception e){
        throw e;
    }
}

public void GetSelectedProductDetail(){
    try{
        if(lstProducts != null && lstProducts.size() > 0){
            selectedProduct = (new Map<Id, SN_Product__c>(lstProducts)).get(objQuote.Product__c);
        }
    }
    catch(Exception e){
        throw e;
    }
}
}

Visual Force Page

<apex:page standardController="SN_Quote__c" showHeader="true" sidebar="true" extensions="SNQuoteController">
    <apex:form id="frmQuote" style="width : 100%;" >
         <apex:pageBlock title="Quote Detail" id="pnlMain">
               <apex:variable value = "{!selectedProduct.Id}" var = "selectedProId"/>
               <apex:variable value = "{!selectedProduct.Product_Line__c}" var = "selectedProLineId"/>
               <apex:selectList id="ddlProducts" size="1" value="{!objQuote.Product__c}">     
                     <apex:actionSupport event="onchange" action="{!GetSelectedProductDetail}" rerender="pnlMain" />                      
                     <apex:selectOptions value="{!lstProductOption}"/>
               </apex:selectList>
         </apex:pageBlock>
    </apex:form>
    <script>
        function openModal(){
            //I need to access those apex:variable here with frequent value changes as per apex:selectList onchange
            return false;
        }
    </script>
</apex:page>
1
  • Can you post the part of the code related to this question please?
    – MANUELAN00
    Commented May 19, 2016 at 6:08

3 Answers 3

2

Since you want to access the variable on change of the picklist, you would need to rerender the section containing script

<apex:page standardController="SN_Quote__c" showHeader="true" sidebar="true" extensions="SNQuoteController">
    <apex:form id="frmQuote" style="width : 100%;" >
         <apex:pageBlock title="Quote Detail" id="pnlMain">
               <apex:variable value = "{!selectedProduct.Id}" var = "selectedProId"/>
               <apex:variable value = "{!selectedProduct.Product_Line__c}" var = "selectedProLineId"/>
               <apex:selectList id="ddlProducts" size="1" value="{!objQuote.Product__c}">     
                     <apex:actionSupport event="onchange" action="{!GetSelectedProductDetail}" rerender="pnlMain,openModal" />    <!-- added openModal here, which contains the script -->                  
                     <apex:selectOptions value="{!lstProductOption}"/>
               </apex:selectList>
         </apex:pageBlock>
    </apex:form>
    <apex:outputPanel id="openModal">
        <script>
            function openModal(){
                console.log('{!selectedProId}');  //This would contain the updated data
                console.log('{!selectedProLineId}');
            }
        </script>
    </apex:outputPanel>
</apex:page>
0

you can access the apex:variables in javascript like;

function openModal(){
             alert("{!selectedProId}"+"   ---  "+"{!selectedProLineId}");
            return false;
        }
2
  • Thanks San Tosh and Saumya. For your quick reply, i already tried your solution. But I am getting this error. "Save error: Unknown property 'SN_Quote__cStandardController.selectedProId'" Commented May 20, 2016 at 12:59
  • You are getting becoz selectedProId is not declared in controller. You need to use document.getElementById() to access the select value in javascript function Commented Apr 20, 2017 at 16:51
0

In Apex Controller:

public class myController{

public String getValue{get;set;}
   public getValueString(){
      getValue = ‘Show this in VF Page’;
   }
}

In VF Page

<apex:page controller=”myController”>

<script> 
    var valuefromApex = '{!getValue}' 
    console.log('valuefromApex >>>>>> ',valuefromApex );
</script>

</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .