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;

} }

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
| improve this answer | |
  • 2
    Database.insert(records, false) avoids the need of try-catch, though, and also presents a nice error on Visualforce pages. – sfdcfox Dec 21 '19 at 16:02
  • Didn't know that - worth a separate answer. – Keith C Dec 21 '19 at 16:08
  • @sfdcfox tried above its not working. Its neither inserting new contact nor displaying any error. – Vikram Dec 22 '19 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.