0

I am having two object reservation and contact are in look-up relation. I want to insert the page details as attached in the image in their respective objects with a particular contact record and particular Reservation record created after save button clicked from VF page. VF page contain following fields: FirstName LastName Phone (Contact object) Booking Time/Date(Required field in Reservation field) Customer_Name__c (as Look-up) I have attached the image of my Visualforce page. Please Suggest me the visualforce and controller code to complete my task.I am new to salesforce and unable to proceed with coding using VF and apex code.

  • Let's sum this up : pleas say more about this relation - which is master? – Artur Kępczyński Feb 15 '14 at 20:39
  • contact is parent and reservation object is child. – Mayukh Feb 15 '14 at 21:02
1

So what you have to do is create custom controller, create VF page with custom controller. You have to create those objects in controller constructor, then display in page required fields. Last thing is creating custom save button calling save action from controller

Class CustomController{

public Contact contact {get; set;}
public Reservation__c reservation {get; set;}

public void CustomController(){
    contact = new Contact();
    reservation = new Reservation();
}

public void save(){
    try{
        /** fill in contact required fields other than those in the page **/
        insert contact;

        /** fill in reservation required fields other than those in the page **/
        reservation.Customer_Name__c = contact.Id; // Fill in Cintact lookup
        insert reservation
    }
    catch(Exception e){
        /** Handle exception **/
    }
}

public PageReference cancel(){
    return new PageReference(/** Some Page reference **/);
}

Bit of page

<apex:page Controller="CustomController" >
 <apex:sectionHeader title="Title" subtitle="Some Subtitle"/>
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Title">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >

            <apex:inputField value="{!contact.Name}" required="true"/>
            /** use apex:inputField to display all fields you wanted  **/
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I hope this will help you. In case of any trouble - please let me know - I will try to help you

| improve this answer | |
  • @birthus....In save method.... i haven't understood by your term fill in as my required field in contact is lastname and in reservation is billing_datetime. My lookup field is Customer_Name__c. And in the line reservation.Contact__c=contact.ID in save method.How come we refer Contact__c as it is a standard object or you referring Contact__c as my lookup field api name? – Mayukh Feb 15 '14 at 21:36
  • I added this comment in case you have some required fields on Contact or Reservation which you don't want show in page, in such case you have to fill them in to sucessfuly insert objects. I used Contact__c because that is usually name of lookup field to Contact object. Is everything clear now? – Artur Kępczyński Feb 16 '14 at 5:45
  • yea..its clear as i have to show the require field in the page... but this is code is for controller.what about the vf page code.can u help me out? – Mayukh Feb 16 '14 at 11:37
  • and as my reservation object is custom object...i should declare it as public Reservation__c reservation{get; set;}; or just public Reservation reservation{get; set;}; – Mayukh Feb 16 '14 at 12:01
  • Of course you have to use API name - I miss '__c' - I probably was just tired. I will try provide some code for you – Artur Kępczyński Feb 16 '14 at 12:12

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.