+ Start a Discussion
Nikki Phillips 8Nikki Phillips 8 

Inputfield onchage does not save record when immediately clicking save

I have a VisualForce page that has Two input fields with onchange actions that if I were type a currency value into a field it will update the other input field and then immediately click save will re-calculate and do the onchange event before the save of the record happens. **Mind you if I click out of the field before immediately clicking save it will calculate and save the record.
My VF page:
 
<apex:page standardController="EventPackageRevenueBreakdown__c"
	extensions="EventPackageRevenueBreakdownExt" standardStylesheets="true"
	tabstyle="EventPackageRevenueBreakdown__c" docType="html-5.0">
<apex:form >
		<apex:sectionHeader title="{!$ObjectType.EventPackageRevenueBreakdown__c.label} {!$Label.Edit}"
			subtitle="{!EventPackageRevenueBreakdown__c.Name}"
			help="{!$Label.NIBaseHelpURL}#cshid= event_package_revenue_breakdown_edit">
		</apex:sectionHeader>
		<apex:pageBlock mode="edit"
			title="{!$ObjectType.EventPackageRevenueBreakdown__c.label} {!$Label.Edit}">
			<apex:pageblockbuttons >
				<apex:commandbutton action="{!save}" value="{!$Label.Package_Save}"></apex:commandbutton>
				<apex:commandbutton action="{!SaveAndNew}"
					value="{!$Label.Package_SaveAndNew}"></apex:commandbutton>
				<apex:commandbutton action="{!cancel}"
					value="{!$Label.Package_Cancel}"></apex:commandbutton>
			</apex:pageblockbuttons>
			<apex:pagemessages ></apex:pagemessages>
			<apex:pageblocksection title="{!$Label.Package_Information}"
				id="block123">
				<apex:actionFunction name="UpdateInclusivePrice"
					action="{!UpdateInclusivePrice}" rerender="block123"></apex:actionFunction>
				<apex:actionFunction name="UpdateUnitPrice"
					action="{!UpdateUnitPrice}" rerender="block123"></apex:actionFunction>
				<apex:actionFunction name="CalculateInclusive"
					action="{!CalculateInclusive}" rerender="block123"></apex:actionFunction>
				<apex:outputpanel layout="block" styleClass="requiredBlock"></apex:outputpanel>
				<apex:inputField required="false" value="{!EventPackageRevenueBreakdown__c.UnitPrice__c}"
					label="{!IF(showInclusivePrices, $Label.ExclPrice, $ObjectType.EventPackageRevenueBreakdown__c.fields.UnitPrice__c.label)}"
					 onchange="UpdateInclusivePrice()" onkeypress="enterPress(event, 'inclusive')">
				</apex:inputField>
				<apex:inputfield required="false"
					value="{!EventPackageRevenueBreakdown__c.Location__c}"></apex:inputfield>
				<apex:pageBlockSectionItem rendered="{!NOT(showInclusivePrices)}">
				</apex:pageBlockSectionItem>
				<apex:pageBlockSectionItem rendered="{!showInclusivePrices}">
					<apex:outputLabel value="{!$Label.InclPrice}"></apex:outputLabel>
					<apex:inputField required="false" styleClass="test" value="{!EventPackageRevenueBreakdown__c.InclusiveUnitPrice__c}"
						onChange="UpdateUnitPrice()" onkeypress="enterPress(event, 'exclusive')"></apex:inputField>
				</apex:pageBlockSectionItem>
				<apex:outputfield value="{!EventPackageRevenueBreakdown__c.BookingPackageEvent__c}" />
			<apex:pageblocksectionitem >
					<apex:outputlabel >{!$ObjectType.EventPackageRevenueBreakdown__c.fields.RevenueClassification__c.label}</apex:outputlabel>
					<apex:outputpanel layout="block" styleClass="requiredInput">
						<apex:outputpanel layout="block" styleClass="requiredBlock"></apex:outputpanel>
						<apex:inputfield value="{!EventPackageRevenueBreakdown__c.RevenueClassification__c}"
							onChange="CalculateInclusive()">
						</apex:inputfield>
					</apex:outputpanel>
				</apex:pageblocksectionitem>
			</apex:pageblocksection>
			<apex:pageblocksection title="{!$Label.AdminChargeAndGratuity}">
				<apex:inputfield required="false"
					value="{!EventPackageRevenueBreakdown__c.AdminCharge__c}"
					onChange="CalculateInclusive()"></apex:inputfield>
				<apex:inputfield required="false"
					value="{!EventPackageRevenueBreakdown__c.Gratuity__c}"
					onChange="CalculateInclusive()"></apex:inputfield>
			</apex:pageblocksection>
			<apex:pageblocksection title="{!$Label.InclusivePrice}"
				rendered="{!showInclusivePrices}" collapsible="true">
				<apex:inputcheckbox value="{!EventPackageRevenueBreakdown__c.AdminIsIncludedInInclusivePrice__c}"
					onChange="CalculateInclusive()"></apex:inputcheckbox>
				<apex:inputcheckbox value="{!EventPackageRevenueBreakdown__c.GratuityIsIncludedInInclusivePrice__c}"
					onChange="CalculateInclusive()"></apex:inputcheckbox>
			</apex:pageblocksection>
		</apex:pageBlock>
		<apex:actionFunction name="saveInclusive" action="{!saveFromEnterInclusive}"></apex:actionFunction>
		<apex:actionFunction name="saveUnit" action="{!saveFromEnterUnit}"></apex:actionFunction>
	</apex:form>
	<script>
	function enterPress(e, fields)
	{
		if(e.keyCode==13)
		{
			if(e.preventDefault)
			{ 
            	e.preventDefault();
            }
            if(fields == 'inclusive')
            {
            	saveInclusive();
           }
           else
           {
           		saveUnit();
           }
        }        
    }</script>
