+ Start a Discussion
cnp_nareshcnp_naresh 

Updating custom object is not working

HI Every body, I have a apex class, visualforce page. I am creating records and updating records of a custom object with this class and page only.Inserting is working well.But updating was working fine before but now its not working.

 

Apex class:

public with sharing class DiscountPlan{

    public DiscountPlan() {

    }
    public string DiscountRecordId {get;set;}

    public DiscountPlan(ApexPages.StandardController controller) 
                        {
                        DiscountRecordId=ApexPages.currentPage().getParameters().get('Id');
                        if(DiscountRecordId == null) 
                                {
                                    CreateDiscountPlan= new Discount_plan__c();
    
                                }
                        if(DiscountRecordId != null)
                                {
                                    CreateDiscountPlan=[select Id,Name,End_of_plan__c,Coupon_code__c,discount__c,expire_date__c,Event_name__c,fixed_discount__c,inventory__c,Max_number__c,Min_number__c,Registration_level__c,Start_date__c,Registration_level__r.Price__c from Discount_plan__c where id=:DiscountRecordId];
                                }
    
                        }

    Discount_plan__c CreateDiscountPlan;
    public Discount_plan__c GetCreateDiscountPlan(){return CreateDiscountPlan;}
    public void setCreateDiscountPlan(Discount_plan__c CreateDiscountPlan) {this.CreateDiscountPlan=CreateDiscountPlan;}
    public boolean Inventory{get;set;}
    public boolean AddGroupDiscount {get;set;}
    public boolean AddCouponDiscount{get;set;}
    public string EndOfInventory{get;set;}
    public string SelectPlan{get;set;}
    public Decimal EventPrice1{get;set;}
    Decimal EventPrice;
    public string TotalDiscount{get;set;}
    list<string> ObjectFields=new list<string>();
    public integer RelatedDiscountPlansSize{get;set;}
   

    public decimal getEventPrice()
                    {
                    Registration_level__c LevelRec=[Select Id,Price__c from Registration_level__c where Id=:ApexPages.currentPage().getParameters().get('RL_id') or Id=:CreateDiscountPlan.Registration_level__c];
                    EventPrice=LevelRec.Price__c;
                    EventPrice=EventPrice.setscale(2);
                    
                    return EventPrice;
                    }
    public void setEventPrice(decimal EventPrice){this.EventPrice=EventPrice;}
    public string getEventName(){return ApexPages.currentPage().getParameters().get('Event_name');}

    public string getRegistrationLevelName(){return ApexPages.currentPage().getParameters().get('Rl_name');}
           
    
    
    public PageReference save(){
                        if(DiscountRecordId == null){
                        CreateDiscountPlan.Registration_level__c=ApexPages.currentPage().getParameters().get('RL_id');
                        CreateDiscountPlan.Event_name__c=ApexPages.currentPage().getParameters().get('Event_Id');
                        system.debug('Session id'+ApexPages.currentPage().getParameters().get('Session_id'));
                        insert CreateDiscountPlan;
                        }
                        if(DiscountRecordId != null){
                        system.debug('check inventory'+CreateDiscountPlan.inventory__c);
                        update CreateDiscountPlan;
                        }
                        PageReference RedirectUrl;
                        If(CreateDiscountPlan.Registration_level__c!=null){
                        RedirectUrl= new PageReference('/'+CreateDiscountPlan.Registration_level__c);
                        }
                        RedirectUrl.setRedirect(true);

                        return RedirectUrl;
                 }
    public PageReference cancel(){
                        CreateDiscountPlan.Registration_level__c=ApexPages.currentPage().getParameters().get('RL_id');
                        PageReference RedirectUrl;
                        If(CreateDiscountPlan.Registration_level__c!=null){
                        RedirectUrl= new PageReference('/'+CreateDiscountPlan.Registration_level__c);
                        }
                        RedirectUrl.setRedirect(true);
                        return RedirectUrl;

}
/*public void SaveAndNew(){
CreateDiscountPlan.Registration_level__c=ApexPages.currentPage().getParameters().get('RL_id');
CreateDiscountPlan.Event_name__c=ApexPages.currentPage().getParameters().get('Event_Id');
//CreateDiscountPlan.Session__c=ApexPages.currentPage().getParameters().get('Session_id');
system.debug('Session id'+ApexPages.currentPage().getParameters().get('Session_id'));
insert CreateDiscountPlan;
CreateDiscountPlan=null;
}*/

    public PageReference UploadCouponfile(){
                            PageReference returnURL;
                            returnURL = new PageReference('/apex/UploadCSVDiscounts');
                            returnURL.setRedirect(true);
                            return returnURL;
                        }

    public List<SelectOption> getRelatedDiscountPlans() {
                                 List<SelectOption> options = new List<SelectOption>();
                                string LevelId=ApexPages.currentPage().getParameters().get('RL_id');

                                String queryString = 'SELECT Id,Name FROM Discount_plan__c where Registration_level__c=:LevelId';
                                options.add(new SelectOption('--Select--','--Select--'));
                                for(sObject o : Database.query(queryString)){
                                            options.add(new SelectOption((String)o.get('Id'),(String)o.get('Name')));
                                            //Event_Id_Name_Map.put((String)o.get('Name'),(Id)o.get('Id'));
                                            }
                                RelatedDiscountPlansSize=options.size();
                            return options;
                    }
}

 Visualforce page:

