0

I'm creating a VF page with a list of all contacts on the top and a form to insert a new one on the bottom but the last feature doesn't work. This is the code of the VF page:

<apex:page standardController="Contact" extensions="ContactControllerExtension" recordSetVar="contacts" lightningStylesheets="true">
<apex:pageBlock >
    <apex:form >       
        <div style="margin-bottom: 15px;">
            <span>Find contact: </span>
            <apex:inputText />
            <apex:commandButton value="Search" action="{!}"/>
        </div>
        <div>
            <apex:pageBlockTable title="Contatti" value="{!contacts}" var="contact">
                <apex:column value="{!contact.FirstName}"/>
                <apex:column value="{!contact.LastName}"/>
                <apex:column > 
                    <apex:commandLink action="{!openRecordPage}" target="_blank">
                        View details
                        <apex:param assignTo="{!contactID}" name="id" value="{!contact.id}"/>
                    </apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>
        </div>
        <div style="margin-top: 15px; margin-bottom: 15px;">
            <apex:commandButton value="Previous" action="{!previous}" rendered="{!IF(hasPrevious, true, false)}"/>
            <apex:commandButton value="Next" action="{!next}" rendered="{!IF(hasNext, true, false)}"/>
        </div>
    </apex:form>
    <apex:form>
        <apex:pageBlockSection title="Insert new contact" columns="1">
            <apex:pageMessages></apex:pageMessages>
            <apex:inputField value="{!contact.FirstName}"/>
            <apex:inputField value="{!contact.LastName}"/>
            <apex:inputField value="{!contact.Phone}"/>     
            <apex:inputField value="{!contact.Email}"/>
            <apex:commandButton value="Insert" action="{!save}"/>
        </apex:pageBlockSection>
    </apex:form>
</apex:pageBlock>

When I put values in the input fields and press 'Insert' there's a redirect to the Lightning Experience, but the record is not commited. No message error from <apex:pageMessages>. I'm using an a controller extension but I didn't override the save method. What's the problem?

  • Adding your controller code will help the community to see what is being called and executed. – manjit5190 Apr 5 at 8:41
0

I solved the problem overriding the standard save method in my controller extension.

Visualforce code:

<apex:page standardController="Contact" extensions="ContactControllerExtension" recordSetVar="contacts" lightningStylesheets="true">
<apex:pageBlock >
    <apex:form >       
        <div style="margin-bottom: 15px;">
            <span>Find contact: </span>
            <apex:inputText />
            <apex:commandButton value="Search" action="{!}"/>
        </div>
        <div>
            <apex:pageBlockTable title="Contatti" value="{!contacts}" var="contact">
                <apex:column value="{!contact.FirstName}"/>
                <apex:column value="{!contact.LastName}"/>
                <apex:column > 
                    <apex:commandLink action="{!openRecordPage}" target="_blank">
                        View details
                        <apex:param assignTo="{!contactID}" name="id" value="{!contact.id}"/>
                    </apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>
        </div>
        <div style="margin-top: 15px; margin-bottom: 15px;">
            <apex:commandButton value="Previous" action="{!previous}" rendered="{!IF(hasPrevious, true, false)}"/>
            <apex:commandButton value="Next" action="{!next}" rendered="{!IF(hasNext, true, false)}"/>
        </div>
        <apex:pageBlockSection title="Insert new contact" columns="1">
            <apex:pageMessages></apex:pageMessages>
            <span>First Name: <apex:inputText value="{!newContact.firstName}"/></span>
            <span>Last Name: <apex:inputText value="{!newContact.lastName}"/></span>
            <span>Phone: <apex:inputText value="{!newContact.phone}"/></span>
            <span>Email: <apex:inputText value="{!newContact.email}"/></span>             
            <apex:commandButton value="Insert" action="{!save}"/>
        </apex:pageBlockSection>
    </apex:form>
</apex:pageBlock>

Extension code:

public class ContactControllerExtension {
    List<Contact> cont;
    Id contactID;

    public String firstName {get; set;}
    public String lastName {get; set;}
    public String phone {get; set;}
    public String email {get; set;}        

    public ContactControllerExtension(ApexPages.StandardSetController stdController) {
        cont = (List<Contact>)stdController.getRecords();
    }

    public String getContactID() {
        return contactID;
    }

    public void setContactID(Id elementId) {
        contactID = elementId;
    }

    public PageReference openRecordPage() {
        PageReference recordPage = new PageReference('/lightning/r/Contact/' + contactID + '/view');
        recordPage.setRedirect(true);
        return recordPage;
    }

    public PageReference save() {
        Contact newContact = new Contact(FirstName = firstName, LastName = lastName, Phone = phone, Email = email);

        try {
            insert newContact;
        } catch (Exception e) {
            System.debug('Exception occurred: ' + e);
        }

        return null;
    }
}
| improve this answer | |

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.