You need to sign in to do that
Don't have an account?
Alibaba
How do I show an error but also reload page?
When a VF page fails a validation on Save, I want to show the error. This I can do by returning null from the controller (if I return a reference to the same page, the error doesn't display). The user then should theoretically go to another record to fix the issue causing the validation problem (which is on a different record).
However, if the user hits Save again, I get in trouble because the CaseComment id is already set (I get "Cannot specify ID in an insert call").
Right now, I'm simply getting a new CaseComment object when the first Save fails. Is there a more elegant way of doing this?
Thanks!
However, if the user hits Save again, I get in trouble because the CaseComment id is already set (I get "Cannot specify ID in an insert call").
Right now, I'm simply getting a new CaseComment object when the first Save fails. Is there a more elegant way of doing this?
Thanks!
You can use a transaction to ensure that whenever you gets an error, all the process be rolled back.
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Here's the code that handles the Save. You can see that, to avoid the problem, I am seting the casecomment pointer to null in case of an error. However, there is something inelegent about this.
Here's where I insert the case comment:
I think the issue its not the rolleback its the view state after the roleback its done. The variable its keeping some data.
I am still thinking how to clean the variables to their orginal state. But a work arround would be.
Change this.
ncasecomment=null;
for this
if(ncasecomment != null) {
ncasecomment.Id = null;
}
This way you clean the recently assigned Id.
Other way you can do its to instanciate the object, so any calculated field its also cleaned, but this will also clean you form.
Please let me know if this help.
Bill
Thanks. Setting "ncasecomment=null" as I do, does reinstantiate the object, because my getCaseComment function does this:
My point is that things are working now, but it seems that this problem would occur with any child object in a VF page, and I'm wondering what's an elegant way of dealing with this?
I don't have the full knowledge of the application but one way can be to create methods to validate all the data in the form. Not sure if its possible, sometimes complex data you need to insert to do some calculations, but if its possible to do the valiation, will be good to validate all before insert and show error messages before capturing the exception.
Other way can be to move all logic to a trigger so when account its inserted on that trigger to fire the invoice creation. This way if something fails everything will be unsaved and the view state will be also consistent as not Id will be generated.
But I will keep thinking about this issue, if its possible to partially clean the view state after a Database rolleback.
Regards,
Bill