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
SFDC12SFDC12 

vf scenario

Hi every my scenario is to display the opportunity picklist in dropdown based on the picklist we select related records should display and if we click on update ,it should update ihave tried so far ,able to display picklist values but unable to display the records,below is the code

Apex:
public class opp1 {
    public List<SelectOption>options {set;get;}
    public List<opportunity>opportunities {set;get;}
    public List<oppwrapper>oppwraplist {set;get;}
    public string selected {set;get;}
    public opp1(){
       options=new List<SelectOption>();
        opportunities=new List<opportunity>();
        oppwraplist=new List<oppwrapper>();
        List<string>stagevalues=new List<string>{'prospecting','price/quote','need Analysis'}; 
            for(string op:stagevalues){
             SelectOption eachstage=new SelectOption(op,op);
               options.add(eachstage); 
            }
    }
    public void updatestages(){
        List<opportunity>opportunities=new List<opportunity>();
        for(oppwrapper ow:oppwraplist){
            if(ow.flag){
                ow.opp.stageName='selected';
                opportunities.add(ow.opp);
            }
        }
        
    }
    public class oppwrapper{
        public opportunity opp {set;get;}
         public boolean flag {set;get;}
    }    
    

}

vf:
<apex:page controller="opp1" >
    <apex:form >
        <apex:pageBlock >
            
      
           <apex:selectList value="{!selected}" size="1">
               <apex:selectOptions value="{!options}">
               </apex:selectOptions> 
               
            </apex:selectList>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!opportunities}" var="a">
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.stageName}"/>
                <apex:column value="{!a.Amount}"/>
                
            </apex:pageBlockTable>
            </apex:pageBlockSection>
      
             <apex:pageBlockSection >
            <apex:commandButton value="update" action="{!updatestages}"/>
        </apex:pageBlockSection>
            
        </apex:pageBlock>
       
    </apex:form>
    
</apex:page>
Best Answer chosen by SFDC12
Maharajan CMaharajan C
Hi Anshi,

Please try the below updated code:

VF Page:
 
<apex:page controller="opp1" >
    <apex:form >
        
        <apex:selectList value="{!selected}" size="1">
            <apex:actionSupport action="{!oppList}" event="onchange"
                                reRender="pb"/>
            <apex:selectOptions value="{!options}">
            </apex:selectOptions> 
        </apex:selectList>
        
        <apex:pageBlock id="pb" >
            
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!oppwraplist}" var="a" >
                    <apex:column >
                            <apex:inputCheckbox value="{!a.flag}" id="inputId"/>                        
                    </apex:column>
                    <apex:column value="{!a.opp.name}"/>
                    <apex:column value="{!a.opp.stageName}"/>
                    <apex:column value="{!a.opp.Amount}"/>
                    
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <apex:commandButton value="update" action="{!updatestages}"/>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
        
    </apex:form>
    
</apex:page>

Apex Class:
 
public class opp1 {
    public List<SelectOption>options {set;get;}
    public List<oppwrapper>oppwraplist {set;get;}
    public string selected {set;get;}
    public opp1(){
        options=new List<SelectOption>();
        oppwraplist=new List<oppwrapper>();
        List<string>stagevalues=new List<string>{'--None--','prospecting','price/quote','need Analysis','Closed Won','Closed Lost    '}; 
            for(string op:stagevalues){
                SelectOption eachstage=new SelectOption(op,op);
                options.add(eachstage); 
            }
    }
    public void updatestages(){
        List<opportunity>opportunities=new List<opportunity>();
        for(oppwrapper ow:oppwraplist){
               system.debug(' ******** ');
            if(ow.flag){
                system.debug(' ******** ' + ow.opp.Name);
                ow.opp.stageName='selected';
                opportunities.add(ow.opp);
            }
        }
        
        // uncomment below lines of you want to update the opportunity
        /* if(!opportunities.isEmpty()){
            update opportunities;
        }  */
        
    }
    public class oppwrapper{
        public opportunity opp {set;get;}
        public boolean flag {set;get;}
    }    
    
