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
AlibabaAlibaba 

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!

 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

You can use a transaction to ensure that whenever you gets an error, all the process be rolled back.
 
Savepoint sp = Database.setSavepoint();

		try {
			// your code
		} catch (Exception error) {
			// Rollback transaction
			Database.rollback(sp);
			
			// Return the error
		}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
AlibabaAlibaba
Hi, Thanks for the quick reply. I did do that, but it doesn’t change the fact that CaseComment object already has an id assigned to it from the failed insert. Thanks!
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
I see, can you share your code so we can have a look?
AlibabaAlibaba
Sure, thanks!

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.
 
public override PageReference save() {
	
	//Set a savepoint so I can rollback if needed.
	Savepoint sp = Database.setSavepoint();
	
	//Try to create new case comment
	If (CreateCaseComment() == True) {
		//Try to create new invoice
		if (CreateInvoice() == True){
			//Send user to the updated case with the new invoice.
			return ( new ApexPages.StandardController(ncase)).view();		
		}
	}
	
	//Didn't work. Rollback and send user back to this page
	Database.rollback(sp); 
	//Reset the case comment.
	ncasecomment=null;
	return null;
}
Here's where I insert the case comment:
protected Boolean CreateCaseComment () {
	
	try {

		if (ncasecomment.ParentId == null){
			ncasecomment.ParentId = ncase.id; //Make sure that you're not trying to write this field if second time here.
		}
		insert ncasecomment;
		}	catch (Exception e){
		ApexPages.addMessages (e);
		return false;
	}
	return true;
}



 
William LópezWilliam López
Hello Alibaba,

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

 
AlibabaAlibaba
Hi Bill,

Thanks. Setting "ncasecomment=null" as I do, does reinstantiate the object, because my getCaseComment function does this:
 
public CaseComment getCaseComment() {
	
	if (ncasecomment==null) ncasecomment = new CaseComment();
    return ncasecomment;
}

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?
 
William LópezWilliam López
I see, well I would say maybe change the architecture a littble bit, the issue is you are saving error messages in the view state apex messages but this also mean some objects in the view state like the account keep and unvalid stage afer a rolleback.

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