<apex:page standardController="Discount_plan__c" extensions="DiscountPlan">
<script>
window.onload=DisableFunction;
function DisableFunction(){
var Pricefield= document.getElementById('{!$Component.CreateDiscountPlan.CouponRecord.CouponInfo.AmountItem.Eventinfo_Price}');
Pricefield.disabled=true;
TotalDiscount();

}
function TotalDiscount(){
//alert('naresh');
var desc= document.getElementById('{!$Component.CreateDiscountPlan.CouponRecord.CouponInfo.AmountItem.Eventinfo_Cdiscount}').value;
var Price= document.getElementById('{!$Component.CreateDiscountPlan.CouponRecord.CouponInfo.AmountItem.Eventinfo_Price}').value;
var FixedDisc= document.getElementById('{!$Component.CreateDiscountPlan.CouponRecord.CouponInfo.AmountItem.Eventinfo_Fixeddiscount}').value;
var totalDisc=document.getElementById('{!$Component.CreateDiscountPlan.CouponRecord.CouponInfo.AmountItem.Eventinfo_TotalDisc}');
var IntFixedDesc=parseFloat(FixedDisc)
totalDisc.value=(desc*Price+IntFixedDesc);

//alert(totalDisc.value);

}
</script>
<apex:form id="CreateDiscountPlan">
        <apex:pageBlock id="CouponRecord" title="Discount plan">
                <apex:pageBlockButtons >
                        <apex:commandButton action="{!save}" value="Save" immediate="true"/>
                        
                        <!--<apex:commandButton action="{!SaveAndNew}" value="Save and new"/>-->
                        <apex:commandButton action="{!cancel}" value="Cancel"/>
                        <apex:commandButton action="{!UploadCouponfile}" value="Upload a discount file"/>
                </apex:pageBlockButtons>
                <apex:pageBlockSection id="CouponInfo" title="Plan details" columns="1" collapsible="false">
                        <apex:pageBlockSectionItem rendered="{!IF(EventName!=null,true,false)}">
                                <apex:outputLabel value="Event name"/>
                                <apex:outputText value="{!EventName}"/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem rendered="{!if((RegistrationLevelName!=null && EventName!=null),true,false)}">
                                <apex:outputLabel value="Registration level name"/>
                                <apex:outputText value="{!RegistrationLevelName}"/>
                        </apex:pageBlockSectionItem>                                               
                        <apex:pageBlockSectionItem rendered="{!IF(CreateDiscountPlan.Event_name__c!=null,true,false)}">
                                <apex:outputLabel value="Event name"/>
                                <apex:outputPanel >
                                        <apex:inputField value="{!CreateDiscountPlan.Event_name__c}" style="width:250px" id="FieldInfo_Event"/>
                                        <script>document.getElementById('{!$Component.FieldInfo_Event}').disabled = true; </script>
                                </apex:outputPanel>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem rendered="{!IF(CreateDiscountPlan.Registration_level__c!=null,true,false)}">
                                <apex:outputLabel value="Registration level name"/>
                                        <apex:outputPanel >
                                                <apex:inputField value="{!CreateDiscountPlan.Registration_level__c}" style="width:250px" id="FieldInfo_level"/>
                                                <script>document.getElementById('{!$Component.FieldInfo_level}').disabled = true; </script>
                                        </apex:outputPanel>
                        </apex:pageBlockSectionItem>
                        <apex:inputField value="{!CreateDiscountPlan.Name}" id="Eventinfo_PlanName"/>
                        <apex:pageBlockSectionItem Id="AmountItem">
                                <apex:outputPanel >
                                        <b><apex:outputLabel value="Total Discount"/></b>
                                </apex:outputPanel>
                                <apex:outputPanel onkeyup="TotalDiscount();">
                                        &nbsp;&nbsp;&nbsp;Discount %&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fixed discount&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Total discount<br />
                                        &nbsp;(&nbsp;<apex:inputField value="{!CreateDiscountPlan.discount__c}" style="width:60px;" id="Eventinfo_Cdiscount"/>&nbsp;&nbsp;X&nbsp;&nbsp;
                                        <apex:inputText value="{!EventPrice}" style="width:90px;" id="Eventinfo_Price" />
                                        &nbsp;)+&nbsp;
                                        <apex:inputField value="{!CreateDiscountPlan.fixed_discount__c}" style="width:80px;" id="Eventinfo_Fixeddiscount"/>&nbsp;=&nbsp;
                                        <apex:inputText value="{!TotalDiscount}" style="width:60px;" disabled="true" id="Eventinfo_TotalDisc"/>
                                </apex:outputPanel>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                                <apex:outputLabel value="Start date" />
                                        <apex:outputPanel >
                                        <apex:inputField value="{!CreateDiscountPlan.Start_date__c}" id="Eventinfo_Startdate"/>
                                        <apex:outputPanel rendered="{!IF(RelatedDiscountPlans.size>1,true,false)}">
                                        <b>OR</b> End of plan &nbsp;&nbsp;&nbsp;
                                        <apex:selectList id="AccountId" value="{!CreateDiscountPlan.End_of_plan__c}" size="1">                 
                                                <apex:selectOptions value="{!RelatedDiscountPlans}"/>
                                        </apex:selectList>
                                </apex:outputPanel>
                                </apex:outputPanel>
                        </apex:pageBlockSectionItem>
                        <apex:inputField value="{!CreateDiscountPlan.expire_date__c}" id="Eventinfo_Enddate"/>
                        <apex:pageBlockSectionItem > 
                                <apex:outputPanel style="position:relative;left:90px;">
                                <apex:inputCheckbox value="{!Inventory}">
                                <apex:actionSupport event="onchange" reRender="DisplayInventory"/>
                                </apex:inputCheckbox>
                                <apex:outputLabel value="Add inventory"></apex:outputLabel><br />
                                <apex:outputPanel id="DisplayInventory"  style="position:relative;left:50px;">
                                <apex:outputPanel rendered="{!Inventory==true}">
                                <b><apex:outputLabel value="Inventory"></apex:outputLabel></b>
                                <apex:inputField value="{!CreateDiscountPlan.inventory__c}" style="position:relative;left:15px;" id="Eventinfo_CInventory"/><br /><br />
                                </apex:outputPanel>
                                </apex:outputPanel>
                                </apex:outputPanel>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                                <apex:outputPanel style="position:relative;left:90px;">
                                <apex:inputCheckbox value="{!AddGroupDiscount}">
                                <apex:actionSupport event="onchange" reRender="DisplayGroupDiscount"/>
                                </apex:inputCheckbox>
                                <apex:outputLabel value="Add group discount"></apex:outputLabel><br />
                                <apex:outputPanel id="DisplayGroupDiscount"   style="position:relative;left:50px;">
                                <apex:outputPanel rendered="{!AddGroupDiscount==true}">
                                <b><apex:outputLabel value="Based on # registered" /></b>&nbsp;&nbsp;&nbsp;&nbsp;
                                <apex:outputLabel value="Min number"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:outputLabel value="Max number"/><br />
                                <apex:inputField value="{!CreateDiscountPlan.Min_number__c}" style="position:relative;left:130px;width:80px;" id="Eventinfo_MinNumber"/>&nbsp;&nbsp;&nbsp;&nbsp;
                                <apex:inputField value="{!CreateDiscountPlan.Max_number__c}" style="position:relative;left:130px;width:80px;"  id="Eventinfo_MaxNumber"/>
                                </apex:outputPanel>
                                </apex:outputPanel>
                                </apex:outputPanel>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                                <apex:outputPanel style="position:relative;left:90px;">
                                <apex:inputCheckbox value="{!AddCouponDiscount}">
                                <apex:actionSupport event="onchange" reRender="DisplayCouponDiscount"/>
                                </apex:inputCheckbox>
                                <apex:outputLabel value="Add discount coupon" /><br />
                                <apex:outputPanel id="DisplayCouponDiscount" style="position:relative;left:50px;">
                                <apex:outputPanel rendered="{!AddCouponDiscount==true}">
                                <b><apex:outputLabel value="Coupon code" /></b>
                                <apex:inputField value="{!CreateDiscountPlan.Coupon_code__c}"  style="position:relative;left:20px;" id="Eventinfo_Ccode"/>
                                </apex:outputPanel>
                                </apex:outputPanel>
                                </apex:outputPanel>
                        </apex:pageBlockSectionItem>
                </apex:pageBlockSection>                 
        </apex:pageBlock>
