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
James GoldingJames Golding 

visualforce page does not assign record attributes

So basically i have a page that is going through case creation, if the contact doesn't exist it, there is a button that shows the user a contact creation section.
When I press save I get errors that make no sense.

Visualforce
<apex:page Controller="CaseProcess" lightningStylesheets="true" >
    <apex:form >
        <apex:pageBlock Title ="Case Creation">
            <apex:pageMessages></apex:pageMessages>
            <apex:pageBlockSection title="Case Setup" id="caseSetup">
                <apex:inputField value="{!newCase.Subject}" required="true"/>
                <apex:inputField value="{!newCase.Origin}" required="true"/>
                <apex:inputField value="{!newCase.AccountId}" required="true"/>
                <apex:inputField value="{!newCase.RecordTypeId}" required="true"/>
                <apex:inputField value="{!newCase.ContactId}" required="true"/>
                <apex:inputField value="{!newCase.EntitlementId}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection>
                <apex:commandButton value ="New Contact" action = "{!showCreateContact}" rendered = "{!notWizardContactSection}" immediate="true" html-formnovalidate="formnovalidate" style="align-content: center">
                	<apex:actionSupport reRender="contactCreation" />
            	</apex:commandButton>
            </apex:pageBlockSection> 
            <apex:commandbutton value="Save Case" action="{!saveCase}"/> 
    		<apex:commandbutton value="Cancel" action="{!cancelCase}" immediate="true" html-formnovalidate="formnovalidate"/>
        </apex:pageBlock> 
        <apex:pageBlock id="contactCreation" rendered="{!wizardContactSection}" title="Contact Creation">
            <!-- create contact section -->   
            <apex:pageBlockSection >
                <apex:inputField value="{!newContact.Name}"/>
            	<apex:inputField value="{!newContact.FirstName}" />
                <apex:inputField value="{!newContact.LastName}" id="lastName" />
                <apex:inputField value="{!newContact.AccountId}" id="accountId"/>
                <apex:inputField value="{!newContact.Email}" />
                <apex:inputField value="{!newContact.MobilePhone}" />
            </apex:pageBlockSection>  
            <apex:pageBlockSection>
                <apex:commandButton value ="Save Contact" action = "{!saveContact}" immediate="true" html-formnovalidate="formnovalidate">
                    <apex:actionSupport reRender="contactCreation" />
                </apex:commandButton>
            </apex:pageBlockSection>
            <!-- buttons section -->
        </apex:pageBlock>
    </apex:form>
</apex:page>
Controller
 
public class CaseProcess {

    public Contact newContact{get; set;}
    public Case newCase{get; set;}

    
    // assign variables for the different sections of the form
    public boolean wizardContactSection{get; set;}
    public boolean notWizardContactSection{get; set;}
    
    public CaseProcess(){
        // assign variables for hidding and showing sections of the form
        newCase = new Case();
        newContact = new Contact();
        wizardContactSection = false;
        notWizardContactSection = true;
    }
    public pagereference cancelCase(){
        return new pagereference('/lightning/o/Case/list');
    }
    public pagereference saveCase(){
        newCase.Status = 'New';
        insert newCase;
        return new pagereference('/lightning/o/Case/list');
    }
    
    public void showCreateContact(){
        wizardContactSection = true;
        notWizardContactSection = false;
        newContact.AccountId = newCase.AccountId;
    }
    public void saveContact(){
        /*
        if (newContact.AccountId == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Please add an account to the contact.'));
            return;
        }*/
        try{
            insert newContact;    
        	newCase.ContactId = newContact.Id;
        	wizardContactSection = false;
        }
        catch(DmlException ex){
        	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getDmlMessage(0)));   
        }
    }

}

And error message
User-added image
I can't work out why, I have tried moving all the contact related stuff into an extention but that doesn't work either 

 
Best Answer chosen by James Golding
Ashish Singh SFDCAshish Singh SFDC
Hi James,

This is happening because immediate="true" doesn't allow inputField to bypass to the controller. You may have to write JS for validation for making field as requred.

You can try removing immediate="true" from saveContact commandbutton and it should not throw any error.

Best Regards,
Ashish Singh.

All Answers

SUCHARITA MONDALSUCHARITA MONDAL
Hi James,
For Contact creation, LastName is required field.  While creating Contact, you are passing only AccountId, add LastName also.
I think it'll resolve the issue.

Please share your thoughts!

Thanks,
Sucharita
Ashish Singh SFDCAshish Singh SFDC
Hi James,

This is happening because immediate="true" doesn't allow inputField to bypass to the controller. You may have to write JS for validation for making field as requred.

You can try removing immediate="true" from saveContact commandbutton and it should not throw any error.

Best Regards,
Ashish Singh.
This was selected as the best answer
James GoldingJames Golding
SUCHARITA MONDAL you've complete missed the point here. 

Ashish Singh thank you very much, did not know immediate had also did this. It's confusing because it was showing parts of the visualforce page that could only be seen after the code was run. I was assuming it was to do with how I was declairing the object.