1

Using VisualForce & APEX to submit a new Contact record. Generating a custom form and trying to save the records as a new 'Contact' on my SF instance.

Once I click on 'Save' button below error is generated

enter image description here

VisualForce Page code

<apex:page Controller="ContactController" >
    <apex:form >

        <apex:pageBlock title="New Contact Form">

            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!c.FirstName}"/>
                <apex:inputField value="{!c.LastName}"/>
                <apex:inputField value="{!c.Email}"/>
                <apex:inputField value="{!c.Birthdate}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
          <apex:pageBlockTable value="{!samepage}" var="c">
          <apex:column headerValue="First Name">
          <apex:outputField value="{!c.Firstname}"/>
          </apex:column>

          <apex:column headerValue="Last Name">
          <apex:outputField value="{!c.Lastname}"/>
          </apex:column>

          <apex:column headerValue="Birthdate">
          <apex:outputField value="{!c.Birthdate}"/>
          </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>
</apex:page>

APEX Controller Code

public with sharing class ContactController {

public Contact c { get; set; }

public List<Contact> samepage { get; set; }

//Constructor
public ContactController(){
   c=new Contact();
}

public PageReference save() {

// Database.DMLOptions dml = new Database.DMLOptions(); // dml.DuplicateRuleHeader.AllowSave = true; Database.SaveResult sro = Database.insert(c, false);

        if(sro.isSuccess()) {  
        // Operation was successful, so get the ID of the record that was processed  
        System.debug('Successfully inserted contact. Contact ID: ' + sro.getId());  
        }  
        else {  
            // Operation failed, so get all errors  
            for(Database.Error err : sro.getErrors()) {  
            System.debug('The following error has occurred.');  
            System.debug(err.getStatusCode() + ': ' + err.getMessage());  
            System.debug('Account fields that affected this error: ' + err.getFields());  
            }  
        }  

    return null;

} }

2 Answers 2

1

Looks like you have a rule set up in your org to disallow duplicate emails. If that is there intentionally, and you want to keep it, here are a couple of options:

  • Improve the error handling in your controller by putting a try/catch around the insert c and using c.addError(ex); to better report the error via an apex:pageMessages in the Visualforce
  • Include more active logic in your controller (i.e. do a query to check) to tell the user they are duplicating an existing email address
3
  • 2
    Database.insert(records, false) avoids the need of try-catch, though, and also presents a nice error on Visualforce pages.
    – sfdcfox
    Commented Dec 21, 2019 at 16:02
  • Didn't know that - worth a separate answer.
    – Keith C
    Commented Dec 21, 2019 at 16:08
  • @sfdcfox tried above its not working. Its neither inserting new contact nor displaying any error.
    – Vikram
    Commented Dec 22, 2019 at 17:40
0
    public PageReference save() {
       Database.DMLOptions dml = new Database.DMLOptions(); 
       dml.DuplicateRuleHeader.AllowSave = true; 
       Database.SaveResult sro = Database.insert(c, dml);
       if(sro.isSuccess()) {  
        System.debug('Successfully inserted contact. Contact ID: ' + sro.getId());  
       }  
       else {  
            for(Database.Error err : sro.getErrors()) {  
              System.debug('The following error has occurred.');  
              System.debug(err.getStatusCode() + ': ' + err.getMessage());  
              System.debug('Account fields that affected this error: '+err.getFields());  
            }  
       }      
       return null;
    }

I've replaced false with dml in your save method

2
  • tried above code, it's not inserting neither any error message. Please check updated controller code in my question.
    – Vikram
    Commented Dec 22, 2019 at 17:37
  • Replace false with dml(in insert statement)
    – user61140
    Commented Dec 22, 2019 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .