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
xzuvelxzuvel 

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;
    }

xzuvelxzuvel

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.

ShikibuShikibu

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.

 

 

    public String getItems3()
    {
        List<Product2> proList = [select ProductCode from Product2 where name = :select2 limit 1];
        return prolist;
    }

 Here's how to get the productcode:

 

 

        return [select ProductCode from Product2 where name = :select2 limit 1][0].ProductCode;

Do be careful of exceptions if the Select returns an empty list.

 

 

 

 

xzuvelxzuvel

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.

ShikibuShikibu

Make a value required or set a default on the select.

forcedotcomforcedotcom

return prolist.ProductCode

forcedotcomforcedotcom

Second thoughts might need to be return prolist[0].ProductCode;