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
Soundar Rajan PonpandiSoundar Rajan Ponpandi 

Changed Lookup to picklist in Vf Page & Trying to save !!!

Dears,

1. I changed lookup to picklist in vf page
2. Selected picklist value is not saved in corresponding field.

Can anyone please advise ??

@controller
Public Class lookuptoPick{
Public String AddressId{Get;Set;}

public list<selectOption> getPickAccountVals(){
            list<selectOption> options  = New List<selectOption>();
            options.add(new selectOption('' , '-- None --'));
            for(Clinical_Address_Association__c claddr: [Select id,name from Clinical_Address_Association__c]){
                options.add(new selectoption(claddr.id,claddr.name));
            }
            system.debug('Options |' + options);
        return options;
    }
 Public void save(){
  // Insert Object Here 
 }

}


@VF PAge
<apex:pageBlockSectionItem dataStyle="width:1%" labelStyle="width:1%" >
   <apex:outputLabel >Address Id</apex:outputLabel>
    <apex:selectList size="1" value="{!AddressId}" label="Address ID" multiselect="false" style="width:180px">
         <apex:selectOptions value="{!PickAccountVals}" ></apex:selectOptions>
    </apex:selectList>
</apex:pageBlockSectionItem>

1. Getting AddressId by {Get;set;}
2. Then how can i save this object.

Thanks in advance !!

Regards,
Soundar P
 
Best Answer chosen by Soundar Rajan Ponpandi
AshishkAshishk
Add command button and call save method then perform dml.
 
<apex:pageBlockSectionItem dataStyle="width:1%" labelStyle="width:1%" >
<apex:form>
   <apex:outputLabel >Address Id</apex:outputLabel>
    <apex:selectList size="1" value="{!AddressId}" label="Address ID" multiselect="false" style="width:180px">
         <apex:selectOptions value="{!PickAccountVals}" ></apex:selectOptions>
    </apex:selectList>
<apex:commandButton value="Save" rerender="none" action="{!save}"/>
</apex:form>
</apex:pageBlockSectionItem>
Hope this works,
Thanks
 

All Answers

AshishkAshishk
Add command button and call save method then perform dml.
 
<apex:pageBlockSectionItem dataStyle="width:1%" labelStyle="width:1%" >
<apex:form>
   <apex:outputLabel >Address Id</apex:outputLabel>
    <apex:selectList size="1" value="{!AddressId}" label="Address ID" multiselect="false" style="width:180px">
         <apex:selectOptions value="{!PickAccountVals}" ></apex:selectOptions>
    </apex:selectList>
<apex:commandButton value="Save" rerender="none" action="{!save}"/>
</apex:form>
</apex:pageBlockSectionItem>
Hope this works,
Thanks
 
This was selected as the best answer
Soundar Rajan PonpandiSoundar Rajan Ponpandi
Hi Ashishk,

I am really abbriciate your answer ... i am getting following error while saving. Please Look my code below.

@controller
public with sharing class PSO_SiteAcc_ContAssoNewContr {
    public Clinical_Site_Association__c siteasso{get;set;}
    list<Clinical_Site_Association__c> siteassoList = New list<Clinical_Site_Association__c>();
    public string recId;
    public string siteid;
    public string View{get;set;}
    public string returnback;
    public string Account{get;set;}
    public string AccountId{get;set;}
    public String AddressId{get;set;}
    public string contactids{get;set;}
    public string likestring{get;set;}
    public String currentDomain{get;set;}
    public Boolean successfulFlag{get;set;}
    List<Clinical_Site_Association__c> options1 = New List<Clinical_Site_Association__c>();

    
    public Boolean isLightningMode{
        get{              
            return COM_Utils.isLightningMode();          
        }
        set;
    }

    public PSO_SiteAcc_ContAssoNewContr(Apexpages.standardcontroller stdctr) {
        siteasso = new Clinical_Site_Association__c ();  
        
        if(Apexpages.currentpage().getParameters().get('Id') != '') {
            recId  = Apexpages.currentPage().getParameters().get('Id');
        }

        if( string.isNotBlank ( recId ) ) { 
            siteasso =  [SELECT Study__c,Site_Id__c,Country__c,Account__c,Account__r.Type,Contact__c,Clinical_Site__c,Provide_Access__c,Name,Start_Date__c,Role__c,End_Date__c,Contact_Email__c,Contact_Address1__c,Address_2__c,Address_3__c,
                        Contact_Phone__c,Contact_City__c,Contact_Fax__c,Contact_State__c,Contact_Mobile__c,Country_Zip_Code__c,Alternate_Email__c,Country_Country__c,Address_ID__c,Clinical_Site__r.Principal_Investigator__c, Obsolete__c
                        FROM Clinical_Site_Association__c 
                        WHERE Id =: recId];      
        }

        if( Apexpages.currentpage().getParameters().get('ParentId') != '' ) {
            String siteidString = Apexpages.currentPage().getParameters().get('ParentId');
System.debug( LoggingLevel.ERROR,'@@@siteidString : ' + siteidString );
            siteid = siteidString != null && siteidString != '' ? siteidString : siteasso.Clinical_Site__c ;
System.debug( LoggingLevel.ERROR,'@@@siteid : ' + siteid );
            this.siteasso.Clinical_Site__c = siteid;            
        }
        
        if( Apexpages.currentPage().getParameters().get('View') != '' 
            && Apexpages.currentPage().getParameters().get('View') != null 
          ) {
             View = Apexpages.currentPage().getParameters().get('View');
        } else {
            View = siteasso.Contact__c == null ? 'Account' : 'Contact';
        }

        if(Apexpages.currentpage().getParameters().get('retURL') != '')  {
             returnback = Apexpages.currentPage().getParameters().get('retURL');
System.debug( LoggingLevel.ERROR,'@@@returnback : ' + returnback );
        }

        if(Apexpages.currentPage().getParameters().get('likeString') != null) {
             likeString = Apexpages.currentPage().getParameters().get('likeString');                
        }  
        if(Apexpages.currentPage().getParameters().containskey('currentDomain')) {
          currentDomain = Apexpages.currentPage().getParameters().get('currentDomain');
        }
        contactids = siteasso.Contact__c;       
        getsiteassRecord();   
    }
    
    
   
    public list<selectOption> getPickAccountVals(){
            list<selectOption> options  = New List<selectOption>();
            options.add(new selectOption('' , '-- None --'));
            for(Clinical_Address_Association__c claddr: [Select id,name from Clinical_Address_Association__c]){
                options.add(new selectoption(claddr.id,claddr.name));
            }
            system.debug('Options |' + options);
        return options;
    }
    
    
    public void Save() {
    try {
          successfulFlag = false;
          if( AccountId != null && AccountId != '' ) {
              siteasso.Account__c = AccountId;
              //siteasso.Address_ID__c = AddressId;
              System.debug('**********************Address Id' + siteasso.Address_ID__c);
            }
            
           //Clinical_Address_Association__c csa = [Select id,name From 
           Clinical_Address_Association__c Where Id =: AddressId]; 
           siteasso.Address_ID__c = AddressId; 
           system.debug('********Checking ' + AddressId);         
           upsert siteasso;
           system.debug('save method executed successfully');            
           successfulFlag = true;
        }catch(Exception e) {
            successfulFlag = false;
            Apexpages.addMessages(e);
        } 
    }
    
     
}

Note  : I am getting a following error

Error : Address ID: Value does not exist or does not match filter criteria.


Thanks in Advance !!!

Regards,
Soundar Rajan P
AshishkAshishk
Do you have any lookup filter on lookup field, where you are updating Address ID?