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
arosysarosys 

static list in apex

I created a static list in my apex class like this :

 

Static List<String> Storeitems = new List<String>();
Static List<Integer> Qty= new List<Integer>();

 

I  am adding values in to it from my vf page like this :

 

<apex:commandLink action="{!addtolist}" value="Add to List">
<apex:param name="ProductId" value="{!prod.Id}"/>
</apex:commandLink>

 

And this is my function in apex class :

 

 

public pagereference addtolist(){

String ProductId = System.currentPageReference().getParameters().get('ProductId');

System.Debug('ProductId >>>>>>>>>>>>>>>>'+ProductId);

Storeitems.add(ProductId );
Qty.add(1);

 

System.Debug('StoreItems >>>>>>>>>>>>>>>>'+Storeitems.get(0));
System.Debug('Qty >>>>>>>>>>>>>>>>>>'+Qty.get(0));


Pagereference pg = new PageReference('/apex/SiteProducts');

pg.setredirect(true);

return pg;

}

 

 

 

But every time i am adding a new Productid in list , it shows the new one at list.get(0).

means its not appending it to existing one rather every time adding it as first element.

 

AsitM9AsitM9
<apex:commandLink action="{!addtolist}" value="Add to List">
<apex:param name="ProductId" value="{!prod.Id}"/>
</apex:commandLink>

 VF: SiteProducts

 

 

Class:  SiteProductsCon

Static List<String> Storeitems = new List<String>();
Static List<Integer> Qty= new List<Integer>();
public pagereference addtolist(){

String ProductId = System.currentPageReference().getParameters().get('ProductId');

System.Debug('ProductId >>>>>>>>>>>>>>>>'+ProductId);

Storeitems.add(ProductId );
Qty.add(1);
 
System.Debug('StoreItems >>>>>>>>>>>>>>>>'+Storeitems.get(0));
System.Debug('Qty >>>>>>>>>>>>>>>>>>'+Qty.get(0));

Pagereference pg = new PageReference('/apex/SiteProducts');

return pg;

}

 

sfdcfoxsfdcfox

Static variables are transient, which means that they do not retain their value each time the page is refreshed or submitted. Remove the static keyword to make them non-transient.

arosysarosys

After removing static also , list is not retaining the values.

AsitM9AsitM9
have you removed pg.setredirect(true) ?
arosysarosys

Ok its working fine after removing pg.setredirect(true).

But when i am refreshing the page its again gone.

As i am making these pages for site

i cannot restrict usres to refresh the page.

sfdcfoxsfdcfox
Using SetRedirect(true) will wipe out the current view state. You will have to use cookies if you not want to use the view state.