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
Rohit ShisodeRohit Shisode 

input field values getting lost when calling controller method from action function that is called from js

I have contact fields that i need to validate on client side and if everything is ok then insert that contact. But when i go into js for validation and call controller method from js using action fucntion all my inputField values are null. Contact does not get values that i have written, Please help!
VF:
<apex:pageBlock rendered="{!IsPkg}" id="contactPageBlock">
            <apex:pageBlockSection title="Guest Registration" columns="1" collapsible="false" id="contactSection">
                <apex:inputField value="{!contactObj.FirstName}" label="First Name" id="fname"/>
                <apex:inputField value="{!contactObj.LastName}" label="Last Name" id="lname"/>
                <apex:inputField value="{!contactObj.Email}" label="Email" id="cmail"/>
            </apex:pageBlockSection>
                <apex:commandButton onclick="validate();return false;" value="Save"/>
                <apex:actionfunction name="callfromJS" action="{!saveContact}" immediate="true"/>
        </apex:pageBlock>

Js
function validate(){
            try{
                var fNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.fname}");
                var lNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.lname}");
                var eNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.cmail}");
                console.log(fNameObj.value);
                console.log(lNameObj.value);
                console.log(eNameObj.value);
                if(trim(fNameObj.value) == "" || trim(lNameObj.value) == "" || trim(eNameObj.value) == ""){
                    console.log("Inside If");
                    window.alert("All fields are mandatory!");
                    return false;
                } else if(trim(eNameObj.value) != ""){
                    var x = eNameObj.value;
                    var atpos = x.indexOf("@");
                    var dotpos = x.lastIndexOf(".");
                    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
                        window.alert("Not a valid e-mail address");
                        return false;
                    }else {
	                    var r = confirm("Register a new guest?");
	                    if(r==true){
	                        callfromJS();
	                        return true;
	                    }else{
	                       return false;
	                    }
	                }
	            }
            } catch(e){
                console.log("Inside Catch");
            }
        }
    </script>

​Controller method
public void saveContact(){
        System.debug('Inside Save');
        System.debug('Contact Constructed is::'+contactObj);
        System.debug('selectedEventId:::'+selectedEventId);
        System.debug('selectedPackageId:::'+selectedPackageId);
        if(String.isNotBlank(selectedPackageId)){
            System.debug('Contact Constructed is::'+contactObj);
            if(contactObj.LastName != null){
                System.debug('Contact Constructed is::'+contactObj);
                contactObj.Package__c = selectedPackageId;
                try{
                    System.debug('Contact Constructed is::'+contactObj);
                    //insert contactObj;
                } catch(Exception e){
                    
                }
            }
        }
    }

 
Best Answer chosen by Rohit Shisode
John Pipkin 14John Pipkin 14
Rohit, 

The attribute "immediate" in your actionFunction is set to True. When it is set to True, values are not passed to the controller automatically. Try removing the "immediate" attribute or you will have to use apex:param inside the actionFunction to pass the controller the data. 

Hope that helps

All Answers

Raj VakatiRaj Vakati
Remove immediate="true" attribe and try 
Rohit ShisodeRohit Shisode
I removed it still doesn't work
 
John Pipkin 14John Pipkin 14
Rohit, 

The attribute "immediate" in your actionFunction is set to True. When it is set to True, values are not passed to the controller automatically. Try removing the "immediate" attribute or you will have to use apex:param inside the actionFunction to pass the controller the data. 

Hope that helps
This was selected as the best answer
Rohit ShisodeRohit Shisode
Thank you both for your help. It did work! Thanks