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
tdeptdep 

Input to a field with Unique (Dupe checking) enabled

Hi,

 

I currently have a small form that a user can input 4 fields for a new entry into the database. Everything works fine when I remove the "Unique" check for dupes on the Email field. But if I have it enabled, it jumps to the auth required screen.

 

As you can see below, the class is very simple and functions properly if a user inputs a new entry that does not exist. How can I check against unique at the insert? Or should I be doing a field validation rule? Not sure which is the best path to take.

 

public class addbrokerclass 
{
	public Broker_Contact__c newBroker { get; set; }
		
	public addbrokerclass() 
	{ 
	newBroker = new Broker_Contact__c();
	}
	
	public PageReference add() {
		insert newBroker;
		return new PageReference('/survey/landing');
                            }
}

 Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
sfcksfck

This might work:

public PageReference add() {
    try {
        insert newBroker;
        return new PageReference('/survey/landing');
    }
    catch (Exception e){
        return null;
    }
}

 It checks for whether the system has been able to insert the record, and if it is not able (e.g. because it is a duplicate) it will stay on the same page instead of trying to move you on.

 

Try putting this somewhere on your visualforce page as well. It should show you the error message

<apex:pagemessages />

 

All Answers

sfcksfck

This might work:

public PageReference add() {
    try {
        insert newBroker;
        return new PageReference('/survey/landing');
    }
    catch (Exception e){
        return null;
    }
}

 It checks for whether the system has been able to insert the record, and if it is not able (e.g. because it is a duplicate) it will stay on the same page instead of trying to move you on.

 

Try putting this somewhere on your visualforce page as well. It should show you the error message

<apex:pagemessages />

 

This was selected as the best answer
tdeptdep

A catch ended up working.

 

To demo it, I had to remove the Unique on Email :)