I am trying to write a logic where I have to clone a custom objectwith its child (having lookup relationship). The clone record show appear with status 'New' and Date- Month fields for that record should be editable to the user . Else everything should be read only.

I am able to do the cloing perfectly with the logic written below but stuck with the portion of making the month date field editable only when clone button is clicked and status of the record is 'New'.

Could you please advice the steps!

1 Answer 1


I suspect the issue is with the setRedirect(true). When you set the PageReference to redirect the view state is wiped out. This means that the clone variable will have its initial value when the page loads. It doen't retain the value that you set.

I can think of two possibilities for you:

  1. Don't use the setRedirect(true) so that you don't lose the view state.
  2. Continue using setRedirect(true), but create a query to set the clone variable to something from a value in the Database, so that it isn't dependent on a variable set before the redirect is done.

I'm not 100% certain of how the clone variable is being set, though, so I might not be correct.

  • "clone" is just a Boolean variable which is set to true when the user clicks on clone button. It thought it would help me in making the pageblocksection visible/hidden by this line -- "<apex:pageBlockSectionItem rendered="{!clone}">". since the date-month field in the page will only become visible to user when he clicks on "clone" button.Rest of the times I need not to show this date-month field. Hope this helps.
    – user4644
    Dec 8, 2013 at 13:13
  • The clone doesn't make it hidden because of the redirect. Your code sets it to true, but then a redirect is done which clears the context/view state. If you are staying on the same page after the cloneAction is called then remove the setRedirect(true). You can just return null. There's some more explanation here. Dec 8, 2013 at 13:30
  • Peter Knolle, thanks for your i/p.however, can you suggest me what can be done in that case? I need to clone and after cloning happens have to redirect to the new record's page. thnks
    – user4644
    Dec 8, 2013 at 13:43
  • 1
    Sure. return new ApexPages.StandardController(theNewRecAfterInserted).view();, where theNewRecAfterInserted is the new record after you've inserted, so that it has an Id. That will redirect to the detail page for that record Dec 8, 2013 at 17:43
  • 1
    Basically: if (yourErrorCondition) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,’Error Msg')); return null; } and in VF in part of page that is rendered: <apex:pageMessages />. More on different messaging options here Dec 9, 2013 at 11:20

You must log in to answer this question.