2

This Visualforce page is setup to edit some fields from the users Contact record but it is not saving any changes and throwing error Required Field Missing [Last Name]

<apex:page standardController="Contact" extensions="MyAccountExtension" sidebar="false" showHeader="false" standardStylesheets="false"
           applyHtmlTag="false" applyBodyTag="false" docType="html-5.0">
    <apex:stylesheet value="{!$Resource.StyleMain}"/>
<html>
<head>
    <meta charset="UTF-8"/>
<title>Tegile</title>  
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />    
    <link rel="shortcut icon" href="{!$Resource.testFavicon}" type="image/x-icon"/>
    <link rel="icon" href="{!$Resource.testFavicon}" type="image/x-icon"/>
</head>

<body>

<div class="container">
  <header>
    <apex:image url="{!$Resource.Logo}" styleClass="logo" alt="logo" width="200" height="67" id="logo" />      
<ul>
  <li><a href="{!$Page.MyCases}">My Cases</a></li>
  <li><a href="{!$Page.MyAssets}">My Assets</a></li>
  <li><a href="{!$Page.MyAccount}">My Account</a></li>
  <li><a href="{!$Page.Knowledge}">Knowledge</a></li>
  <li><a href="{!$Site.Prefix}/secur/logout.jsp">Logout</a></li>
</ul>
  </header>

<apex:form >    
    <div class="lineruleAssets">My Account
    <apex:commandButton style="margin-left: 14px;" styleClass="btn" action="{!URLFOR($Page.NewCase)}" value="Open a New Case"/>
    </div>
</apex:form>  

  <article class="content">
<section>

    <apex:form>

        <table>
            <tr>
                <td class="tdtop"><span class="formLabels">Name</span></td>
                <td><apex:inputField label="" value="{!MyContact.Firstname}"/>
                <apex:inputField label="" value="{!MyContact.Lastname}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Email</span></td>
                <td><apex:inputField label="" value="{!MyContact.Email}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Phone</span></td>
                <td><apex:inputField label="" value="{!MyContact.Phone}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Mobile Phone</span></td>
                <td><apex:inputField label="" value="{!MyContact.MobilePhone}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Other Phone</span></td>
                <td><apex:inputField label="" value="{!MyContact.OtherPhone}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Contact Preference</span></td>
                <td><apex:inputField label="" value="{!MyContact.Contact_Preference__c}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Communication Preference</span></td>
                <td><apex:inputField label="" value="{!MyContact.Communication_Preferences__c}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Contact Time Zone</span></td>
                <td><apex:inputField label="" value="{!MyContact.Contact_Time_Zone__c}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Work Schedule</span></td>
                <td><apex:inputField label="" value="{!MyContact.Work_Schedule__c}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Work Start Time</span></td>
                <td><apex:inputField label="" value="{!MyContact.Start_Time__c}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"><span class="formLabels">Work Stop Time</span></td>
                <td><apex:inputField label="" value="{!MyContact.Stop_Time__c}"/><br/><br/></td>
            </tr>
            <tr>
                <td class="tdtop"></td>
                <td>

                    <apex:commandButton style="margin-left: 14px;" action="{!saveContact}" styleClass="btn" value="Update Contact Details"/>
                    <apex:commandButton styleClass="btn" action="{!URLFOR($Page.MyAccount)}" value="Cancel"/>
                </td>
            </tr>
        </table>  

</apex:form>


</section>


    <!-- end .content --></article>
  <footer>

    <ul>
      <li><a href="#">Privacy Policy</a></li>
      <li><a href="#">Terms of use</a></li> 
      <li>Copyright © 2015 Xyz, Inc., All Rights Reserved</li>
    </ul>

  </footer>
  <!-- end .container -->
    </div>
    </body>
</html>
</apex:page>

I am using this Controller Extension

public class myAccountExtension {
    public Contact webcontact {get;set;}

    public myAccountExtension(ApexPages.StandardController stdController) { 

    webcontact = (Contact) stdController.getRecord();
    }

