+ Start a Discussion
hisrinuhisrinu 

How to display error message only for the first time?

Hi,

 

 I want to display an error message when a duplicate account has been created.

 Once I shown the error message, still user wants to create new record it has to save.

 

 Any suggestions or thoughts on this?

mikefmikef

There are two ways to solve your issue, the way I see it.

 

First approach; 

Create a Visualforce page that the users are directed to every time they click save.

In this page you can display a more friendly warning message using the ApexPages.Message class.

 

Second approach;

In the before insert trigger, query for dup accounts. If you find any dups based on your query, you can use the .addError() method to write out your error message. You will need to populate a variable to bypass the second time.

 

 

I highly suggest using the first approach, for two reasons. One triggers a to be used for non UI type processing, and there for can process multiple records at a time. You would need to check if the trigger contained more then one record and skip this process if it did.

Second Visualforce is designed to be a UI process and will insure you are dealing with one Account at a time.

 

 

SovaneSovane

I would go for the visualforce approach too.
However the second approach made me wonder :
how would it be possible to know it is the second time you access the record as the addError prevent the insert to be commited ?

Sovane

hisrinuhisrinu

Hi Mike,

 

 I tried the first way, through the trigger we can't return to a page so what I did was I invoked an apex class from the trigger from there I am trying to return but it is not returning the VF page.

 

 

Trigger

trigger Duplicate_Check on Account(before insert)
{
for(Account a : Trigger.New)
{
Integer count = 0;
count = [select count() from account where Name = :a.Name];
if(count>0)
testing.test123();
}
}

Class
Public class testing
{
Public static PageReference test123()
{
return page.testing;
}
}

Second approach is not clear for me, can you explain more on this.

 

Please let me know if there are any other ways.

 

 

Message Edited by hisrinu on 02-03-2009 04:29 AM
mikefmikef

You can't return a page when inside the trigger, even from a class.

 

You need to redirect the save button to a visualforce page.

hisrinuhisrinu

Hi Mike,

 

I really didn't get any answer from your reply, how do we redirect on click of save from vanilla salesforce to VF screen. If you have any such example can you just post it here.

 

Once again thanks for your reply.

garybgaryb

We did something like this recently. We used a standard Salesforce page and a trigger; if the record is a duplicate, the trigger logic would add an error to the page. However, we also had a checkbox called "Override" - if this was checked, the logic to check for a duplicate is not executed. We had to implement this solution so that it worked for many records, so it is possible to do such a thing. The main thing to watch out for is you need to set the override checkbox back to unchecked in your trigger logic when saving the record, otherwise when you save with the box checked, it will remain checked!

hisrinuhisrinu

Hi,

 

Thanks for your reply.

 

I tried this way earlier, once you show the error message how can we update the fields in that record?

It is a little bit tricky one. Can you post your code here, so that we can check it out.

garybgaryb

What we did may differ slightly from what you are trying to do, so what I describe below is what we did rather than specific advice about what you should do - hopefully you should be able to fill in the gaps!

 

Let me start by explaining the user's experience. 

 

The user wants to update a record (I'm describing update but this applies to insert as well). He updates a field to a value that we determine may not be valid by looking up against an object in a trigger. When the user clicks save, they are presented with a warning at the top of the screen, with an instruction that if they still want to proceed, there is a checkbox they can check. The user checks the checkbox and clicks save again. The record is saved without warning.

 

The cause of your problem is that there's no way of warning a user and letting them decide to do it anyway. You can only prevent the action from happening (addError()) or you can let it happen with no warning. The checkbox method I've described above kind of combines these two options. You use the addError to inform the user (of a duplicate in your case); you then use a checkbox to let them ignore the error.

 

I can't post our code here, but here's soem psuedo-code.

 

As it is (in your case), we have some logic that checks to see if there's a duplicate - if there is, we report an error. Presumably this is in a trigger and looks something like:

 

 

boolean hasDuplicate = checkForDuplicate();

 

if(hasDuplicate) {

      object.addError('It looks like the object your inserting is already in the database');

}

 

So the user tries to insert a duplicate and is stopped from doing it. Let's say there is now a checkbox on the object called Override__c. If the user has checked this box, we let them perform the operation i.e. we do NOT add the error. Pesudo-code:

 

 

if(object.Override__c == false && hasDuplicate) {

      object.addError('It looks like the object your inserting is already in the database. If you want to insert it anyway, check the "Override" box and click Save again.');

}

 

 

In this case, if that box is checked, the first part of the if statement is false and so the addError is not executed. Notice we've updated the error message as well.

 

One other thing with this method: when the object is saved, the value of Override will be saved along with it, so if the box has been checked, it will be stored as true in the database. If someone views or edits the record, they will not be warned. Maybe this is OK in your case, if it's not you need a way of setting the checkbox back to false. You can do this at the end of your trigger by updating all the objects checkboxes to false (or similar).

 

What we did was to do it in the before trigger, as in before triggers you can change the value of fields. To do that in an after trigger requires a database operation. Our problem was that we needed the value of the field in an after trigger; if we set it to false in the before trigger, the after trigger is always going to read the value as false! The way we got around this was to use a static map of booleans keyed by the ID of the object they relate to. In the before trigger we store the value of the field against the ID of the object; we then set it to fals and in the after trigger we use the static map variable.

 

Make sense? I hope so :)

 

Thinking out loud: I think it may be possible to use an s-control to do what you need, but I don't know s-controls and javascript well enough to specify how. Basically, on clicking the save button, present a dialogue box with a warning and "OK" and "Cancel" buttons. If the user clicks OK, perform the operation; if not don't. You'd need a method to call from the javascript to check for duplicates; depending on the response you'd present the dialogue box or not. You'd need to then handle the users decision and save/not save accordingly.

 

Hope that helps.

 

 

hisrinuhisrinu

Hi Garyb,

 

 Thanks a lot for your big explanation.

 

  I got it what are you trying to say, no need to post the code. Thanks for your reply.