2

I'm unable to save the changes that made in the visualforce page. I'm using <apex:inputtextarea/>

I have already spent good amount of time trying to figure out but I'm totally clueless now and I have tried different technic to save the record but its not saving the changes, what might be the wrong here?

PS: I'm not using anywhere attribute set to true

<apex:page controller="EmployeeDetailController" setup="false" showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false" >

<apex:messages />

<c:Sidebar />

<c:Header />

<apex:form id="frm1">
<apex:commandButton styleClass="btn btn-primary btn-sm" value="SAVE" action="{!saveMe}"    />

<apex:inputtextarea label="RichText" id="rt" richtext="true" value="{!employee.Description__c}" cols="100" rows="10"/>

<!--there is more code to it but its irrelevant for my problem-->

</apex:form>
</apex:page>

Controller:

public Employee__c employee {
    get {
          employee = [select id,name,description__c from Employee__c WHERE id = '123'];
         return employee;
    }
    set; 
}

Save action method:

public PageReference saveMe()
    { 
        try{ 
             //Employee__c emp = new Employee__c();
             //emp = employee;
             update employee;
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm, 'Saved Successfully!')); 
        }
        catch(Exception e) 
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));            
        }finally {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'error'));
        }
        return null;
    } 
  • when I debug it does fire the action method saveMe – Nick Kahn Feb 2 '17 at 3:59
  • Please check my ans and let me know if that works – Santanu Boral Feb 2 '17 at 16:34
3

A couple of observations:

  1. Due to the finally block, it will always show error message.

    Event if that is removed, it will not update the record.

  2. In the save method when you try to assign employee record, it practically using the getter, that's it initializes the data.

So, make following changes:

In Controller

public Parent__c employee {get;set;}

    public void init()
    {
        employee = [select id,name,description__c, Other__c from Parent__c WHERE id = ....];
    }

Visualforce

Use <apex:page> action to call init() method.

<apex:page controller="EmployeeDetailController" setup="false" showHeader="false" sidebar="false" 
standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false" action="{!init}">

It should work.

  • 1+ Thanks for the reply and it did work on my first test but I'm testing more to make sure it's working. I'm curious to know what was wrong on my approach? And why your approach is working? – Nick Kahn Feb 2 '17 at 16:37
  • Or I can also call init method from constructor instead of form action right? – Nick Kahn Feb 2 '17 at 16:42
  • I have mentioned, why your approach is giving the issue – Santanu Boral Feb 2 '17 at 16:46
3

Try lazy-loading the employee record. Right now each get call returns a fresh reference.

Lazy Load Pattern

public Employee__c employee
{
    get
    {
        if (employee == null)
            employee = [/*query*/];
        return employee;
    }
    private set;
    // the page *can't* even set this value...
}

A couple other comments about your code:

  • As mentioned elsewhere, your finally block is, at best, useless. Just remove it here. It always executes, and is likely to confuse your interface.

    The finally statement identifies a block of code that is guaranteed to execute and allows you to clean up your code. A single try statement can have up to one associated finally statement. Code in the finally block always executes regardless of whether an exception was thrown or the type of exception that was thrown. Because the finally block always executes, use it for cleanup code, such as for freeing up resources.

    None of the above recommendations for when to use a finally block apply in your situation.

  • You should return some sort of redirect PageReference on a successful save. As written, your method will never redirect.

    try
    {
        update employee;
        return new PageReference('/' + SObjectType.Employee__c.getKeyPrefix());
    }
    
  • You should know what type of Exception to catch! What are you expecting here besides DmlException? Also note it's much less verbose to call ApexPages.addMessages.

    catch (DmlException d)
    {
        ApexPages.addMessages(d);
    }
    
  • I did applied the lady load but still the same behavior is there a way to capture the errors? I did have try/catch but i never see any errors on the page but the problem is that after click on the save button it refresh entire page. – Nick Kahn Feb 2 '17 at 4:12
  • Add to your button a rerender of your <apex:messages> tag (need to add an id attribute to it first). Any specified rerender will prevent page refresh. – Adrian Larson Feb 2 '17 at 4:17
  • added <apex:commandButton styleClass="btn btn-primary btn-sm" value="SAVE" action="{!saveMe}" rerender="msgPanel" /> <apex:messages id="msgPanel" /> when i click on save button it does not refresh the page. – Nick Kahn Feb 2 '17 at 4:22
  • i did if you look at my previous reply i added to messages tag – Nick Kahn Feb 2 '17 at 4:27
  • i disabled the components tag and tried but still same behavior, its almost midnight here :) – Nick Kahn Feb 2 '17 at 4:37

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.