0

I have created a visualforce report page to perform a search on Case object and displayed data in report format. I am trying to InlineEdit "Case Meeting Notes" field.

Issue 1: If I try to add text in Case Meeting Notes field for multiple records. Only first record got save.

Issue 2: When I perform search on this VF report, lets say, case xxxx is in Pending Closure status. At the same time, another co-worker updated this particular case xxxx to Closed status. Now, after some time, on the same VF report, when I added text to Case Meeting Notes field and hit Save button. Case Status got override back to Pending Closure.

Can you please guide me where I am getting wrong and what update should I make?

Thanks in advance!

Here is visualforce page:

<apex:page controller="VFC_CasesMultipleSearch" lightningStylesheets="true">
    <apex:form >
        <apex:pageBlock title="Case Details To Search">
            <apex:pageBlockSection >
                <apex:inputText value="{!cas.SAC__c}" label="Search SAC"/>
                <apex:inputField value="{!cas.Product__c}" label="Search Product"/>
                <apex:inputText value="{!cas.Service_Level__c}" label=" Search Service Level"/>
                <apex:inputCheckbox value="{!cas.CER_Enhancement_Request__c}" label="Enhancement Request?"/>
                <apex:inputCheckbox value="{!cas.Status_Closed__c }" label="Status Closed?"/>
                <apex:inputCheckbox value="{!cas.Internal_Only__c }" label="Internal Only?"/>
                <apex:inputField value="{!cas.From_Closed_Date__c}" label="From Closed Date"/>
                <apex:inputField value="{!cas.To_Closed_Date__c}" label="To Closed Date"/>
            </apex:pageBlockSection> 
            
            <!---Search Button--> 
            <apex:pageblockButtons html-align="left" location="bottom">
                <apex:commandButton value="Search" action="{!searchCase}"/>
            </apex:pageblockButtons>
            </apex:pageBlock>

        <apex:pageBlock title="Case Details" id="noRec" rendered="{! IF( caseList != null && caseList.size ==0 , true, false)}" >
            <apex:outputPanel >
                <h1>No Records Found </h1>
            </apex:outputPanel>
        </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 headerValue="Case Number" >
                    <apex:outputLink target="_blank" value="/{!c.id}">{!c.CaseNumber}. 
                    </apex:outputLink> 
                </apex:column>
                
                <apex:column value="{!c.CreatedDate}" headerValue="Created Date"/>
                
                <apex:column value="{!c.Product__c}" headerValue="Product"/>
                
                <apex:column value="{!c.Version__c}" headerValue="Version"/>
                
                <apex:column value="{!c.Environment__c}" headerValue="Environment"/>
                
                <apex:column value="{!c.Subject}" headerValue="Subject"/>
                
                <apex:column value="{!c.Status}" headerValue="Status"/>
                
                <apex:column value="{!c.Severity__c}" headerValue="Priority"/>
                
                <apex:column value="{!c.Contact_Name__c}" headerValue="Case Contact"/>
                
                <apex:column value="{!c.OwnerId}" headerValue="Case Owner"/>
                
               <apex:column value="{!c.Related_Defect__c}" headerValue="Related Defect?"/>

                <apex:column headerValue="Case Meeting Notes">
                   <apex:outputField value="{!c.Case_Comments_On_VFReport__c}">
                     <apex:inlineEditSupport showOnEdit="OnClick"/>
                   </apex:outputField>
                </apex:column>
            </apex:pageBlockTable>
     
            
                    <!---Save & Export as Excel Buttons -->
                    <apex:pageBlockButtons html-align="left" location="top">
                          <apex:commandButton value="Save" action="{!save}" />
                          <apex:commandButton value="Export as Excel" action=" 
                                     {!exportAsExcel}"/>
                    </apex:pageBlockButtons>
            
                    <!---Next & Previous Buttons -->
                   <apex:pageBlockButtons html-align="left" location="bottom">
                          <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}" />
                          <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}" />
                   </apex:pageBlockButtons> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Here is controller:

