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
sheila srivatsavsheila srivatsav 

split custom setting values

I have a custom setting called BindValues__c which has a single field say value__c
It contains values as : A1  1   , A2  2   , A3  3  ,  A4  4 , A5  5 , A6  6 , A7 7 , A8  8 , A9  9 , A10  10 , A11  11

I have worked on a code which binds the values into a picklist as shown below , also I attached a screen shot to show my requirement.

custom setting
values

data
public class CodeGeneratorController {
    public List<SelectOption> Values { get; set; }
    
    public CodeGeneratorController() {
        getValues();
    }
    
    public void getValues() {
        Values = new List<SelectOption>();
        
        List<BindVaues__c> allValues = new List<BindVaues__c>();

        allValues=[SELECT  Name FROM BindVaues__c];
        
        for( BindVaues__c val : allValues) {
          Values.add( new SelectOption( val.Id, val.Name) );
        }
    }
}
<apex:page controller="CodeGeneratorController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputLabel value="Values :"/>
                  
                <apex:selectList size="1">
                    <apex:selectOptions value="{!Values}"/>
                </apex:selectList>
             </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
My requirement is I need to access values from 1 to 5 , then do some processing , then values from 6 to 11 then do some other logic.

so I need to split the custom setting values into 2 parts i. 1 to  5 and   6  to 11.
I hope I am clear.
Please let me know how I can proceed further.

Thanks
sheila
 
Best Answer chosen by sheila srivatsav
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Sheila, I'm not sure this is what you want -- but it creates two sets of SelectOptions, one for the first bunch of BindValues and one for the rest of the BindValues.  Hopefully it will get you pointed in the right direction.
public class CodeGeneratorController
{
    private final Integer valueSplit = 5;

    public List<SelectOption> firstValues
    {
        get
        {
            if ( firstValues == null )
            {
                firstValues = convertToSelectOptions
                (   [   SELECT  Id, Name
                        FROM    BindValues__c
                        ORDER BY Value__c ASC
                        LIMIT :valueSplit
                    ]
                );
            }
            return firstValues;
        }
        private set;
    }

    public List<SelectOption> lastValues
    {
        get
        {
            if ( lastValues == null )
            {
                lastValues = convertToSelectOptions
                (   [   SELECT  Id, Name
                        FROM    BindValues__c
                        ORDER BY Value__c ASC
                        OFFSET :valueSplit
                    ]
                );
            }
            return lastValues;
        }
        private set;
    }

    public List<SelectOption> convertToSelectOptions( List<BindValues__c> values )
    {
        selectOptions = new List<SelectOption>();
        for ( BindValues__c value : values )
        {
            selectOptions.add( new SelectOption( value.Id, value.Name) );
        }
        return selectOptions;
    }
}

<apex:page controller="CodeGeneratorController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputLabel value="Values :"/>
                <apex:selectList size="1">
                    <apex:selectOptions value="{!firstValues}"/>
                </apex:selectList>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:outputLabel value="Values :"/>
                <apex:selectList size="1">
                    <apex:selectOptions value="{!lastValues}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

All Answers

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Sheila, I'm not sure this is what you want -- but it creates two sets of SelectOptions, one for the first bunch of BindValues and one for the rest of the BindValues.  Hopefully it will get you pointed in the right direction.
public class CodeGeneratorController
{
    private final Integer valueSplit = 5;

    public List<SelectOption> firstValues
    {
        get
        {
            if ( firstValues == null )
            {
                firstValues = convertToSelectOptions
                (   [   SELECT  Id, Name
                        FROM    BindValues__c
                        ORDER BY Value__c ASC
                        LIMIT :valueSplit
                    ]
                );
            }
            return firstValues;
        }
        private set;
    }

    public List<SelectOption> lastValues
    {
        get
        {
            if ( lastValues == null )
            {
                lastValues = convertToSelectOptions
                (   [   SELECT  Id, Name
                        FROM    BindValues__c
                        ORDER BY Value__c ASC
                        OFFSET :valueSplit
                    ]
                );
            }
            return lastValues;
        }
        private set;
    }

    public List<SelectOption> convertToSelectOptions( List<BindValues__c> values )
    {
        selectOptions = new List<SelectOption>();
        for ( BindValues__c value : values )
        {
            selectOptions.add( new SelectOption( value.Id, value.Name) );
        }
        return selectOptions;
    }
}

<apex:page controller="CodeGeneratorController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputLabel value="Values :"/>
                <apex:selectList size="1">
                    <apex:selectOptions value="{!firstValues}"/>
                </apex:selectList>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:outputLabel value="Values :"/>
                <apex:selectList size="1">
                    <apex:selectOptions value="{!lastValues}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
This was selected as the best answer
v varaprasadv varaprasad
Hi Sheila,

Please check once below sample code : 
 
public class CodeGeneratorController {
    public List<SelectOption> Values { get; set; }
    
    public CodeGeneratorController() {
        getValues();
    }
    
    public void getValues() {
        Values = new List<SelectOption>();
        
        List<BindVaues__c> allValues = new List<BindVaues__c>();

        allValues=[SELECT  Name FROM BindVaues__c];
        integer i = 0;
        for( BindVaues__c val : allValues) {		
           Values.add( new SelectOption( val.Id, val.Name) );
		   i++;
		   if(i == 5)
		   break;
        }
    }
}



===================
allValues=[SELECT  Name FROM BindVaues__c limit 5];

allValues=[SELECT  Name FROM BindVaues__c offset 5 limit 5];


Using offset we can split first 5 records...


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com


 
sheila srivatsavsheila srivatsav
Thanks Anderson & Varaparsad.
That meets my requirement.

sheila