+ Start a Discussion
cduncombe44cduncombe44 

commandbutton JS issues

Please help.  I have searched these forums for days and am still unable to fix my issue.

 

 

I have a VF page, that calls a popup VF page where a new contact is created.  On that pop up page, I have a commandButton, that calls a custom save that performs some logic and inserts the new contact.  That command button also has a JS function called in the 'oncomplete'.  The function simply has an alert, and then closes the window  

 

The issue that I am having is I can't get the JS function to get the right value of the property that is set in my save method.  So it is displaying the alert and closing the window, even when there are errors on the page.  The page messages are appearing as they should, the alert just comes anyway.  I am setting a controller property in the save method to 1 if errors are found.  But the JS function is only getting the 'old' value of the property.  I assume this is because it is grabbing the property before the savemethod is executed, but I can;t figure out how to solve this issue

 

CommandButton

<apex:commandButton action="{!createContact}" oncomplete="CloseWindow();" id="saveButton" value="Create New Contact" rerender="page"/> 

 JS function 

<script>
function CloseWindow()
{		var x = '{!hasError}';
		if(x != 1) {
			alert('New Contact Created');							
	    	window.top.close();
	    	
		}
		else {
			alert('Some alert to fix errors');
		}
		
}
</script>

 

controller

public integer hasError{get;set;}

public PageReference createContact() {
    	hasError = 0;    	
    	
    	if(newCon.FirstName == null || newCon.FirstName == '') {
    		hasError = 1;
    	}
    	if(newCon.LastName == null || newCon.LastName == '') {
    		hasError = 1;
    	}
    	if(newCon.Relationship_to_Subscriber__c == null || newCon.Relationship_to_Subscriber__c == '') {
    		hasError = 1;
    	}
    	
    	try{		  	
	     insert newCon;		  	
		  	
        }Catch (DMLException e) {
		   
        }  
}

 



 

Ideally, what I would like is if there are errors (any of those 3 fields checked in the controller are blank) the normal pagemessages appear ad the user fixes them.  If there arent errors, I would like to have an alert it was sucessful, close the window, and refresh the parent page.  I would also appreciate some help with the proper way to refresh the parent page.

 

Any help would be greatly appreciated.

 

Thanks,

Chris

 

Abhay AroraAbhay Arora

Check below i think it will solve your problem

 

var zip = document.getElementById(“{!$Component.zipcode}”);
alert(‘zip:’+zip);

cduncombe44cduncombe44

Thank you for the reply, unfortunately its still not working.  See the changes I made below

 

 

I added a hidden input that gets its value from the controller property

<apex:inputhidden id="hiddenError" value="{!hasError}"/>

 

Changed my JS function as follows

<script>
function CloseWindow()
{		
		var x = document.getElementById('{!$Component.hiddenError}');        
		if(x != 1) {
			alert('Should be no errors, error variable: '+ x);							
	    	window.top.close();	    	
		}
		else {
			alert('Should be errors, error variable: '+ x);
		}		
}
</script>

   Whether I click the button with or without errors, it is making it into the IF statement and saying the var is null, and closing the window.  So I dont think the getelementbyId is working correctly, or I am doing something wrong with it. 

 

I really just need some solution where I can tell if there are errors or not from within my JS function.  Any help is greatly appreciated.  Thanks

 

-Chris