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
sfdc dev 2317sfdc dev 2317 

Need a help on save function of custom controller

 Below code was intended to do dynamic search on Case object and display results in pageblocktable. Now, I have added inlineEditSupport and save function to it. As I want to save values in inputField "Case Review Notes" field (Case_Comments_On_VFReport__c). When I click on Save button, it does save value, but only on UI. When I refresh page, value is gone. Also when I tried to query Case_Comments_On_VFReport__c in devconsole I don't see any update on this field.
I am just trying to get hands on Visualforce page and Apex. Can you guys please help me here with code?

Visualforce Page:
<apex:page controller="VF_CaseSearch" action="{!searchCase}" tabStyle="Case" sidebar="false">
    <apex:form >
        <apex:pageBlock>

            <apex:pageblockSection >
                <apex:inputText value="{!cas.CaseNumber}" label="Search Case Number"/>
            </apex:pageblockSection>
            
            <!---Search Button--> 
            <apex:pageblockButtons location="bottom">
                <apex:commandButton value="Search" action="{!searchCase}"/>
            </apex:pageblockButtons>
            </apex:pageBlock>
 
        <apex:pageBlock title="Case Details" id="details" rendered="{! IF( caseList != null && caseList.size >0, true, false)}" mode="edit">
            <apex:pageBlockTable value="{!caseList}" var="c">  
                <apex:column value="{!c.CaseNumber}" headerValue="Case Number"/>
                <apex:column value="{!c.CreatedDate}" headerValue="Created Date"/> 
                <apex:column value="{!c.Status}" headerValue="Status"/>
                <apex:column value="{!c.Severity__c}" headerValue="Priority"/>
                <apex:column headerValue="Case Review Notes">
                    <apex:inputField value="{!c.Case_Comments_On_VFReport__c}" />
                </apex:column>
            </apex:pageBlockTable>
           <apex:inlineEditSupport />
            
                    <!---Save Button -->
                    <apex:pageBlockButtons html-align="left" location="top">
                          <apex:commandButton value="Save" action="{!save}" />
                    </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Custom Controller:
public with sharing class VF_CaseSearch {

  public Case cas{get;set;}
  public List<Case> caseList {get;set;}
  List<string> conditions = new List<string>();
  
    public VF_CaseSearch(){
      cas = new Case();
  }
  
  public void searchCase(){
      if(caseList !=null && caseList.size()>0){
          caseList=null;
      }
      searchCase**s** ();
      conditions.clear();
  }
  
  
  public Void searchCase**s**(){
      if(caseList != null && !caseList.isEmpty()){
          caseList.clear();
      }

      //create a dynamic query for filter results
      String strQuery ='SELECT Id, CaseNumber, CreatedDate, Status, Severity__c, Case_Comments_On_VFReport__c, FROM Case';
    
      if(cas.SAC__c !=null && cas.SAC__c !=''){
          conditions.add('SAC__c Like\'%' +cas.SAC__c +'%\' ');
      }
           
      if (conditions.size() > 0) {
          strQuery += '  WHERE  ' + conditions[0];
          for (Integer i = 1; i < conditions.size(); i++)
              strQuery += '  AND  ' + conditions[i];
      }
      
      caseList = Database.query(strQuery);
  }
    
     // Save button
     public PageReference save(){
        try{
            Database.update(caseList);
            system.debug('caseList'+ caseList);
            return ApexPages.CurrentPage();
        }
        catch(DMLException e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,e.getMessage()));
        }
        return null;
    }
}


 
Best Answer chosen by sfdc dev 2317
Ashish Singh SFDCAshish Singh SFDC
Hi, 

I see two issue here:
1. The dynamic SOQL has select statement has comma(,) before FROM
2. You're VF is trying to update all the input field regardless of the case on which user is trying to update the case.

In Line 26, remove the comma before FROM in SOQL string and in line 44, add a false parameter in the update method of Database class i.e, change it to Database.Update(caseList,false);

This should allow you to save your record to the system. 

Please mark it solve, if it resolves your issue.

Thanks,
Ashish Singh.

All Answers

Ashish Singh SFDCAshish Singh SFDC
Hi, 

I see two issue here:
1. The dynamic SOQL has select statement has comma(,) before FROM
2. You're VF is trying to update all the input field regardless of the case on which user is trying to update the case.

In Line 26, remove the comma before FROM in SOQL string and in line 44, add a false parameter in the update method of Database class i.e, change it to Database.Update(caseList,false);

This should allow you to save your record to the system. 

Please mark it solve, if it resolves your issue.

Thanks,
Ashish Singh.
This was selected as the best answer
sfdc dev 2317sfdc dev 2317
Hi Ashish,

Thank you so much for the suggestion, it's spot on! And that was indeed a great catch for comma! 

User-added image

Cheers!