+ Start a Discussion
sheila srivatsavsheila srivatsav 

database.upsert issue

I have a custom button defined in the page layout of account
when I click this button, enter the account number then it should save back to object
public with sharing class InitiateApprovalController {

    public Account acc{get;set;}
    
    public string accid;
    
    public InitiateApprovalController(ApexPages.StandardController stdcontroller) {

    accid=ApexPages.CurrentPage().getParameters().get('id');
    this.acc=(Account)stdcontroller.getRecord();
    
    acc=[select ID,Name,AccountNumber from Account where ID =:acc.ID];
       
    }
    
    public InitiateApprovalController()
    {
       acc=new Account();
         
    }

    public PageReference customsave()
    {
        try{
        system.debug('acc before update ='+acc);
            
        database.Upsert(acc);
        PageReference pr=new PageReference('/'+acc.Id);

        return pr;
        }
        catch( Exception e ){
            ApexPages.addMessages(e);
            return Null;
        }       
    }
    
}

<apex:page standardController="Account"
            extensions="InitiateApprovalController">
  
  <apex:form >
  
  <apex:pageBlock mode="edit">
            <apex:pageBlockSection title="Approvers" 
                                   id="ApproversId">
                                   
             <apex:inputField Id="name" 
                              html-placeholder="Account Name.." 
                              value="{!Account.Name}" />

             <apex:inputField Id="accnumber" 
                              html-placeholder="Account Number.." 
                              value="{!Account.AccountNumber}" />
                                 
            </apex:pageBlockSection>
            
    <apex:pageBlockButtons location="bottom" html-align="right">
    
    <apex:commandButton id="customsave1"
                        value="save"
                        action="{!customsave}"/>
                        
    </apex:pageBlockButtons>
    
     </apex:pageBlock>
  </apex:form>
    
</apex:page>
I am surprised because I am using Database.Upsert but still it is not saving to object

Can any one please let me know?

Thanks
sheila
 
Best Answer chosen by sheila srivatsav
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sheila,

I trust you are doing very well.

The issue is that you are using value="{!Account.Name}" and value="{!Account.AccountNumber}" in inputField in Visualforce. Whereas, in extension controller, you are using public Account acc{get;set;}  and upserting acc. But, there is no acc in visualforce.

Update your code:

Visualforce:
<apex:page standardController="Account"
           extensions="InitiateApprovalController">
    
    <apex:form >
        
        <apex:pageBlock mode="edit">
            <apex:pageBlockSection title="Approvers" 
                                   id="ApproversId">
                
                <apex:inputField Id="name" 
                                 html-placeholder="Account Name.." 
                                 value="{!acc.Name}" />
                
                <apex:inputField Id="accnumber" 
                                 html-placeholder="Account Number.." 
                                 value="{!acc.AccountNumber}" />
                
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons location="bottom" html-align="right">
                
                <apex:commandButton id="customsave1"
                                    value="save"
                                    action="{!customsave}"/>
                
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

Controller:
public class UpsertAccC {
    
    public Account acc{get;set;}
    
    public string accid;
    
    public InitiateApprovalController(ApexPages.StandardController stdcontroller) {
        
        accid=ApexPages.CurrentPage().getParameters().get('id');
        this.acc=(Account)stdcontroller.getRecord();
        
        acc=[select ID,Name,AccountNumber from Account where ID =:acc.ID];
        
    }
    
    public InitiateApprovalController()
    {
        acc=new Account();
        
    }
    
    public PageReference customsave()
    {
        try{
            system.debug('acc before update ='+acc);
            
            database.Upsert(acc);
            PageReference pr=new PageReference('/'+acc.Id);
            
            return pr;
        }
        catch( Exception e ){
            ApexPages.addMessages(e);
            return Null;
        }       
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sheila,

I trust you are doing very well.

The issue is that you are using value="{!Account.Name}" and value="{!Account.AccountNumber}" in inputField in Visualforce. Whereas, in extension controller, you are using public Account acc{get;set;}  and upserting acc. But, there is no acc in visualforce.

Update your code:

Visualforce:
<apex:page standardController="Account"
           extensions="InitiateApprovalController">
    
    <apex:form >
        
        <apex:pageBlock mode="edit">
            <apex:pageBlockSection title="Approvers" 
                                   id="ApproversId">
                
                <apex:inputField Id="name" 
                                 html-placeholder="Account Name.." 
                                 value="{!acc.Name}" />
                
                <apex:inputField Id="accnumber" 
                                 html-placeholder="Account Number.." 
                                 value="{!acc.AccountNumber}" />
                
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons location="bottom" html-align="right">
                
                <apex:commandButton id="customsave1"
                                    value="save"
                                    action="{!customsave}"/>
                
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

Controller:
public class UpsertAccC {
    
    public Account acc{get;set;}
    
    public string accid;
    
    public InitiateApprovalController(ApexPages.StandardController stdcontroller) {
        
        accid=ApexPages.CurrentPage().getParameters().get('id');
        this.acc=(Account)stdcontroller.getRecord();
        
        acc=[select ID,Name,AccountNumber from Account where ID =:acc.ID];
        
    }
    
    public InitiateApprovalController()
    {
        acc=new Account();
        
    }
    
    public PageReference customsave()
    {
        try{
            system.debug('acc before update ='+acc);
            
            database.Upsert(acc);
            PageReference pr=new PageReference('/'+acc.Id);
            
            return pr;
        }
        catch( Exception e ){
            ApexPages.addMessages(e);
            return Null;
        }       
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
sheila srivatsavsheila srivatsav
Hi khan

It was gr8 to see your post,
My mistake . 
The changes you said are working.
thanks once again.

sheila