+ Start a Discussion
harsha vardhan vasa 9harsha vardhan vasa 9 

i need save functionality for the below code

save button and when clicked that record with updated value should saved against that record.
when user updating the lookup on VF page it should get saved to record.
need save functionality.
Below code:
VF page:
<apex:page Controller="CustAccount" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages id="showmsg"></apex:pageMessages>
            <apex:pageBlockSection>
                <apex:inputText value="{!searchstring}" label=" enter username here" />
                <apex:commandButton value="Search" action="{!search}" reRender="accpg,showmsg" />
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!acc}" var="a" id="accpg" rendered="{!acc.size != 0}">
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.stagename}"/>
                <apex:column value="{!a.amount}" />
                <apex:column title="UserName" headerValue="Username">
                <apex:inputfield value="{!a.UserName__c}" /> 
               </apex:column>
                <apex:column>
                <apex:inputfield value="{!a.ownerid}" />
                    </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public with sharing class CustAccount {
    public string searchstring {get;set;}
    public List<opportunity> acc {get;set;}
    public custaccount() {
    }
    public void search(){
        if(searchstring !=null || searchstring!= ''){
            system.debug(searchstring );
            /*  acc= [SELECT Name, StageName, CloseDate, Amount, Owner.Name, Owner.Username,username__C
FROM Opportunity where Owner.username like : ('%' +searchstring + '%')];  */
            acc= [SELECT Name, StageName, CloseDate, Amount, Owner.Name, Owner.Username,username__c
                  FROM Opportunity where Owner.username like : ('%' +searchstring + '%')]; 
            system.debug('value os acc is '+acc);
            system.debug(acc.size());
            if(acc.size() ==0){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'No matching opportunities found for the entered value'));
            }
        }
    }
}
pls help me guys.
Regards,
harsha
Deepali KulshresthaDeepali Kulshrestha
Hi Harsha,
Please go through the following code as it may be helpful in solving your problem:
VF Page
<apex:page Controller="Controller_FTFView" >
    <apex:sectionHeader title="" subtitle="Mass Edit"/>
    <apex:pageMessages id="messages" />
    <apex:form >
            <apex:pageBlock mode="detail" title="Edit the following records" id="pb">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" rerender="pb,messages"/>
                <apex:commandButton action="{!cancel}" value="Cancel" rerender="pb,messages"/>
                 </apex:pageBlockButtons>          
            <apex:pageBlockSection columns="1">
                  <apex:pageBlockTable value="{!FTFRecords}" var="p">
                    <apex:column value="{!p.Name}"/>          
                    <apex:column value="{!p.WR_Number__c}"/>
                    <apex:column value="{!p.RFA_Number__c}"/>
                    <apex:column value="{!p.Closure_Outcome__c}"/>     
                    <apex:column headerValue="FTF?" >                   
                        <apex:inputField value="{!p.FTF__c}"/>
                    </apex:column>
                    <apex:column headerValue="FTF Reason" >                   
                        <apex:inputField value="{!p.FTF_Reason__c}"/>
                    </apex:column>
                    <apex:column headerValue="FTF Comment" >
                        <apex:inputField value="{!p.FTF_Comment__c}"/>
                    </apex:column>                    
                  </apex:pageBlockTable>
       </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:
public with sharing class Controller_FTFView{
    public List<PD_Record__c> FTFRecords {get; set;}

    public Controller_FTFView(){
        FTFRecords= [
            SELECT Name, Closure_Outcome__c, FTF__c, FTF_Comment__c, FTF_Reason__c, RFA_Number__c,WR_Number__c
            FROM PD_Record__c
            WHERE WMIS_Patch__c Like '11A5%' AND Owner_Is_Me__c = 0 AND Job_Status__c = 'Closed - Successful' AND (Closure_Outcome__c = 'Parts Ordered - In Stock' OR Closure_Outcome__c = 'Parts Ordered - OOS - Is obtainable' or Closure_Outcome__c = 'One Time Buy - Obtainable' or Closure_Outcome__c = 'Safety Reasons i.e. Manufacturer referral' or Closure_Outcome__c = 'Appliance Replacement Rec. (ART)')];
    }

    public PageReference save(){
        try{
            update FTFRRecords;
        }catch(DMLException e){
            //Report DML exceptions to the Apex Page messages element in Visualforce
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,e.getMessage()));
        }

        //Returning null for the page reference, directs user back to the same VF page, but rerenders values.
        //Otherwise, you could return another Visualforce page or a link to a record's detail page here as well.
        return null;
    }

    public PageReference cancel(){
        //Return a link to the parent record perhaps or do something else besides return null here
        return null;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.