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
Waqas AliWaqas Ali 

Show Contracts in VF and change status in VF through Picklist

I have button in opportunity page , When that button is pressed, I want to show my pending contracts in Visual force page and want o Expire or Laspe through VF page. Here is my class
public class myclass {
 
    
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    
    public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  
 records = new List<WrapperSObject>();
 for (Contract c : [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending'])
 {
 records.add(new WrapperSObject(c));
 records.add(new WrapperSObject(getOptions(), '--None--'));
}

             return null;
          
}

 

public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
      
    public WrapperSObject( List < SelectOption > options, String selectedValue) {
    
     this.options = options;
     this.selectedValue = selectedValue;

     }
     public WrapperSObject(Contract c) {
     con=c;
     }
     
     
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }

}

Here is my VF page.
<apex:page standardController="Opportunity"
 extensions="myclass"
 action="{!autoRun}">
  
  <apex:form >

<apex:pageBlock title="My Pending Contracts"> 
        <apex:pageBlockTable value="{!records}"  var="wrapper"> 
             <apex:column > 
                <apex:facet name="header">Contract Number</apex:facet> 
                <apex:outputText value="{!wrapper.con.ContractNumber}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Contract Name</apex:facet> 
                <apex:outputText value="{!wrapper.con.Status}"/> 
            </apex:column>
            
            <apex:column > 
                <apex:facet name="header">Change Status</apex:facet> 
                <apex:selectList value="{!wrapper.selectedValue}" size="1" >
                            <apex:selectOptions value="{!wrapper.options}" />
                        </apex:selectList>
            </apex:column>
                       
        </apex:pageBlockTable>
        
    </apex:pageBlock> 
    </apex:form>
    
   
  
 </apex:page>
Why this small blank Dropdown menu is showing ? 
How to fix this ?
User-added image

Now how to change the status of these contracts ? I mean how to return the value "Expired or Lapse" and update crosponding Contract ? 

Guys any help ?

 
KapilCKapilC
Hi Waqas,

In your class line numer 19 and 20 is the root cause of your problem. Actually you have created 2 constructors in your wrapper class. Always keep in mind if you have more than one consutructor in your class you can call only one at a time but in your code you are creating 2 records each time that is why you are always geeting just double records on your page. I have modified your class could you please test it with updated code. Let my know if it's not work. 

Thanks.
Kapil
 
public class myclass {
  
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    
    public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  
		 records = new List<WrapperSObject>();
		 for (Contract c : [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending']) {
			records.add(new WrapperSObject(c));
		 }
	 return null;
			  
}

 

public class WrapperSObject {
	public Contract con {get; set;}
	public List < SelectOption > options {get; set;}
    public String selectedValue {get; set;}
  
    
     public WrapperSObject(Contract c) {
     con=c;
	 // Calling Inner Class's Function To Populate SelectOption Which Will Be On VF Page.
	 this.options = getOptions();
     }
    // Inner Class Function To Create Option List.
	private List < SelectOption > getOptions() {
	List<SelectOption> options = new List<SelectOption>();
	    options.add(new SelectOption('--None--','--None--');
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    } 
     
  }
}

 
Waqas AliWaqas Ali
HI KapilC, 
Thanks a lot, Through your answer have solved my issue. Here is new code
public class myclass {
  
    String theId = ApexPages.currentPage().getParameters().get('id');
    
    public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    
    public myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
  
		 records = new List<WrapperSObject>();
		 for (Contract c : [select Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Pending']) {
			records.add(new WrapperSObject(c, getOptions(), '--None--'));
		 }
	 return null;
			  
}

 

public class WrapperSObject {
	public Contract con {get; set;}
	public List < SelectOption > options {get; set;}
    public String selectedValue {get; set;}
  
    
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;

     }
    // Inner Class Function To Create Option List.
	private List < SelectOption > getOptions() {
	List<SelectOption> options = new List<SelectOption>();
	    options.add(new SelectOption('--None--','--None--');
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    } 
     
  }
}


NOw i want to know that how to get selected value in picklist and update contract status through SOQL query??

 

One again Thank's a lot KapilC.

KapilCKapilC
Mark It Solved Dude :-)
KapilCKapilC
And removed '--None--' option from inner class as well.
Waqas AliWaqas Ali

yeah i'll mark but still i need to about 

how to get selected value in picklist and update contract status through SOQL query?? 

You can help ? KapilC

KapilCKapilC
Hi Waqas,
As you are having status field in your contract object so you can directly use that field on vf page by <apex:inputfield> tag. You don't have use slectoptionlist. Save the update contract list at last.

Thanks.
Kapil