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
Olivia FordeOlivia Forde 

Save not returning to View Page

Hi
The Save button n my VF Form is not returning to the VIew Page so it is not obviouse that the record has saved. This is the controller code
    public PageReference save(){
        try{            
            
            upsert editItem;
                        
            system.debug(BusUnits.keyset());
                        
            for(String key:BusUnits.keyset()){
                if(BusUnits.get(key) == true){
                    tempMarketItem = MarketItems.get(key);
                    system.debug(tempMarketItem);
                    if(tempMarketItem.Items__c == null){
                        tempMarketItem.Items__c = editItem.id;
                    }
                    tempMarketItem.Business_Unit__c = key;
                    system.debug(tempMarketItem);                    
                    MarketItems.put(key, tempMarketItem);
                    upsert MarketItems.get(key);
                    for(integer i : numberOfDates.get(key)){
                        if((i < numberOfDates.get(key).size()) && (effectiveDates.get(key).get(i).Effective_Date__c != null)){
                            
                            tempED = new Effective_Date__c();
                            tempED = effectiveDates.get(key).get(i);
                            if(tempED.Market_Item__c == null){
                              tempED.Market_Item__c = MarketItems.get(key).Id;  
                            } 
                            effectiveDates.get(key).put(i, tempED);
                            upsert effectiveDates.get(key).get(i);
                        }
                        
                    }
                }    
                system.debug('BusUnitsOld' +BusUnitsOld);
                system.debug('BusUnits' +BusUnits);                
                if((MarketItems.get(key).Business_Unit__c == key) && (BusUnits.get(key) == false)){
                    delete MarketItems.get(key);
                }       
            }
                        
            PageReference home = new PageReference('/'+editItem.id);
            
            home.setRedirect(true);
            return home;
            
        }
        catch (Exception ex){
            ApexPages.addMessages(ex);
            system.debug(ex);
            return null;
        }
    }


And teh Form code
<apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save" id="theButton"/>        
        </apex:pageBlockButtons>
Abhishek_DEOAbhishek_DEO
Please replace the pagerefernce code with below code to see if it works

return new ApexPages.StandardController(editItem).view();
Abhishek_DEOAbhishek_DEO

Sorry!! did not understand. Could you please post the error?

Also, please see if using  "ID" in caps resolve the issue(in your old code.)

Olivia FordeOlivia Forde
Its bringing me back tot he View page but the data is not Saving now ( I have replaced all teh code inteh pagereference section
Abhishek_DEOAbhishek_DEO
Enable the debug log to check if there is any internal error. I thought you are getting issue in redirecting to view page but if the data is not getting saved then it could be a different issue
Olivia FordeOlivia Forde
The original code saves the data but does nto return to View, The code you gave returns to View but does not save the data
 
David OvellaDavid Ovella
can you try to put the "reference" out of the "try"
public PageReference save(){ 
try{                
   
}
catch (Exception ex){

}

PageReference home = new PageReference('/'+editItem.id);
home.setRedirect(true);
return home;

}