public with sharing class VFC_CasesMultipleSearch {
    
    public Case cas{get;set;}  
    public List<Case> caseList {get;set;}
    List<string> conditions = new List<string>();
    private integer totalRecs = 0;
    private integer offsetSize = 0;
    private integer limitSize= 50;
    
    public VFC_CasesMultipleSearch(){
        cas = new Case();
    }
    
    public void searchCase(){
        totalRecs = 0;
        offsetSize = 0;
        if(caseList !=null && caseList.size()>0){
            caseList=null;
        }
        searchCases ();
        conditions.clear();
    }
    
    //sorting columns logic
    public String sortDir {
        get{
            if(sortDir == null){
                sortDir = 'asc'; 
            } 
            return sortDir;  
        }
        set;
    }
    
    // the current field to sort by. defaults to casenumber
    public String sortField {
        get{ 
            if (sortField == null){
                sortField = 'casenumber'; 
            } 
            return sortField;  
        }
        set;
    }
    
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
        // run the query again
        searchCases();
    }
    //end sorting columns logic

    
    //Search button
    public Void searchCases(){
        
        if(caseList != null && !caseList.isEmpty()){
            caseList.clear();
        }
        
        if(cas.From_Closed_Date__c != null){
          String fromDate = cas.From_Closed_Date__c +'';
          fromDate = fromDate.split(' ',0)[0]+'T00:00:00.000Z';
          conditions.add('ClosedDate >='+ fromDate);
      }
      
      if(cas.To_Closed_Date__c  !=null){
          String toDate = cas.To_Closed_Date__c +'';
          toDate = toDate.split(' ',0)[0]+'T23:59:59.000Z';
          conditions.add('ClosedDate <='+ toDate);
      }
       
        if(cas.SAC__c !=null && cas.SAC__c !=''){
            conditions.add('SAC__c =\'' +cas.SAC__c+ '\' ');
        }
        
        if(!String.isEmpty(cas.Product__c)){
            conditions.add('Product__c =\'' +cas.Product__c+ '\' ');
            //system.debug('Product__c =\'' +cas.Product__c+ '\' ');
        }
        
        if(cas.Service_Level__c !=null && cas.Service_Level__c !=''){
            conditions.add('Service_Level__c=\'' +cas.Service_Level__c+ '\'');
        }
        
        if(cas.Status_Closed__c){
            conditions.add('Status_Closed__c=' +cas.Status_Closed__c);
        }else{
            conditions.add('Status_Closed__c=' +cas.Status_Closed__c);
        }
        
        if(cas.CER_Enhancement_Request__c){
            conditions.add('CER_Enhancement_Request__c=' +cas.CER_Enhancement_Request__c);
        }else{
            conditions.add('CER_Enhancement_Request__c=' +cas.CER_Enhancement_Request__c);
        }
        
        if(cas.Internal_Only__c){
            conditions.add('Internal_Only__c=' +cas.Internal_Only__c);
        }else{
            conditions.add('Internal_Only__c=' +cas.Internal_Only__c);
        }
        
         //create a dynamic query for filter results
        String strQuery ='SELECT Id, CaseNumber, CreatedDate, Product__c, Version__c, Environment__c, Status, ClosedDate, Status_Closed__c, Subject, OwnerId, Severity__c, Contact_Name__c, Related_Defect__c, DefectURL__c, Target_Date__c, Case_Comments_On_VFReport__c, SAC__c, CER_Enhancement_Request__c, Service_Level__c FROM Case';
        
        if (conditions.size() > 0) {
            strQuery += '  WHERE  ' + conditions[0];
            for (Integer i = 1; i < conditions.size(); i++){
                strQuery += '  AND  ' + conditions[i];
            }
        }
        
        if(totalRecs !=null && totalRecs ==0){
            List<Case> caseTemp = Database.query(strQuery);
            totalRecs = (caseTemp !=null && caseTemp.size()>0) ? caseTemp.size() : 0;
        }
        
        strQuery += ' order by ' + sortField + ' ' + sortDir + ' LIMIT :limitSize OFFSET :offsetSize';
        caseList = Database.query(strQuery);
        
    }// end Search button
    
    // Save button
    public PageReference save(){
        try{
            Database.upsert(caseList, false);
            system.debug('caseList'+ caseList);
            return ApexPages.CurrentPage();
        }
        catch(DMLException e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,e.getMessage()));
        }
        return null;
    }//end Save button
    
    //Export button
    public PageReference exportAsExcel(){
        PageReference vfPage = new PageReference('/apex/VFP_CaseReportExportPage');
        return vfPage;
    }
    
    // previous button
    public void previous()
    {
        offsetSize = (offsetSize-limitSize);
        searchCases();
    }
    
    // next button
    public void next(){
        offsetSize = offsetSize + limitSize;
        searchCases();
    }
    
    public boolean getprev(){
        if(offsetSize == 0){ 
            return true;
        }else {
            return false;
        }
    }
    
    public boolean getnxt(){
        if((offsetSize + limitSize) > totalRecs){
            return true;
        }else {
            return false;
        }
    }
}

1
  • Your code is ~50% larger than it should be if well optimized. You may want to search around here on SFSE to learn some tricks on optimization. – sfdcfox Apr 17 at 1:28
0

After saving, you need to perform a "search" again to get the latest values from the database. Redirecting to "ApexPages.currentPage()" won't update the records in the page memory.

public void save(){
    for(Database.UpsertResult result: Database.upsert(caseList, false)) {
      if(!result.isSuccess()) {
        return; // show errors
      }
    }
    // Everything saved okay, refresh page
    searchCases();
}
1
  • Hi @sfdcfox! Thank you for your response. I did add your suggestion, but it still saving 1st record only. Other consecutive records do not get insert neither get updated. – curiousCaseOfSfdc Apr 19 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.