function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
varri jahnavivarri jahnavi 

Passing Javascript variables to wrapper class

Hi All,

I am currently working on a Apex class, That's perform different functionalities.

Like pagination, search by keyword, and selecting the records and sending some of the data to second page. And in second page have some input text fields that were not a object fileds inturn they are individual fields which will autopopulate with default values when redirected from first page. Now here i need to enter the data for some input fields and on button click it will calculate the rest of the inpit fileds based on the data provided. Fot this calculation and passing the data i am writing a javascript junction. Here i am coming up with  a problem.

The problem is-
the values that were calculated in page using javascript were not passing to controller. Please can anyone help me on how to acomplish this task..
Here i have a pageblock table records that needs to be passed to controller.

Thank You.
SrikanthKuruvaSrikanthKuruva
why can't we do the logic in the class itself?
Vinit_KumarVinit_Kumar
Rather than using Javascript to pass values to 2nd page,why don't you use Pagereference method in controller,something like below :-

PageReference pr = new Pagereference('/apex/<your 2ndpage>');
pr.getParameters().put('id',<value>);
pr.setRedirect(true);
return pr;

In 2nd page,you just need to fetch what's there in 'id' param and then you are good to go.

If this helps,please mark it as best answer to help others :)
ezdhanhussainezdhanhussain
Yes I agree with Vinit Kumar.. Rather than using javascript to pass values it's easy and simple to use pagereference..
//page 1 code
<apex:commandbutton value="Take me to page 2" action="{!passid}"/>
//controller code of page 1
public id id;
public id id1;
 public PageReference passid() {
id='first id value';
id1 = 'second id value';
// here i am passing two parameters in url. you can pass more  parameters untill you haven't reached url limit 
    pageReference pr = new pageReference('https://c.ap1.visual.force.com/apex/productlist?addTo='+id+'&%retURL='+id1);
        return pr;
    }
//page 2 controller code
//write this in constructor of  page 2 so that you can retrieve values on page load
id id =  System.currentPageReference().getParameters().get('addTo');
id id1 =  System.currentPageReference().getParameters().get('retUrl');

Hope this answers your question..


varri jahnavivarri jahnavi
But the input text fields that i am using in second page doesn't belongs to first page. Then how can i can pass them in page reference??

Thank You
Vinit_KumarVinit_Kumar
Then,why do you want to send the values from 1st page to another if you have binded it on 2nd page ...
varri jahnavivarri jahnavi
some of the fields in first page i want to autopopulate in second page.. that's the reason i have passed them
Vinit_KumarVinit_Kumar
So for that you need to send the values using Pagereference like I told you earlier !!
varri jahnavivarri jahnavi
Let me give me an example-


here i have 2 pages using the same controller-

First page-
<apex:page Controller="accountnew" id="thePage">

<script type="text/javascript">
function total(index){
alert('hi');

    for(i=0;i<=index;i++)
    {
       document.getElementById('thePage:theForm:thePageBlock:theVar:thePageBlockTable:'+i+':hiddenField').value = 'this is a description';
      alert('new');
      }
      
}
</script>


  <apex:form id="theForm">
  <apex:pageBlock id="thePageBlock">
  <apex:variable value="{!-1}" var="index" id="theVar">
  <apex:pageBlockTable title="Products" value="{!account}" var="l" id="thePageBlockTable">
  
   <apex:column >
         <apex:inputtext value="{!l.name}"/>
    </apex:column>
   
     <apex:column >
       <apex:inputtext value="{!newdesc}" id="hiddenField"/>          /*   (Note---------------------> newdesc is not a account object field */
       <apex:variable value="{!index+1}" var="index"> </apex:variable>
    </apex:column>
   
    </apex:pageBlockTable>

    <input type="button" value="Total" onClick="total('{!index}');"/>
    <apex:commandButton value="save" action="{!newpage}"/>
      </apex:variable>
    </apex:pageBlock> 
    
     
  </apex:form>
</apex:page>


Second Page-
<apex:page controller="accountnew">

<apex:form id="theForm">

      <apex:commandButton value="test" action="{!test}"/>
     
     
      <apex:pageBlock >
  <apex:pageBlockTable title="Products" value="{!account}" var="l">
  
   <apex:column >
         <apex:inputtext value="{!l.name}"/>
    </apex:column>
   
     <apex:column >
       <apex:inputtext value="{!newdesc}" id="hiddenField"/>
    </apex:column>
   
    </apex:pageBlockTable>
   
    </apex:pageBlock>

     </apex:form>
</apex:page>


Contrller--
public class accountnew {

public String newdesc{get;set;}

public List<Account> getAccount(){

List<Account> rows = new List<Account>();
List<Account> selectlist = ([SELECT Id,Name FROM account limit 10]);


for(account b : selectlist){
   
     rows.add(b);
   }        
     return rows;   
   
}


public void test() {

system.debug('value:'+ newdesc);


}

public PageReference newpage() {
  pagereference pricePage = new pagereference('/apex/NewPricePage');
  pricePage.setRedirect(false);
  return pricePage;
}
}


Here scenario is when i redirect to second page defalut values needs to go in newdesc, that got populated from first page. Now in second when i am changing the newdesc values all are setting to the same value which i don't want...instead for each account name it needs to accept different newdesc.

ex:
From first page i get these results
Name    newdesc
AcctA      this is a description
AcctB     his is a description
etc...

In seconpage i am expecting the results to be when redirected from first page--

Name    newdesc
AcctA      this is a description
AcctB     his is a description
etc...

And now I should have ability to change the "newdesc" to different values and expecting output result is-
Name    newdesc
AcctA      9999
AcctB     8888
etc...

But i am getting the results as-
Name    newdesc
AcctA      9999
AcctB     9999
etc...

How to resolve it..Please help.



Vinit_KumarVinit_Kumar
As the controller is same,and you are setting the values with same variable,the value would be set would remain same in both the pages.

I would suggest to use different variable for both the textboxes.