2

Didn't get any error but data isn't getting saved in contact and reservation object after clicking the save button...

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)

Controller:

public class CustomController{

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

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

    public void save(){
        try{
            insert contact;
            reservation.Customer_Name__c = contact.Id; 
            insert reservation;
        }
        catch(Exception e){
            /** Handle exception **/
        }
    }
}

VisualForce Page:

<apex:page Controller="CustomController" >
 <apex:sectionHeader title="Reservation"/>
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Welcome To ForkWork">
        <apex:pageBlockSection columns="1" > 
            <apex:inputField value="{!contact.salutation}" />
            <apex:inputField value="{!contact.firstname}" />
            <apex:inputField value="{!contact.lastname}" />
            <apex:inputField value="{!contact.phone}" />
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="2" >
            <apex:inputField value="{!reservation.Booking_Time_Date__c}" required="true"/>
            <apex:inputField value="{!reservation.Table_for__c}" />
        </apex:pageBlockSection>
         <apex:pageBlockButtons location="bottom">
                            <apex:commandButton action="{!save}" value="Save"/>
         </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
6
  • Replace /** Handle exception / with ApexPages.addMessages(e); / Handle exception **/ is just comment. Then you should see exception Feb 16, 2014 at 20:08
  • Hey birthus... I did dat but when i fill my field in vf page and click the save button...i get a error message at top that : (attempt to de-reference a null object)...and data doesn't get save in respective object.
    – Mayukh
    Feb 17, 2014 at 10:29
  • But you have your error :) Now you should use System.debug() in your code and check what is null. I think it is Contact, comment this part "reservation.Customer_Name__c = contact.Id; insert reservation;" and check what message you will get Feb 17, 2014 at 10:56
  • Yea done that now i am not getting any error message but page just refeshed without saving any record in both reservation and contact object after clicking save button.
    – Mayukh
    Feb 17, 2014 at 11:22
  • Please try to only use tags very specific to your question subject. This will help attract users with the right knowledge. I've already updated to question for you, but please remember :) Feb 17, 2014 at 13:50

2 Answers 2

3

It's almost certainly your exception handling that is hiding/obscuring the issue. As a test, try commenting out the exception handling to see if the actual exception is displayed.

Within your exception handling block you could also log the exception to make it easier to track.

E.g.

try {
    //...
} catch (Exception ex) {
    System.debug(LoggingLevel.Error, 'CustomController.save() Exception: ' + ex);
}
2
  • Hey i have done that but still getting error attempt to de-reference a null object.
    – Mayukh
    Feb 17, 2014 at 10:30
  • Mayukh, enable debug logs for whichever user you are testing the page as (Setup->Monitoring->Debug Logs). When you hit the visualforce page, you should see your null pointer exception in one of the logs, and it should tell you the exact line the exception is encountered. Feb 17, 2014 at 14:18
0

Try

public class CustomController{

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

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

public PageReference  save(){
    try{
        insert contact;
        //reservation.Customer_Name__c = contact.Id; 
        //insert reservation;
    }
    catch(Exception e){
       System.debug(LoggingLevel.Error, 'CustomController.save() Exception: ' + e);
    }
    return new PageReference('/' + contact.Id);
}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .