function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Denise CrosbyDenise Crosby 

custom controller save method problem saving 2 objects

Hi. I created a custom controller to save both an event and a contact. Here's the code. I have an error at line 20 after entering the last name for the contact. The error states the last name is required. I am lost.
 
<apex:page controller="NewEventwithContactController" >
<apex:form >
<apex:slds >
        
<div class="slds-scope"> 
<apex:pageBlock title="New Meeting">
<apex:pageMessages />
<apex:pageBlockSection id="eventInformation">
    <apex:inputfield value="{!ev.description}"/> 
    <apex:inputfield value="{!ev.WhatID}"  />
	<apex:inputField value="{!Newcontact.firstName}"  />
    <apex:inputField value="{!Newcontact.lastName}"  />
    <apex:inputField value="{!Newcontact.email}" />     
</apex:pageBlockSection>
        
<apex:pageBlockbuttons >
   <apex:commandbutton value="Save" action="{!save}" immediate="true"/>  
</apex:pageBlockbuttons>    
    
</apex:pageBlock>
    
</div>
</apex:slds>       
</apex:form>
</apex:page>


public class NewEventwithContactController {

public event ev { get; set; }
   
public contact NewContact { get; set; }
    
public NewEventwithContactController (){
    Newcontact = new Contact();
    ev = new event();
    ev.CurrencyIsoCode = UserInfo.getDefaultCurrency();
    ev.OwnerId = UserInfo.getUserId();
    ev.StartDateTime = Datetime.now().addMinutes(-Datetime.now().minute());
    ev.EndDateTime = Datetime.now().addMinutes(60-Datetime.now().minute());  
    ev.Subject = 'New Meeting';
}
    
public PageReference save() {
    if (ev.WhatID != null)
      	{ NewContact.AccountId = ev.WhatID; }
      insert NewContact;
      ev.WhoId = NewContact.Id;
    return new ApexPages.StandardController(ev).save();
  }  
   
}

 
Best Answer chosen by Denise Crosby
Alain CabonAlain Cabon
Hello Denise,

It is just immediate="true" here for the "Save".

Immediate - A Boolean value that specifies whether the action associated with this component should happen immediately, without processing any validation rules associated with the fields on the page. If set to true, the action happens immediately and validation rules are skipped. If not specified, this value defaults to false.

The documentation talks just about "validation rules" but here, the complete value of "last name" disappears even if you type it (?).

I had been testing a solution for your other question. The main problem is the refresh of the picklist. The solution will use <apex:actionregion> but that doesn't work as expected all the time like immediate=true here (why does the value disappear even it is not the validation rule?).

Regards

All Answers

Alain CabonAlain Cabon
Hello Denise,

It is just immediate="true" here for the "Save".

Immediate - A Boolean value that specifies whether the action associated with this component should happen immediately, without processing any validation rules associated with the fields on the page. If set to true, the action happens immediately and validation rules are skipped. If not specified, this value defaults to false.

The documentation talks just about "validation rules" but here, the complete value of "last name" disappears even if you type it (?).

I had been testing a solution for your other question. The main problem is the refresh of the picklist. The solution will use <apex:actionregion> but that doesn't work as expected all the time like immediate=true here (why does the value disappear even it is not the validation rule?).

Regards
This was selected as the best answer
Denise CrosbyDenise Crosby
Oh wow. After removing immediate=true, the page starts working. You're a genius. I had tried to simplify my earlier code and just have the contact information as part of the same pageBlockSection so I won't have to refresh any picklists. I'll have to see if it will fly with the big bosses. Thank you so much!