</apex:form> 
</apex:page>

 

   I did inserting and updating records like this many times, every where its working fine, but here its not working.

 

  I checked with debug logs.The problem is I am unable to get the new value from visualforce page to apex method(save). So only updating is happening with old values.

 

  I spent many hours to find the issue But I am unable to find. can somebody please help me.

 

Thanks,

Naresh

bob_buzzardbob_buzzard

Your save button has the immediate attribute set to true:

 

 <apex:commandButton action="{!save}" value="Save" immediate="true"/>

 This tells visualforce to skip validation rules and not apply changes to the data.

cnp_nareshcnp_naresh

Great guess. It worked perfect.

 

But one more issue. If I remove Immediate attribute from commandbutton, then inserting and updating both are not happening when I click Save button. Because I have below line in JavaScript.

 

window.onload=DisableFunction;  

 

If I comment this line, then saving and updating working well. But I need this line in my program. How to execute insert and update by keeping above JavaScript code. Please help

 

Thanks,

Naresh B

bob_buzzardbob_buzzard

What does DisableFunction do?

cnp_nareshcnp_naresh

HI Bob,

 

    Thanks for the reply. That disableFunction will disable a field in visualforce page. You can find this function in my visualforce page code.

 

Thanks,

Naresh B

bob_buzzardbob_buzzard

It may be that you've overridden an existing Visualforce onload function.  Try adding your function to the end of the existing one rather than overwriting, as per one of my blog posts:

 

http://bobbuzzard.blogspot.com/2011/05/onload-handling.html