    public Contact getMyContact() {
        User[] users = [SELECT ContactId FROM User WHERE Id = :UserInfo.getUserId()];
        User usr = users[0];

        Contact[] contacts = [SELECT Name, Firstname, Lastname, Email, Phone, MobilePhone, OtherPhone,
                              Contact_Preference__c, Communication_Preferences__c,
                              Contact_Time_Zone__c, Work_Schedule__c, Start_Time__c, Stop_Time__c
                              FROM Contact WHERE Id = :usr.ContactId];
            if (contacts.isEmpty()) {
            return Null;
            // handle when contacts is empty
            } else {
                Contact MyContact = contacts[0];

            return MyContact;       
        }
    }
        public PageReference saveContact() {
        try {
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.useDefaultRule = true;
            webcontact.setOptions(dmlOpts);
            upsert(webcontact);
        } catch (System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        PageReference p = Page.MyAccount;
        p.setRedirect(true);
        return p;
    }
}
2

It looks to me like you have a binding issue

  1. Your controller constructor assigns to webContact whatever Contact the page is bound to (whatever contact Id is supplied to the page, if any). It is not clear to me how the page is initially displayed and from where. I suspect webContact is simply an empty Contact SObject.
  2. Your VF form is bound to a different object, the result of a query to Contact based on the running user
  3. But your saveContact() method tries to update the value of webContact - which was never updated by any of the inputFields. Hence required field lastName is missing.
3
  • Currently this page does not get a ContactId passed to it but I am going to try it with an Id being in the URL when the user comes to the page. The User is getting to this page from a Contact Detail page showing them their contact info so I can pass the Id when they click on Edit Details. I think based on what you are saying this could solve my problem. I will update after trying this
    – Jason
    May 9 '15 at 2:14
  • OK just passing the Id with the URL to the page did allow the page to submit when i click on the button to update the contact details and it goes through but doesn't actually update the contact. I will have to look at this tomorrow when I have more time.
    – Jason
    May 9 '15 at 2:35
  • I made some changes based on this answer that solved my problem. I changed the Contact details page to send the Contact id as a parameter in the url when user chooses to edit the details and removed the SQL query from the Controller extension completely on the edit Contact page this eliminated the binding issue I believe
    – Jason
    May 11 '15 at 12:27
1

You should have <apex:inputfield value='{!MyContact.LastName}'/> somewhere on your VF page. LastName is hardcoded required by Salesforce, so if you try to do anything like insert a contact without a last name, it will fail no matter what you do.

Name is a compound field comprised of the First and Last Name. Whenever you're doing anything revolving around manipulating or creating contacts, you'll want to use the FirstName and LastName fields.

Here's some info on Compound fields: https://www.salesforce.com/developer/docs/api/Content/compound_fields_limitations.htm

The API documentation has a better list of fields that you can reference than the base UI does. You'll likely run in to something VERY similar to this if you start doing anything with Addresses as well.

https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_contact.htm

4
  • I now am using the <apex:inputfield value='{!MyContact.LastName}'/> and still getting required field missing. I modified the original question to show using the new lastname format. I have tried putting system.debug(Contact.Lastname); on the line before the upsert in the Controller Extension and the output I get in the log is just USER_DEBUG [31]|DEBUG|LastName
    – Jason
    May 8 '15 at 21:19
  • System.debug(Contact.LastName) refers to the Schema rather than your actual contact you're updating. Put System.debug(MyContact.LastName);. Are you putting a value into the LastName field? May 8 '15 at 22:05
  • when I put System.debug(MyContact.LastName); I get the error in dev console of Variable does not exist: MyContact.Lastname yes there is a value already in the Lastname field and there is one still in it when I submit the edit request.
    – Jason
    May 8 '15 at 22:10
  • so i tried changing the debug to System.debug(webcontact.LastName); since my upsert is upsert(webcontact); and the debug result is USER_DEBUG [31]|DEBUG|null No idea if this would be a correct way to use the webcontact variable
    – Jason
    May 8 '15 at 22:16
0

Are you sure it's not erroring out during the save? You shoudl add an tag to your VF page to display any errors that might be happening.

Also can you paste the debug log for when the saveContact() method is being called?

3
  • I added apex:messages and the error it is showing on the page is Required fields are missing [Last Name] When I look at the name field for Contacts under Customize:Contacts:Fields this is a type of field I have not seen before. I am not sure how to reference lastname in the VF expression. The existing value does show a full name on the page but the name field is not editable like the other fields. I am researching field sets
    – Jason
    May 8 '15 at 19:55
  • I thought it might be a field set but it does not appear that is what it is so still digging
    – Jason
    May 8 '15 at 20:18
  • LastName is the API name. It is a standard field on several standard sObjects such as Lead and Contact. the "Name" field is actually a combination of the FirstName and LastName fields.
    – dBeltowski
    May 11 '15 at 13:28

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.