You need to sign in to do that
Don't have an account?
How to get a STRING value from a LIST<Product2> pro;
hi im trying to get the string value out of a List<product2> ( list of products), So I can display it on a apex:outputtext.
Al the help is really appreciated.
My VF page Code :
<apex:selectList title="Marca" id="selectlist2" value="{!select2}" size="1">
<apex:actionSupport event="onclick" reRender="output"/>
<apex:selectOptions value="{!items2}"/>
</apex:selectList>
<apex:outputText id="output" style="font-style:italic" value="{0}" rendered="true" title="Producto">
<apex:param value="{!items3}" />
</apex:outputText>
My controller Code:
public List<SelectOption> getItems2()
{
List<SelectOption> options = new List<SelectOption>();
if(select1 == 'Modem1'){
List<Product2> proList = [select name from Product2 where ProductCode like '0001-%'];
for(product2 a : proList)
options.add(new SelectOption(a.Name,a.Name));
}
else if(select1 == 'Modem2'){
List<Product2> proList = [select name from Product2 where ProductCode like '0000-%'];
for(product2 a : proList)
options.add(new SelectOption(a.Name,a.Name));
}
return options;
}
public List<Product2> getItems3()
{
List<Product2> proList = [select ProductCode from Product2 where name = :select2 limit 1];
return prolist;
}
I need to return a string value from my list :
public String getItems3()
{
List<Product2> proList = [select ProductCode from Product2 where name = :select2 limit 1];
return prolist;
}
Right now is returning the id of the product and i need the product code. Is there anyway to get the ProductCode instead of the product2 id. Thanks For all The help.
Hmm. I would not expect the following code snippet to compile. You cannot return a List<Product2> where you declare the return value is a String.
Here's how to get the productcode:
Do be careful of exceptions if the Select returns an empty list.
Okei now im recieving System.ListException: List index out of bounds: 0. since the list empty because I have not selected anything yet on my picklist. Is there a possible way to fix this any workaround.
Thanks for the information in advance.
Make a value required or set a default on the select.
return prolist.ProductCode
Second thoughts might need to be return prolist[0].ProductCode;