0

Working on a VF page where I am inserting a Contact record. I have created a extension controller to insert a custom object record after this Save action completes. But I am not able to get this action. Here is my code:

<apex:page standardController="Contact" extensions="ContactCreationCtrl">
<apex:form >

    <apex:pageBlock title="Create Contact">

        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Contact.FirstName}"/>
            <apex:inputField value="{!Contact.LastName}"/>
            <apex:inputField value="{!Contact.Email}"/>
            <apex:inputField value="{!Contact.Birthdate}"/>
            <apex:inputField value="{!Contact.AccountId}" required="true"/>
        </apex:pageBlockSection>

        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" oncomplete="createSampleObjRecord()" value="Save"/>
        </apex:pageBlockButtons>

    </apex:pageBlock>

</apex:form>

Controller:

public class ContactCreationCtrl {
public Contact newCon {get;set;}

public ContactCreationCtrl(ApexPages.StandardController con){
    newCon = (Contact)con.getRecord();
}

public void createSampleObjRecord(){

    Contact currentCon = [Select FirstName, AccountId From Contact Where Id =: newCon.Id];
    //Insert New record
  }
}

The custom controller's method is never getting executed. Where am I doing wrong?

Thanks

0

You can insert the contact in extension itself without using the standard save functionality. i.e. by directly calling your method from commandButton. For this, you will have to make below changes

Visualforce page : Call your method directly from save button

<apex:commandButton action="{!createSampleObjRecord}" value="Save"/>

Controller : Insert contact in your custom method

public void createSampleObjRecord(){
    insert newCon;
    Contact currentCon = [Select FirstName, AccountId From Contact Where Id =: newCon.Id];
    //Insert New record
}
  • I have tried actionFuncion approach, but that doesn't seemed to be working. – Austin Evans Feb 20 at 6:13
  • NVM, I have tried the 2nd approach and it worked. Thanks – Austin Evans Feb 20 at 6:19
  • That's great ! Updated the answer i.e. removed first approach as actionFunction doesn't seem to work as expected when using standard save action. – Vijay Ganji Feb 20 at 7:26
-1

The oncomplete attribute is trying to call a nonexistent JavaScript function. In your Apex controller, you'll need to implement the save method and call your createSampleObjRecord method from there

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.