</apex:page>

My standard Controller
 
public with sharing class EventPackageRevenueBreakdownExt {
    public boolean showInclusivePrices { get; set; }
    
    private ApexPages.standardController sController;
    private decimal price;
    private decimal inclusivePrice;
    private Id taxGroupId;
    private EventPackageRevenueBreakdown__c eprb;
    private QueryTaxGroupSchedule qtgs;
    public EventPackageRevenueBreakdownExt(ApexPages.standardController stdController) {

            //Set showInclusivePrices based on feature toggle
            if (Test.isRunningTest()) {
                showInclusivePrices = true;
            } else {
                NiPublic__c niSettings = NiPublic__c.getOrgDefaults();
                if (niSettings != null) {
                    showInclusivePrices = niSettings.EnableInclusivePrices__c;
                } else {
                    showInclusivePrices = false;
                }
                stdController.addFields(new list<string>{'UnitPrice__c'});
            }
            
            sController = stdController;
            eprb = (EventPackageRevenueBreakdown__c) sController.getRecord();
            
            if(eprb.UnitPrice__c == null){ 	
			eprb.UnitPrice__c = 0.00;}
			
			price = eprb.UnitPrice__c;			
			
            if (ApexPages.currentPage().getParameters().get('BookingPackageEventId') != null) {
                eprb.BookingPackageEvent__c = ApexPages.currentPage().getParameters().get('BookingPackageEventId');
            }

            if (eprb.BookingPackageEvent__c != null) {
            	BookingPackageEvent__c bookingPackageEvent = [Select Location__c, BookingEvent__r.TaxGroup__c From BookingPackageEvent__c Where Id = :eprb.BookingPackageEvent__c Limit 1];
                eprb.Location__c = bookingPackageEvent.Location__c;
                taxGroupId = bookingPackageEvent.BookingEvent__r.TaxGroup__c;
                qtgs = new QueryTaxGroupSchedule(taxGroupId, false);          
            }

            if (eprb.Name == null) {
                eprb.Name = '{AUTO}';
            }
           
            CalculateInclusive();           
    }

    public Pagereference SaveAndNew() {
    	
    	if(this.save() == null){
    		return null;
    	}
    	
        PageReference pageRef = page.NewEventPackageRevenueBreakdown;
        pageRef.getParameters().put('BookingPackageEventId', eprb.BookingPackageEvent__c);
        pageRef.setRedirect(true);
        return pageRef;
    }
    
    public pageReference save() {
    	
    	//throw new ni.niException(string.valueOf(eprb.UnitPrice__c));
    	
        // if the recipient revenue classification is null, add an error to the field
        // and return null to remain on the current page...
        if(eprb.RevenueClassification__c == null) {
            eprb.RevenueClassification__c.addError(label.MustEnterValue);
            return null;
        }
        else {
        	eprb.InclusiveUnitPrice__c = null;
        	eprb.UnitPrice__c = price;
            return sController.save();
        }
    }

    public Pagereference DeleteRecord() {
        string bkgPkgEventId = eprb.BookingPackageEvent__c;
        delete(eprb);
        if (bkgPkgEventId != null) {
            return(new PageReference('/' + bkgPkgEventId));
        } else {
            return(new PageReference('/'));
        }
    }

    public decimal getTotalRate() {       
        decimal totalRate = qtgs.GetInclusiveBaseRate(taxGroupId, eprb.RevenueClassification__c);
        if(eprb.AdminIsIncludedInInclusivePrice__c)
            totalRate += (eprb.AdminCharge__c==null?0: eprb.AdminCharge__c / 100)*(qtgs.GetInclusiveAdminRate(taxGroupId, eprb.RevenueClassification__c));
        if(eprb.GratuityIsIncludedInInclusivePrice__c)
            totalRate += (eprb.Gratuity__c==null?0: eprb.Gratuity__c / 100)*(qtgs.GetInclusiveGratuityRate(taxGroupId, eprb.RevenueClassification__c));
        return totalRate==null?1:totalRate;
    }
 
    public void UpdateInclusivePrice() {
    	price = eprb.UnitPrice__c;	 	 
        CalculateInclusive();
    }
    
    public void UpdateUnitPrice() {   
    	inclusivePrice = eprb.InclusiveUnitPrice__c;	
    	CalculateExclusive();      
    }
    
    public pagereference SaveFromEnterInclusive() {
		UpdateInclusivePrice(); 
		return save();
	}
	
	public pagereference SaveFromEnterUnit() {
		UpdateUnitPrice();
		return save();
	}
    
    public void CalculateInclusive() {
    	inclusivePrice = (price==null?0:price)*(getTotalRate());
    	eprb.UnitPrice__c = (price==null?0:price.setScale(2, RoundingMode.HALF_UP));
    	eprb.InclusiveUnitPrice__c = (inclusivePrice==null?0:inclusivePrice.setScale(2, RoundingMode.HALF_UP));
    }
    
    public void CalculateExclusive() {
    	price = (inclusivePrice==null?0:inclusivePrice)/(getTotalRate());
        eprb.UnitPrice__c = (price==null?0:price.setScale(2, RoundingMode.HALF_UP));
        eprb.InclusiveUnitPrice__c = (inclusivePrice==null?0:inclusivePrice.setScale(2, RoundingMode.HALF_UP));        
    }

}

Any help would greatly be appreciated. I've tried actionsupport immediate false for the save, sending an onclick event to js when save clicked an attempted to prevent the click event, and using onblur as well.
Adeel Ahmad 17Adeel Ahmad 17
You should call the onChange after onComplete of first function and the second one will be the action function that will be called from onComplete of first.