    public pageReference oppList()
    {
        oppwraplist=new List<oppwrapper>();
        system.debug(' ==> ' + selected);
        if(selected !=null) 
        {
            for(opportunity o : [select Name,StageName,CloseDate,Amount from Opportunity where StageName =: selected]){
                oppwrapper op = new oppwrapper();
                op.opp = o;
                op.flag = false;
                oppwraplist.add(op);
            }
        }
        system.debug(' ==> ' + oppwraplist);
        return null;
        
    } 
    
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Anshi,

Please try the below updated code:

VF Page:
 
<apex:page controller="opp1" >
    <apex:form >
        
        <apex:selectList value="{!selected}" size="1">
            <apex:actionSupport action="{!oppList}" event="onchange"
                                reRender="pb"/>
            <apex:selectOptions value="{!options}">
            </apex:selectOptions> 
        </apex:selectList>
        
        <apex:pageBlock id="pb" >
            
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!oppwraplist}" var="a" >
                    <apex:column >
                            <apex:inputCheckbox value="{!a.flag}" id="inputId"/>                        
                    </apex:column>
                    <apex:column value="{!a.opp.name}"/>
                    <apex:column value="{!a.opp.stageName}"/>
                    <apex:column value="{!a.opp.Amount}"/>
                    
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <apex:commandButton value="update" action="{!updatestages}"/>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
        
    </apex:form>
    
</apex:page>

Apex Class:
 
public class opp1 {
    public List<SelectOption>options {set;get;}
    public List<oppwrapper>oppwraplist {set;get;}
    public string selected {set;get;}
    public opp1(){
        options=new List<SelectOption>();
        oppwraplist=new List<oppwrapper>();
        List<string>stagevalues=new List<string>{'--None--','prospecting','price/quote','need Analysis','Closed Won','Closed Lost    '}; 
            for(string op:stagevalues){
                SelectOption eachstage=new SelectOption(op,op);
                options.add(eachstage); 
            }
    }
    public void updatestages(){
        List<opportunity>opportunities=new List<opportunity>();
        for(oppwrapper ow:oppwraplist){
               system.debug(' ******** ');
            if(ow.flag){
                system.debug(' ******** ' + ow.opp.Name);
                ow.opp.stageName='selected';
                opportunities.add(ow.opp);
            }
        }
        
        // uncomment below lines of you want to update the opportunity
        /* if(!opportunities.isEmpty()){
            update opportunities;
        }  */
        
    }
    public class oppwrapper{
        public opportunity opp {set;get;}
        public boolean flag {set;get;}
    }    
    
    public pageReference oppList()
    {
        oppwraplist=new List<oppwrapper>();
        system.debug(' ==> ' + selected);
        if(selected !=null) 
        {
            for(opportunity o : [select Name,StageName,CloseDate,Amount from Opportunity where StageName =: selected]){
                oppwrapper op = new oppwrapper();
                op.opp = o;
                op.flag = false;
                oppwraplist.add(op);
            }
        }
        system.debug(' ==> ' + oppwraplist);
        return null;
        
    } 
    
}

Thanks,
Maharajan.C
This was selected as the best answer
SFDC12SFDC12
Hi the above class i have tried testclass able to cover 62% how to cover remaining lines.

Thanks in Advance

@isTest
public class opo1Test {
@isTest
    static void call(){
        //sample data 
        opportunity o=new opportunity();
        o.stageName='prospecting';
        o.closeDate=system.today();
        o.name='testopp';
        insert o;
        //call the class name
        opp1 op=new opp1();
        op.updatestages();
        op.deleteoppt();
        op.oppList();
        
         opp1.oppwrapper ow=new opp1.oppwrapper();
        ow.flag=false;
        ow.opp.stageName='prospecting';
      
        
    }
    
}